From c4fcbc8ee00c738d3cef953c41af2fac9f258968 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Mon, 12 May 2025 22:46:18 +0200 Subject: [PATCH] it works! --- c_mpos/src/quirc_decode.c | 70 +++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/c_mpos/src/quirc_decode.c b/c_mpos/src/quirc_decode.c index e9ce6f5f..ab3fd1bb 100644 --- a/c_mpos/src/quirc_decode.c +++ b/c_mpos/src/quirc_decode.c @@ -1,22 +1,23 @@ #include -#include // Added for malloc and free +#include // For malloc and free #include "py/obj.h" #include "py/runtime.h" #include "py/mperrno.h" #include #include "../quirc/lib/quirc.h" -#include "py/mpstate.h" // For micropython_stack_use() +#include "freertos/FreeRTOS.h" // For uxTaskGetStackHighWaterMark +#include "freertos/task.h" // For task-related functions #define QRDECODE_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__); // Function to decode a QR code from a grayscale image buffer static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) { QRDECODE_DEBUG_PRINT("qrdecode: Starting\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); // Check argument count (expecting buffer, width, height) QRDECODE_DEBUG_PRINT("qrdecode: Checking argument count\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); if (n_args != 3) { mp_raise_ValueError(MP_ERROR_TEXT("quirc_decode expects 3 arguments: buffer, width, height")); @@ -24,7 +25,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) { // Extract buffer QRDECODE_DEBUG_PRINT("qrdecode: Extracting buffer\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); @@ -32,17 +33,17 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) { // Extract width and height QRDECODE_DEBUG_PRINT("qrdecode: Extracting width and height\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); mp_int_t width = mp_obj_get_int(args[1]); mp_int_t height = mp_obj_get_int(args[2]); QRDECODE_DEBUG_PRINT("qrdecode: Width=%ld, Height=%ld\n", width, height); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); // Validate dimensions QRDECODE_DEBUG_PRINT("qrdecode: Validating dimensions\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); if (width <= 0 || height <= 0) { 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) { mp_raise_ValueError(MP_ERROR_TEXT("buffer size must match width * height")); } QRDECODE_DEBUG_PRINT("qrdecode: Dimensions validated\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); // Initialize quirc QRDECODE_DEBUG_PRINT("qrdecode: Initializing quirc\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); struct quirc *qr = quirc_new(); if (!qr) { mp_raise_OSError(MP_ENOMEM); } QRDECODE_DEBUG_PRINT("qrdecode: quirc initialized\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); // Resize quirc for the image dimensions QRDECODE_DEBUG_PRINT("qrdecode: Resizing quirc\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); if (quirc_resize(qr, width, height) < 0) { quirc_destroy(qr); mp_raise_OSError(MP_ENOMEM); } QRDECODE_DEBUG_PRINT("qrdecode: quirc resized\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); // Get quirc image buffer and copy grayscale data QRDECODE_DEBUG_PRINT("qrdecode: Beginning quirc processing\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); uint8_t *image; quirc_begin(qr, NULL, NULL); image = quirc_begin(qr, NULL, NULL); // Get pointer to quirc's image buffer QRDECODE_DEBUG_PRINT("qrdecode: quirc image buffer obtained\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); QRDECODE_DEBUG_PRINT("qrdecode: Copying buffer, size=%ul\n", (size_t)(width * height)); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); memcpy(image, bufinfo.buf, width * height); QRDECODE_DEBUG_PRINT("qrdecode: Buffer copied\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); quirc_end(qr); QRDECODE_DEBUG_PRINT("qrdecode: quirc processing ended\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); // Check for QR codes QRDECODE_DEBUG_PRINT("qrdecode: Counting QR codes\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); int count = quirc_count(qr); QRDECODE_DEBUG_PRINT("qrdecode: Found %d QR codes\n", count); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); if (count == 0) { quirc_destroy(qr); @@ -116,7 +117,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) { // Extract and decode the first QR code QRDECODE_DEBUG_PRINT("qrdecode: Extracting first QR code\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); struct quirc_code *code = (struct quirc_code *)malloc(sizeof(struct quirc_code)); if (!code) { @@ -124,17 +125,17 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) { mp_raise_OSError(MP_ENOMEM); } QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_code on heap\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); quirc_extract(qr, 0, code); QRDECODE_DEBUG_PRINT("qrdecode: QR code extracted\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); // it works until here! - // Decode the QR code - this is the part that fails (uncomment to test): + // Decode the QR code - this is the part that fails: QRDECODE_DEBUG_PRINT("qrdecode: Decoding QR code\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); struct quirc_data *data = (struct quirc_data *)malloc(sizeof(struct quirc_data)); if (!data) { @@ -143,7 +144,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) { mp_raise_OSError(MP_ENOMEM); } QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_data on heap\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); int err = quirc_decode(code, data); if (err != QUIRC_SUCCESS) { @@ -153,39 +154,38 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) { mp_raise_ValueError(MP_ERROR_TEXT("failed to decode QR code")); } QRDECODE_DEBUG_PRINT("qrdecode: QR code decoded, payload_len=%d\n", data->payload_len); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); QRDECODE_DEBUG_PRINT("qrdecode: got result: %s\n", data->payload); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); QRDECODE_DEBUG_PRINT("ok so now what?!"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); // Convert decoded data to Python string QRDECODE_DEBUG_PRINT("qrdecode: Creating Python string\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); mp_obj_t result = mp_obj_new_str((const char *)data->payload, data->payload_len); QRDECODE_DEBUG_PRINT("qrdecode: Python string created\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); // Clean up QRDECODE_DEBUG_PRINT("qrdecode: Cleaning up\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); free(data); free(code); quirc_destroy(qr); QRDECODE_DEBUG_PRINT("qrdecode: quirc destroyed\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); fflush(stdout); QRDECODE_DEBUG_PRINT("qrdecode: Returning result\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use()); + mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); return result; - //return mp_const_none; // MicroPython functions typically return None } // Wrapper function to fix incompatible pointer type warning