44#include "py/mperrno.h"
55#include <string.h>
66#include "../quirc/lib/quirc.h"
7+ #include "py/mpstate.h" // Added for micropython_stack_use()
78
89#define QRDECODE_DEBUG_PRINT (...) mp_printf(&mp_plat_print, __VA_ARGS__);
910
1011// Function to decode a QR code from a grayscale image buffer
1112static mp_obj_t qrdecode (mp_uint_t n_args , const mp_obj_t * args ) {
1213 QRDECODE_DEBUG_PRINT ("qrdecode: Starting\n" );
14+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
1315
1416 // Check argument count (expecting buffer, width, height)
1517 QRDECODE_DEBUG_PRINT ("qrdecode: Checking argument count\n" );
18+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
1619 fflush (stdout );
1720 if (n_args != 3 ) {
1821 mp_raise_ValueError (MP_ERROR_TEXT ("quirc_decode expects 3 arguments: buffer, width, height" ));
1922 }
2023
2124 // Extract buffer
2225 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting buffer\n" );
26+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
2327 fflush (stdout );
2428 mp_buffer_info_t bufinfo ;
2529 mp_get_buffer_raise (args [0 ], & bufinfo , MP_BUFFER_READ );
2630 printf ("qrdecode: Buffer extracted, len=%zu\n" , bufinfo .len );
2731
2832 // Extract width and height
2933 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting width and height\n" );
34+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
3035 fflush (stdout );
3136 mp_int_t width = mp_obj_get_int (args [1 ]);
3237 mp_int_t height = mp_obj_get_int (args [2 ]);
3338 QRDECODE_DEBUG_PRINT ("qrdecode: Width=%ld, Height=%ld\n" , width , height );
39+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
3440 fflush (stdout );
3541
3642 // Validate dimensions
3743 QRDECODE_DEBUG_PRINT ("qrdecode: Validating dimensions\n" );
44+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
3845 fflush (stdout );
3946 if (width <= 0 || height <= 0 ) {
4047 mp_raise_ValueError (MP_ERROR_TEXT ("width and height must be positive" ));
@@ -43,50 +50,62 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
4350 mp_raise_ValueError (MP_ERROR_TEXT ("buffer size must match width * height" ));
4451 }
4552 QRDECODE_DEBUG_PRINT ("qrdecode: Dimensions validated\n" );
53+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
4654 fflush (stdout );
4755
4856 // Initialize quirc
4957 QRDECODE_DEBUG_PRINT ("qrdecode: Initializing quirc\n" );
58+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
5059 fflush (stdout );
5160 struct quirc * qr = quirc_new ();
5261 if (!qr ) {
5362 mp_raise_OSError (MP_ENOMEM );
5463 }
5564 QRDECODE_DEBUG_PRINT ("qrdecode: quirc initialized\n" );
65+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
5666 fflush (stdout );
5767
5868 // Resize quirc for the image dimensions
5969 QRDECODE_DEBUG_PRINT ("qrdecode: Resizing quirc\n" );
70+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
6071 fflush (stdout );
6172 if (quirc_resize (qr , width , height ) < 0 ) {
6273 quirc_destroy (qr );
6374 mp_raise_OSError (MP_ENOMEM );
6475 }
6576 QRDECODE_DEBUG_PRINT ("qrdecode: quirc resized\n" );
77+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
6678 fflush (stdout );
6779
6880 // Get quirc image buffer and copy grayscale data
6981 QRDECODE_DEBUG_PRINT ("qrdecode: Beginning quirc processing\n" );
82+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
7083 fflush (stdout );
7184 uint8_t * image ;
7285 quirc_begin (qr , NULL , NULL );
7386 image = quirc_begin (qr , NULL , NULL ); // Get pointer to quirc's image buffer
7487 QRDECODE_DEBUG_PRINT ("qrdecode: quirc image buffer obtained\n" );
88+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
7589 fflush (stdout );
7690 QRDECODE_DEBUG_PRINT ("qrdecode: Copying buffer, size=%ul\n" , (size_t )(width * height ));
91+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
7792 fflush (stdout );
7893 memcpy (image , bufinfo .buf , width * height );
7994 QRDECODE_DEBUG_PRINT ("qrdecode: Buffer copied\n" );
95+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
8096 fflush (stdout );
8197 quirc_end (qr );
8298 QRDECODE_DEBUG_PRINT ("qrdecode: quirc processing ended\n" );
99+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
83100 fflush (stdout );
84101
85102 // Check for QR codes
86103 QRDECODE_DEBUG_PRINT ("qrdecode: Counting QR codes\n" );
104+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
87105 fflush (stdout );
88106 int count = quirc_count (qr );
89107 QRDECODE_DEBUG_PRINT ("qrdecode: Found %d QR codes\n" , count );
108+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
90109 fflush (stdout );
91110 if (count == 0 ) {
92111 quirc_destroy (qr );
@@ -96,15 +115,18 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
96115
97116 // Extract and decode the first QR code
98117 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting first QR code\n" );
118+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
99119 fflush (stdout );
100120 struct quirc_code code ;
101121 quirc_extract (qr , 0 , & code );
102122 QRDECODE_DEBUG_PRINT ("qrdecode: QR code extracted\n" );
123+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
103124 fflush (stdout );
104125 // it works until here!
105126/*
106127 // Decode the QR code - this is the part that fails:
107128 QRDECODE_DEBUG_PRINT("qrdecode: Decoding QR code\n");
129+ mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
108130 fflush(stdout);
109131 struct quirc_data data;
110132 int err = quirc_decode(&code, &data);
@@ -113,28 +135,36 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
113135 mp_raise_ValueError(MP_ERROR_TEXT("failed to decode QR code"));
114136 }
115137 QRDECODE_DEBUG_PRINT("qrdecode: QR code decoded, payload_len=%d\n", data.payload_len);
138+ mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
116139 fflush(stdout);
117140
118141 QRDECODE_DEBUG_PRINT("qrdecode: got result: %s\n", data.payload);
142+ mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
119143 fflush(stdout);
120144 QRDECODE_DEBUG_PRINT("ok so now what?!");
145+ mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
121146
122147 // Convert decoded data to Python string
123148 QRDECODE_DEBUG_PRINT("qrdecode: Creating Python string\n");
149+ mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
124150 fflush(stdout);
125151 mp_obj_t result = mp_obj_new_str((const char *)data.payload, data.payload_len);
126152 QRDECODE_DEBUG_PRINT("qrdecode: Python string created\n");
153+ mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
127154 fflush(stdout);
128155
129156 // Clean up
130157 QRDECODE_DEBUG_PRINT("qrdecode: Cleaning up\n");
158+ mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
131159 fflush(stdout);
132160 quirc_destroy(qr);
133161 QRDECODE_DEBUG_PRINT("qrdecode: quirc destroyed\n");
162+ mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
134163 fflush(stdout);
135164 */
136165
137166 QRDECODE_DEBUG_PRINT ("qrdecode: Returning result\n" );
167+ mp_printf (& mp_plat_print , "qrdecode: Stack usage: %u bytes\n" , micropython_stack_use ());
138168 //return result;
139169 return mp_const_none ; // MicroPython functions typically return None
140170}
0 commit comments