11#include <stdio.h>
22#include <stdlib.h> // For malloc and free
3+ #include <string.h>
4+
35#include "py/obj.h"
46#include "py/runtime.h"
57#include "py/mperrno.h"
6- #include <string.h>
7- #include "../quirc/lib/quirc.h"
8+
89#include "freertos/FreeRTOS.h" // For uxTaskGetStackHighWaterMark
910#include "freertos/task.h" // For task-related functions
1011
12+ #include "../quirc/lib/quirc.h"
13+
1114#define QRDECODE_DEBUG_PRINT (...) mp_printf(&mp_plat_print, __VA_ARGS__);
1215
1316// Function to decode a QR code from a grayscale image buffer
1417static mp_obj_t qrdecode (mp_uint_t n_args , const mp_obj_t * args ) {
1518 QRDECODE_DEBUG_PRINT ("qrdecode: Starting\n" );
16- mp_printf ( & mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
19+ QRDECODE_DEBUG_PRINT ( "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
1720
1821 // Check argument count (expecting buffer, width, height)
1922 QRDECODE_DEBUG_PRINT ("qrdecode: Checking argument count\n" );
20- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
21- fflush (stdout );
2223 if (n_args != 3 ) {
2324 mp_raise_ValueError (MP_ERROR_TEXT ("quirc_decode expects 3 arguments: buffer, width, height" ));
2425 }
2526
2627 // Extract buffer
2728 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting buffer\n" );
28- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
29- fflush (stdout );
3029 mp_buffer_info_t bufinfo ;
3130 mp_get_buffer_raise (args [0 ], & bufinfo , MP_BUFFER_READ );
32- printf ("qrdecode: Buffer extracted, len=%zu\n" , bufinfo .len );
3331
3432 // Extract width and height
3533 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting width and height\n" );
36- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
37- fflush (stdout );
3834 mp_int_t width = mp_obj_get_int (args [1 ]);
3935 mp_int_t height = mp_obj_get_int (args [2 ]);
40- QRDECODE_DEBUG_PRINT ("qrdecode: Width=%ld, Height=%ld\n" , width , height );
41- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
42- fflush (stdout );
36+ QRDECODE_DEBUG_PRINT ("qrdecode: Width=%u, Height=%u\n" , width , height );
4337
4438 // Validate dimensions
4539 QRDECODE_DEBUG_PRINT ("qrdecode: Validating dimensions\n" );
46- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
47- fflush (stdout );
4840 if (width <= 0 || height <= 0 ) {
4941 mp_raise_ValueError (MP_ERROR_TEXT ("width and height must be positive" ));
5042 }
5143 if (bufinfo .len != (size_t )(width * height )) {
5244 mp_raise_ValueError (MP_ERROR_TEXT ("buffer size must match width * height" ));
5345 }
5446 QRDECODE_DEBUG_PRINT ("qrdecode: Dimensions validated\n" );
55- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
56- fflush (stdout );
5747
5848 // Initialize quirc
5949 QRDECODE_DEBUG_PRINT ("qrdecode: Initializing quirc\n" );
60- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
61- fflush (stdout );
6250 struct quirc * qr = quirc_new ();
6351 if (!qr ) {
6452 mp_raise_OSError (MP_ENOMEM );
6553 }
6654 QRDECODE_DEBUG_PRINT ("qrdecode: quirc initialized\n" );
67- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
68- fflush (stdout );
6955
7056 // Resize quirc for the image dimensions
7157 QRDECODE_DEBUG_PRINT ("qrdecode: Resizing quirc\n" );
72- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
73- fflush (stdout );
7458 if (quirc_resize (qr , width , height ) < 0 ) {
7559 quirc_destroy (qr );
7660 mp_raise_OSError (MP_ENOMEM );
7761 }
7862 QRDECODE_DEBUG_PRINT ("qrdecode: quirc resized\n" );
79- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
80- fflush (stdout );
8163
8264 // Get quirc image buffer and copy grayscale data
8365 QRDECODE_DEBUG_PRINT ("qrdecode: Beginning quirc processing\n" );
84- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
85- fflush (stdout );
8666 uint8_t * image ;
8767 quirc_begin (qr , NULL , NULL );
8868 image = quirc_begin (qr , NULL , NULL ); // Get pointer to quirc's image buffer
8969 QRDECODE_DEBUG_PRINT ("qrdecode: quirc image buffer obtained\n" );
90- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
91- fflush (stdout );
92- QRDECODE_DEBUG_PRINT ("qrdecode: Copying buffer, size=%ul\n" , (size_t )(width * height ));
93- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
94- fflush (stdout );
70+ QRDECODE_DEBUG_PRINT ("qrdecode: Copying buffer, size=%u\n" , (size_t )(width * height ));
9571 memcpy (image , bufinfo .buf , width * height );
9672 QRDECODE_DEBUG_PRINT ("qrdecode: Buffer copied\n" );
97- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
98- fflush (stdout );
9973 quirc_end (qr );
10074 QRDECODE_DEBUG_PRINT ("qrdecode: quirc processing ended\n" );
101- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
102- fflush (stdout );
10375
10476 // Check for QR codes
10577 QRDECODE_DEBUG_PRINT ("qrdecode: Counting QR codes\n" );
106- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
107- fflush (stdout );
78+ QRDECODE_DEBUG_PRINT ("qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
10879 int count = quirc_count (qr );
10980 QRDECODE_DEBUG_PRINT ("qrdecode: Found %d QR codes\n" , count );
110- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
111- fflush (stdout );
81+ QRDECODE_DEBUG_PRINT ("qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
11282 if (count == 0 ) {
11383 quirc_destroy (qr );
11484 mp_raise_ValueError (MP_ERROR_TEXT ("no QR code found" ));
11585 }
116- // it works until here, it finds 1 QR code!
11786
11887 // Extract and decode the first QR code
11988 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting first QR code\n" );
120- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
121- fflush (stdout );
12289 struct quirc_code * code = (struct quirc_code * )malloc (sizeof (struct quirc_code ));
12390 if (!code ) {
12491 quirc_destroy (qr );
12592 mp_raise_OSError (MP_ENOMEM );
12693 }
12794 QRDECODE_DEBUG_PRINT ("qrdecode: Allocated quirc_code on heap\n" );
128- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
129- fflush (stdout );
95+ QRDECODE_DEBUG_PRINT ("qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
13096 quirc_extract (qr , 0 , code );
13197 QRDECODE_DEBUG_PRINT ("qrdecode: QR code extracted\n" );
132- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
133- fflush (stdout );
98+ QRDECODE_DEBUG_PRINT ("qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
13499 // it works until here!
135100
136101 // Decode the QR code - this is the part that fails:
137102 QRDECODE_DEBUG_PRINT ("qrdecode: Decoding QR code\n" );
138- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
139- fflush (stdout );
140103 struct quirc_data * data = (struct quirc_data * )malloc (sizeof (struct quirc_data ));
141104 if (!data ) {
142105 free (code );
143106 quirc_destroy (qr );
144107 mp_raise_OSError (MP_ENOMEM );
145108 }
146109 QRDECODE_DEBUG_PRINT ("qrdecode: Allocated quirc_data on heap\n" );
147- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
148- fflush (stdout );
110+ QRDECODE_DEBUG_PRINT ("qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
149111 int err = quirc_decode (code , data );
150112 if (err != QUIRC_SUCCESS ) {
151113 free (data );
@@ -154,43 +116,25 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
154116 mp_raise_ValueError (MP_ERROR_TEXT ("failed to decode QR code" ));
155117 }
156118 QRDECODE_DEBUG_PRINT ("qrdecode: QR code decoded, payload_len=%d\n" , data -> payload_len );
157- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
158- fflush (stdout );
159-
160- QRDECODE_DEBUG_PRINT ("qrdecode: got result: %s\n" , data -> payload );
161- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
162- fflush (stdout );
163- QRDECODE_DEBUG_PRINT ("ok so now what?!" );
164- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
119+ QRDECODE_DEBUG_PRINT ("qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
120+ //QRDECODE_DEBUG_PRINT("qrdecode: got result: %s\n", data->payload);
165121
166122 // Convert decoded data to Python string
167123 QRDECODE_DEBUG_PRINT ("qrdecode: Creating Python string\n" );
168- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
169- fflush (stdout );
170124 mp_obj_t result = mp_obj_new_str ((const char * )data -> payload , data -> payload_len );
171125 QRDECODE_DEBUG_PRINT ("qrdecode: Python string created\n" );
172- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
173- fflush (stdout );
174126
175127 // Clean up
176128 QRDECODE_DEBUG_PRINT ("qrdecode: Cleaning up\n" );
177- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
178- fflush (stdout );
179129 free (data );
180130 free (code );
181131 quirc_destroy (qr );
182- QRDECODE_DEBUG_PRINT ("qrdecode: quirc destroyed\n" );
183- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
184- fflush (stdout );
185-
186- QRDECODE_DEBUG_PRINT ("qrdecode: Returning result\n" );
187- mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark: %u bytes\n" , uxTaskGetStackHighWaterMark (NULL ));
132+ QRDECODE_DEBUG_PRINT ("qrdecode: quirc destroyed, returning result\n" );
188133 return result ;
189134}
190135
191136// Wrapper function to fix incompatible pointer type warning
192137static mp_obj_t qrdecode_wrapper (size_t n_args , const mp_obj_t * args ) {
193- printf ("qrdecode_wrapper: Called with %zu args\n" , n_args );
194138 return qrdecode (n_args , args );
195139}
196140
0 commit comments