Skip to content

Commit ddb1167

Browse files
quirc_decode: add debug and fix warnings
1 parent 5738382 commit ddb1167

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

c_mpos/src/quirc_decode.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,116 @@
11
#include "py/obj.h"
22
#include "py/runtime.h"
33
#include "py/mperrno.h"
4-
54
#include <string.h>
6-
75
#include "../quirc/lib/quirc.h"
86

97
// Function to decode a QR code from a grayscale image buffer
108
static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
11-
printf("qrdecode running\n")
9+
printf("qrdecode: Starting\n");
10+
1211
// Check argument count (expecting buffer, width, height)
12+
printf("qrdecode: Checking argument count\n");
1313
if (n_args != 3) {
1414
mp_raise_ValueError(MP_ERROR_TEXT("quirc_decode expects 3 arguments: buffer, width, height"));
1515
}
1616

1717
// Extract buffer
18+
printf("qrdecode: Extracting buffer\n");
1819
mp_buffer_info_t bufinfo;
1920
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
21+
printf("qrdecode: Buffer extracted, len=%zu\n", bufinfo.len);
2022

2123
// Extract width and height
24+
printf("qrdecode: Extracting width and height\n");
2225
mp_int_t width = mp_obj_get_int(args[1]);
2326
mp_int_t height = mp_obj_get_int(args[2]);
27+
printf("qrdecode: Width=%ld, Height=%ld\n", width, height);
2428

2529
// Validate dimensions
30+
printf("qrdecode: Validating dimensions\n");
2631
if (width <= 0 || height <= 0) {
2732
mp_raise_ValueError(MP_ERROR_TEXT("width and height must be positive"));
2833
}
2934
if (bufinfo.len != (size_t)(width * height)) {
3035
mp_raise_ValueError(MP_ERROR_TEXT("buffer size must match width * height"));
3136
}
37+
printf("qrdecode: Dimensions validated\n");
3238

3339
// Initialize quirc
40+
printf("qrdecode: Initializing quirc\n");
3441
struct quirc *qr = quirc_new();
3542
if (!qr) {
3643
mp_raise_OSError(MP_ENOMEM);
3744
}
45+
printf("qrdecode: quirc initialized\n");
3846

3947
// Resize quirc for the image dimensions
48+
printf("qrdecode: Resizing quirc\n");
4049
if (quirc_resize(qr, width, height) < 0) {
4150
quirc_destroy(qr);
4251
mp_raise_OSError(MP_ENOMEM);
4352
}
53+
printf("qrdecode: quirc resized\n");
4454

4555
// Get quirc image buffer and copy grayscale data
56+
printf("qrdecode: Beginning quirc processing\n");
4657
uint8_t *image;
4758
quirc_begin(qr, NULL, NULL);
4859
image = quirc_begin(qr, NULL, NULL); // Get pointer to quirc's image buffer
49-
memcpy(image, bufinfo.buf, width * height); // Copy buffer directly (grayscale, 8-bit)
60+
printf("qrdecode: quirc image buffer obtained\n");
61+
printf("qrdecode: Copying buffer, size=%zu\n", (size_t)(width * height));
62+
memcpy(image, bufinfo.buf, width * height);
63+
printf("qrdecode: Buffer copied\n");
5064
quirc_end(qr);
65+
printf("qrdecode: quirc processing ended\n");
5166

5267
// Check for QR codes
68+
printf("qrdecode: Counting QR codes\n");
5369
int count = quirc_count(qr);
70+
printf("qrdecode: Found %d QR codes\n", count);
5471
if (count == 0) {
5572
quirc_destroy(qr);
5673
mp_raise_ValueError(MP_ERROR_TEXT("no QR code found"));
5774
}
5875

5976
// Extract and decode the first QR code
77+
printf("qrdecode: Extracting first QR code\n");
6078
struct quirc_code code;
61-
struct quirc_data data;
62-
quirc_extract(qr, 0, &code); // Extract first QR code
79+
quirc_extract(qr, 0, &code);
80+
printf("qrdecode: QR code extracted\n");
6381

6482
// Decode the QR code
83+
printf("qrdecode: Decoding QR code\n");
84+
struct quirc_data data;
6585
int err = quirc_decode(&code, &data);
6686
if (err != QUIRC_SUCCESS) {
6787
quirc_destroy(qr);
6888
mp_raise_ValueError(MP_ERROR_TEXT("failed to decode QR code"));
6989
}
90+
printf("qrdecode: QR code decoded, payload_len=%d\n", data.payload_len);
7091

7192
// Convert decoded data to Python string
93+
printf("qrdecode: Creating Python string\n");
7294
mp_obj_t result = mp_obj_new_str((const char *)data.payload, data.payload_len);
95+
printf("qrdecode: Python string created\n");
7396

7497
// Clean up
98+
printf("qrdecode: Cleaning up\n");
7599
quirc_destroy(qr);
100+
printf("qrdecode: quirc destroyed\n");
76101

102+
printf("qrdecode: Returning result\n");
77103
return result;
78104
}
79105

106+
// Wrapper function to fix incompatible pointer type warning
107+
static mp_obj_t qrdecode_wrapper(size_t n_args, const mp_obj_t *args) {
108+
printf("qrdecode_wrapper: Called with %zu args\n", n_args);
109+
return qrdecode(n_args, args);
110+
}
111+
80112
// Define the MicroPython function
81-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(qrdecode_obj, 3, 3, qrdecode);
113+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(qrdecode_obj, 3, 3, qrdecode_wrapper);
82114

83115
// Module definition
84116
static const mp_rom_map_elem_t qrdecode_module_globals_table[] = {

0 commit comments

Comments
 (0)