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