diff --git a/c_mpos/src/quirc_decode.c b/c_mpos/src/quirc_decode.c index ab3fd1bb..36716d7a 100644 --- a/c_mpos/src/quirc_decode.c +++ b/c_mpos/src/quirc_decode.c @@ -1,50 +1,42 @@ #include #include // For malloc and free +#include + #include "py/obj.h" #include "py/runtime.h" #include "py/mperrno.h" -#include -#include "../quirc/lib/quirc.h" + #include "freertos/FreeRTOS.h" // For uxTaskGetStackHighWaterMark #include "freertos/task.h" // For task-related functions +#include "../quirc/lib/quirc.h" + #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 high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); + QRDECODE_DEBUG_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 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")); } // Extract buffer QRDECODE_DEBUG_PRINT("qrdecode: Extracting buffer\n"); - 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); - printf("qrdecode: Buffer extracted, len=%zu\n", bufinfo.len); // Extract width and height QRDECODE_DEBUG_PRINT("qrdecode: Extracting width and height\n"); - 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 high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); + QRDECODE_DEBUG_PRINT("qrdecode: Width=%u, Height=%u\n", width, height); // Validate dimensions QRDECODE_DEBUG_PRINT("qrdecode: Validating dimensions\n"); - 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")); } @@ -52,91 +44,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 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 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 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 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 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 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 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 high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); + QRDECODE_DEBUG_PRINT("qrdecode: Copying buffer, size=%u\n", (size_t)(width * height)); memcpy(image, bufinfo.buf, width * height); QRDECODE_DEBUG_PRINT("qrdecode: Buffer copied\n"); - 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 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 high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); + QRDECODE_DEBUG_PRINT("qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); int count = quirc_count(qr); QRDECODE_DEBUG_PRINT("qrdecode: Found %d QR codes\n", count); - mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); + QRDECODE_DEBUG_PRINT("qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); if (count == 0) { quirc_destroy(qr); mp_raise_ValueError(MP_ERROR_TEXT("no QR code found")); } - // it works until here, it finds 1 QR code! // Extract and decode the first QR code QRDECODE_DEBUG_PRINT("qrdecode: Extracting first QR code\n"); - 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) { quirc_destroy(qr); mp_raise_OSError(MP_ENOMEM); } QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_code on heap\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); + QRDECODE_DEBUG_PRINT("qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); quirc_extract(qr, 0, code); QRDECODE_DEBUG_PRINT("qrdecode: QR code extracted\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); + QRDECODE_DEBUG_PRINT("qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); // it works until here! // 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 high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); struct quirc_data *data = (struct quirc_data *)malloc(sizeof(struct quirc_data)); if (!data) { free(code); @@ -144,8 +107,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 high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); + QRDECODE_DEBUG_PRINT("qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); int err = quirc_decode(code, data); if (err != QUIRC_SUCCESS) { free(data); @@ -154,43 +116,25 @@ 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 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 high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); - QRDECODE_DEBUG_PRINT("ok so now what?!"); - mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); + QRDECODE_DEBUG_PRINT("qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); + //QRDECODE_DEBUG_PRINT("qrdecode: got result: %s\n", data->payload); // Convert decoded data to Python string QRDECODE_DEBUG_PRINT("qrdecode: Creating Python string\n"); - 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 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 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 high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); - fflush(stdout); - - QRDECODE_DEBUG_PRINT("qrdecode: Returning result\n"); - mp_printf(&mp_plat_print, "qrdecode: Stack high-water mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL)); + QRDECODE_DEBUG_PRINT("qrdecode: quirc destroyed, returning result\n"); return result; } // Wrapper function to fix incompatible pointer type warning static mp_obj_t qrdecode_wrapper(size_t n_args, const mp_obj_t *args) { - printf("qrdecode_wrapper: Called with %zu args\n", n_args); return qrdecode(n_args, args); } diff --git a/internal_filesystem/apps/com.example.camtest/assets/camtest.py b/internal_filesystem/apps/com.example.camtest/assets/camtest.py index 41834187..dd6c6588 100644 --- a/internal_filesystem/apps/com.example.camtest/assets/camtest.py +++ b/internal_filesystem/apps/com.example.camtest/assets/camtest.py @@ -56,7 +56,7 @@ def process_qr_buffer(buffer): # Check if the string is printable (ASCII printable characters) if all(32 <= ord(c) <= 126 for c in result): return result - except UnicodeDecodeError: + except Exception as e: pass # If not a valid string or not printable, convert to hex hex_str = ' '.join([f'{b:02x}' for b in buffer])