micropython_stack_use() doesn't work

This commit is contained in:
Thomas Farstrike
2025-05-12 22:39:41 +02:00
parent 1d53f8f10a
commit 455187aaae
+30
View File
@@ -4,15 +4,18 @@
#include "py/mperrno.h"
#include <string.h>
#include "../quirc/lib/quirc.h"
#include "py/mpstate.h" // Added for micropython_stack_use()
#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());
// 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());
fflush(stdout);
if (n_args != 3) {
mp_raise_ValueError(MP_ERROR_TEXT("quirc_decode expects 3 arguments: buffer, width, height"));
@@ -20,6 +23,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());
fflush(stdout);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
@@ -27,14 +31,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());
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());
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());
fflush(stdout);
if (width <= 0 || height <= 0) {
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) {
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
fflush(stdout);
if (count == 0) {
quirc_destroy(qr);
@@ -96,15 +115,18 @@ 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());
fflush(stdout);
struct quirc_code code;
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());
fflush(stdout);
// 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 usage: %u bytes\n", micropython_stack_use());
fflush(stdout);
struct quirc_data data;
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) {
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());
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());
fflush(stdout);
QRDECODE_DEBUG_PRINT("ok so now what?!");
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
// 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());
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());
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());
fflush(stdout);
quirc_destroy(qr);
QRDECODE_DEBUG_PRINT("qrdecode: quirc destroyed\n");
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
fflush(stdout);
*/
QRDECODE_DEBUG_PRINT("qrdecode: Returning result\n");
mp_printf(&mp_plat_print, "qrdecode: Stack usage: %u bytes\n", micropython_stack_use());
//return result;
return mp_const_none; // MicroPython functions typically return None
}