11#include <stdio.h>
2- #include <stdlib.h> // Added for malloc and free
2+ #include <stdlib.h> // For malloc and free
33#include "py/obj.h"
44#include "py/runtime.h"
55#include "py/mperrno.h"
66#include <string.h>
77#include "../quirc/lib/quirc.h"
8- #include "py/mpstate.h" // For micropython_stack_use()
8+ #include "freertos/FreeRTOS.h" // For uxTaskGetStackHighWaterMark
9+ #include "freertos/task.h" // For task-related functions
910
1011#define QRDECODE_DEBUG_PRINT (...) mp_printf(&mp_plat_print, __VA_ARGS__);
1112
1213// Function to decode a QR code from a grayscale image buffer
1314static mp_obj_t qrdecode (mp_uint_t n_args , const mp_obj_t * args ) {
1415 QRDECODE_DEBUG_PRINT ("qrdecode: Starting\n" );
15- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
16+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
1617
1718 // Check argument count (expecting buffer, width, height)
1819 QRDECODE_DEBUG_PRINT ("qrdecode: Checking argument count\n" );
19- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
20+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
2021 fflush (stdout );
2122 if (n_args != 3 ) {
2223 mp_raise_ValueError (MP_ERROR_TEXT ("quirc_decode expects 3 arguments: buffer, width, height" ));
2324 }
2425
2526 // Extract buffer
2627 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting buffer\n" );
27- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
28+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
2829 fflush (stdout );
2930 mp_buffer_info_t bufinfo ;
3031 mp_get_buffer_raise (args [0 ], & bufinfo , MP_BUFFER_READ );
3132 printf ("qrdecode: Buffer extracted, len=%zu\n" , bufinfo .len );
3233
3334 // Extract width and height
3435 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting width and height\n" );
35- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
36+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
3637 fflush (stdout );
3738 mp_int_t width = mp_obj_get_int (args [1 ]);
3839 mp_int_t height = mp_obj_get_int (args [2 ]);
3940 QRDECODE_DEBUG_PRINT ("qrdecode: Width=%ld, Height=%ld\n" , width , height );
40- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
41+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
4142 fflush (stdout );
4243
4344 // Validate dimensions
4445 QRDECODE_DEBUG_PRINT ("qrdecode: Validating dimensions\n" );
45- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
46+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
4647 fflush (stdout );
4748 if (width <= 0 || height <= 0 ) {
4849 mp_raise_ValueError (MP_ERROR_TEXT ("width and height must be positive" ));
@@ -51,62 +52,62 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
5152 mp_raise_ValueError (MP_ERROR_TEXT ("buffer size must match width * height" ));
5253 }
5354 QRDECODE_DEBUG_PRINT ("qrdecode: Dimensions validated\n" );
54- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
55+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
5556 fflush (stdout );
5657
5758 // Initialize quirc
5859 QRDECODE_DEBUG_PRINT ("qrdecode: Initializing quirc\n" );
59- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
60+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
6061 fflush (stdout );
6162 struct quirc * qr = quirc_new ();
6263 if (!qr ) {
6364 mp_raise_OSError (MP_ENOMEM );
6465 }
6566 QRDECODE_DEBUG_PRINT ("qrdecode: quirc initialized\n" );
66- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
67+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
6768 fflush (stdout );
6869
6970 // Resize quirc for the image dimensions
7071 QRDECODE_DEBUG_PRINT ("qrdecode: Resizing quirc\n" );
71- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
72+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
7273 fflush (stdout );
7374 if (quirc_resize (qr , width , height ) < 0 ) {
7475 quirc_destroy (qr );
7576 mp_raise_OSError (MP_ENOMEM );
7677 }
7778 QRDECODE_DEBUG_PRINT ("qrdecode: quirc resized\n" );
78- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
79+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
7980 fflush (stdout );
8081
8182 // Get quirc image buffer and copy grayscale data
8283 QRDECODE_DEBUG_PRINT ("qrdecode: Beginning quirc processing\n" );
83- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
84+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
8485 fflush (stdout );
8586 uint8_t * image ;
8687 quirc_begin (qr , NULL , NULL );
8788 image = quirc_begin (qr , NULL , NULL ); // Get pointer to quirc's image buffer
8889 QRDECODE_DEBUG_PRINT ("qrdecode: quirc image buffer obtained\n" );
89- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
90+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
9091 fflush (stdout );
9192 QRDECODE_DEBUG_PRINT ("qrdecode: Copying buffer, size=%ul\n" , (size_t )(width * height ));
92- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
93+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
9394 fflush (stdout );
9495 memcpy (image , bufinfo .buf , width * height );
9596 QRDECODE_DEBUG_PRINT ("qrdecode: Buffer copied\n" );
96- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
97+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
9798 fflush (stdout );
9899 quirc_end (qr );
99100 QRDECODE_DEBUG_PRINT ("qrdecode: quirc processing ended\n" );
100- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
101+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
101102 fflush (stdout );
102103
103104 // Check for QR codes
104105 QRDECODE_DEBUG_PRINT ("qrdecode: Counting QR codes\n" );
105- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
106+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
106107 fflush (stdout );
107108 int count = quirc_count (qr );
108109 QRDECODE_DEBUG_PRINT ("qrdecode: Found %d QR codes\n" , count );
109- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
110+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
110111 fflush (stdout );
111112 if (count == 0 ) {
112113 quirc_destroy (qr );
@@ -116,25 +117,25 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
116117
117118 // Extract and decode the first QR code
118119 QRDECODE_DEBUG_PRINT ("qrdecode: Extracting first QR code\n" );
119- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
120+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
120121 fflush (stdout );
121122 struct quirc_code * code = (struct quirc_code * )malloc (sizeof (struct quirc_code ));
122123 if (!code ) {
123124 quirc_destroy (qr );
124125 mp_raise_OSError (MP_ENOMEM );
125126 }
126127 QRDECODE_DEBUG_PRINT ("qrdecode: Allocated quirc_code on heap\n" );
127- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
128+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
128129 fflush (stdout );
129130 quirc_extract (qr , 0 , code );
130131 QRDECODE_DEBUG_PRINT ("qrdecode: QR code extracted\n" );
131- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
132+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
132133 fflush (stdout );
133134 // it works until here!
134135
135- // Decode the QR code - this is the part that fails (uncomment to test) :
136+ // Decode the QR code - this is the part that fails:
136137 QRDECODE_DEBUG_PRINT ("qrdecode: Decoding QR code\n" );
137- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
138+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
138139 fflush (stdout );
139140 struct quirc_data * data = (struct quirc_data * )malloc (sizeof (struct quirc_data ));
140141 if (!data ) {
@@ -143,7 +144,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
143144 mp_raise_OSError (MP_ENOMEM );
144145 }
145146 QRDECODE_DEBUG_PRINT ("qrdecode: Allocated quirc_data on heap\n" );
146- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
147+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
147148 fflush (stdout );
148149 int err = quirc_decode (code , data );
149150 if (err != QUIRC_SUCCESS ) {
@@ -153,39 +154,38 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
153154 mp_raise_ValueError (MP_ERROR_TEXT ("failed to decode QR code" ));
154155 }
155156 QRDECODE_DEBUG_PRINT ("qrdecode: QR code decoded, payload_len=%d\n" , data -> payload_len );
156- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
157+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
157158 fflush (stdout );
158159
159160 QRDECODE_DEBUG_PRINT ("qrdecode: got result: %s\n" , data -> payload );
160- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
161+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
161162 fflush (stdout );
162163 QRDECODE_DEBUG_PRINT ("ok so now what?!" );
163- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
164+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
164165
165166 // Convert decoded data to Python string
166167 QRDECODE_DEBUG_PRINT ("qrdecode: Creating Python string\n" );
167- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
168+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
168169 fflush (stdout );
169170 mp_obj_t result = mp_obj_new_str ((const char * )data -> payload , data -> payload_len );
170171 QRDECODE_DEBUG_PRINT ("qrdecode: Python string created\n" );
171- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
172+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
172173 fflush (stdout );
173174
174175 // Clean up
175176 QRDECODE_DEBUG_PRINT ("qrdecode: Cleaning up\n" );
176- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
177+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
177178 fflush (stdout );
178179 free (data );
179180 free (code );
180181 quirc_destroy (qr );
181182 QRDECODE_DEBUG_PRINT ("qrdecode: quirc destroyed\n" );
182- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
183+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
183184 fflush (stdout );
184185
185186 QRDECODE_DEBUG_PRINT ("qrdecode: Returning result\n" );
186- mp_printf (& mp_plat_print , "qrdecode: Stack usage : %u bytes\n" , micropython_stack_use ( ));
187+ mp_printf (& mp_plat_print , "qrdecode: Stack high-water mark : %u bytes\n" , uxTaskGetStackHighWaterMark ( NULL ));
187188 return result ;
188- //return mp_const_none; // MicroPython functions typically return None
189189}
190190
191191// Wrapper function to fix incompatible pointer type warning
0 commit comments