|
1 | 1 | #include "py/obj.h" |
2 | 2 | #include "py/runtime.h" |
3 | 3 | #include "py/mperrno.h" |
4 | | - |
5 | 4 | #include <string.h> |
6 | | - |
7 | 5 | #include "../quirc/lib/quirc.h" |
8 | 6 |
|
9 | 7 | // Function to decode a QR code from a grayscale image buffer |
10 | 8 | 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 | + |
12 | 11 | // Check argument count (expecting buffer, width, height) |
| 12 | + printf("qrdecode: Checking argument count\n"); |
13 | 13 | if (n_args != 3) { |
14 | 14 | mp_raise_ValueError(MP_ERROR_TEXT("quirc_decode expects 3 arguments: buffer, width, height")); |
15 | 15 | } |
16 | 16 |
|
17 | 17 | // Extract buffer |
| 18 | + printf("qrdecode: Extracting buffer\n"); |
18 | 19 | mp_buffer_info_t bufinfo; |
19 | 20 | mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); |
| 21 | + printf("qrdecode: Buffer extracted, len=%zu\n", bufinfo.len); |
20 | 22 |
|
21 | 23 | // Extract width and height |
| 24 | + printf("qrdecode: Extracting width and height\n"); |
22 | 25 | mp_int_t width = mp_obj_get_int(args[1]); |
23 | 26 | mp_int_t height = mp_obj_get_int(args[2]); |
| 27 | + printf("qrdecode: Width=%ld, Height=%ld\n", width, height); |
24 | 28 |
|
25 | 29 | // Validate dimensions |
| 30 | + printf("qrdecode: Validating dimensions\n"); |
26 | 31 | if (width <= 0 || height <= 0) { |
27 | 32 | mp_raise_ValueError(MP_ERROR_TEXT("width and height must be positive")); |
28 | 33 | } |
29 | 34 | if (bufinfo.len != (size_t)(width * height)) { |
30 | 35 | mp_raise_ValueError(MP_ERROR_TEXT("buffer size must match width * height")); |
31 | 36 | } |
| 37 | + printf("qrdecode: Dimensions validated\n"); |
32 | 38 |
|
33 | 39 | // Initialize quirc |
| 40 | + printf("qrdecode: Initializing quirc\n"); |
34 | 41 | struct quirc *qr = quirc_new(); |
35 | 42 | if (!qr) { |
36 | 43 | mp_raise_OSError(MP_ENOMEM); |
37 | 44 | } |
| 45 | + printf("qrdecode: quirc initialized\n"); |
38 | 46 |
|
39 | 47 | // Resize quirc for the image dimensions |
| 48 | + printf("qrdecode: Resizing quirc\n"); |
40 | 49 | if (quirc_resize(qr, width, height) < 0) { |
41 | 50 | quirc_destroy(qr); |
42 | 51 | mp_raise_OSError(MP_ENOMEM); |
43 | 52 | } |
| 53 | + printf("qrdecode: quirc resized\n"); |
44 | 54 |
|
45 | 55 | // Get quirc image buffer and copy grayscale data |
| 56 | + printf("qrdecode: Beginning quirc processing\n"); |
46 | 57 | uint8_t *image; |
47 | 58 | quirc_begin(qr, NULL, NULL); |
48 | 59 | 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"); |
50 | 64 | quirc_end(qr); |
| 65 | + printf("qrdecode: quirc processing ended\n"); |
51 | 66 |
|
52 | 67 | // Check for QR codes |
| 68 | + printf("qrdecode: Counting QR codes\n"); |
53 | 69 | int count = quirc_count(qr); |
| 70 | + printf("qrdecode: Found %d QR codes\n", count); |
54 | 71 | if (count == 0) { |
55 | 72 | quirc_destroy(qr); |
56 | 73 | mp_raise_ValueError(MP_ERROR_TEXT("no QR code found")); |
57 | 74 | } |
58 | 75 |
|
59 | 76 | // Extract and decode the first QR code |
| 77 | + printf("qrdecode: Extracting first QR code\n"); |
60 | 78 | 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"); |
63 | 81 |
|
64 | 82 | // Decode the QR code |
| 83 | + printf("qrdecode: Decoding QR code\n"); |
| 84 | + struct quirc_data data; |
65 | 85 | int err = quirc_decode(&code, &data); |
66 | 86 | if (err != QUIRC_SUCCESS) { |
67 | 87 | quirc_destroy(qr); |
68 | 88 | mp_raise_ValueError(MP_ERROR_TEXT("failed to decode QR code")); |
69 | 89 | } |
| 90 | + printf("qrdecode: QR code decoded, payload_len=%d\n", data.payload_len); |
70 | 91 |
|
71 | 92 | // Convert decoded data to Python string |
| 93 | + printf("qrdecode: Creating Python string\n"); |
72 | 94 | mp_obj_t result = mp_obj_new_str((const char *)data.payload, data.payload_len); |
| 95 | + printf("qrdecode: Python string created\n"); |
73 | 96 |
|
74 | 97 | // Clean up |
| 98 | + printf("qrdecode: Cleaning up\n"); |
75 | 99 | quirc_destroy(qr); |
| 100 | + printf("qrdecode: quirc destroyed\n"); |
76 | 101 |
|
| 102 | + printf("qrdecode: Returning result\n"); |
77 | 103 | return result; |
78 | 104 | } |
79 | 105 |
|
| 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 | + |
80 | 112 | // 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); |
82 | 114 |
|
83 | 115 | // Module definition |
84 | 116 | static const mp_rom_map_elem_t qrdecode_module_globals_table[] = { |
|
0 commit comments