You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Debugging qrdecode
This commit is contained in:
+12
-10
@@ -12,10 +12,7 @@ static const char *TAG = "qrdecode";
|
||||
|
||||
// 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) {
|
||||
printf("qrdecode: Starting\n");
|
||||
ESP_LOGI(TAG, "qrdecode starting");
|
||||
QRDECODE_DEBUG_PRINT("mp_printf works in qrdecode!");
|
||||
fflush(stdout);
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: Starting\n");
|
||||
|
||||
// Check argument count (expecting buffer, width, height)
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: Checking argument count\n");
|
||||
@@ -62,7 +59,6 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
mp_raise_OSError(MP_ENOMEM);
|
||||
}
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: quirc initialized\n");
|
||||
QRDECODE_DEBUG_PRINT("mp_printf works in qrdecode!");
|
||||
fflush(stdout);
|
||||
|
||||
// Resize quirc for the image dimensions
|
||||
@@ -83,10 +79,10 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
image = quirc_begin(qr, NULL, NULL); // Get pointer to quirc's image buffer
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: quirc image buffer obtained\n");
|
||||
fflush(stdout);
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: Copying buffer, size=%zu\n", (size_t)(width * height));
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: Copying buffer, size=%ul\n", (size_t)(width * height));
|
||||
fflush(stdout);
|
||||
memcpy(image, bufinfo.buf, width * height);
|
||||
QRDECODE_DEBUG_PRINT
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: Buffer copied\n");
|
||||
fflush(stdout);
|
||||
quirc_end(qr);
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: quirc processing ended\n");
|
||||
@@ -102,6 +98,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
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");
|
||||
@@ -110,6 +107,7 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
quirc_extract(qr, 0, &code);
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: QR code extracted\n");
|
||||
fflush(stdout);
|
||||
// it works until here!
|
||||
|
||||
// Decode the QR code
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: Decoding QR code\n");
|
||||
@@ -122,7 +120,10 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
}
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: QR code decoded, payload_len=%d\n", data.payload_len);
|
||||
fflush(stdout);
|
||||
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: got result: %s\n", data.payload);
|
||||
fflush(stdout);
|
||||
QRDECODE_DEBUG_PRINT("ok so now what?!");
|
||||
/*
|
||||
// Convert decoded data to Python string
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: Creating Python string\n");
|
||||
fflush(stdout);
|
||||
@@ -136,10 +137,11 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
quirc_destroy(qr);
|
||||
QRDECODE_DEBUG_PRINT("qrdecode: quirc destroyed\n");
|
||||
fflush(stdout);
|
||||
*/
|
||||
|
||||
printf("qrdecode: Returning result\n");
|
||||
return result;
|
||||
//return mp_const_none; // MicroPython functions typically return None
|
||||
//return result;
|
||||
return mp_const_none; // MicroPython functions typically return None
|
||||
}
|
||||
|
||||
// Wrapper function to fix incompatible pointer type warning
|
||||
|
||||
@@ -13,13 +13,13 @@ buffer_size = width * height # 240 * 240 = 57600 bytes
|
||||
try:
|
||||
# Allocate buffer for grayscale image
|
||||
buffer = bytearray(buffer_size)
|
||||
for i in range(240 * 240):
|
||||
buffer[i] = random.getrandbits(8)
|
||||
#for i in range(240 * 240):
|
||||
# buffer[i] = random.getrandbits(8)
|
||||
# Read the raw grayscale image file
|
||||
#with open('qrcode2.raw', 'rb') as f:
|
||||
# bytes_read = f.readinto(buffer)
|
||||
# if bytes_read != buffer_size:
|
||||
# raise ValueError("File size does not match expected 240x240 grayscale image")
|
||||
with open('qrcode2.raw', 'rb') as f:
|
||||
bytes_read = f.readinto(buffer)
|
||||
if bytes_read != buffer_size:
|
||||
raise ValueError("File size does not match expected 240x240 grayscale image")
|
||||
# Decode QR code using qrdecode module
|
||||
print("decoding...")
|
||||
print(f"buffer length: {len(buffer)}")
|
||||
@@ -28,8 +28,8 @@ try:
|
||||
result = qrdecode.qrdecode(buffer, width, height)
|
||||
print(f"result: {result}")
|
||||
# Remove BOM (\ufeff) from the start of the decoded string, if present
|
||||
if result.startswith('\ufeff'):
|
||||
result = result[1:]
|
||||
#if result.startswith('\ufeff'):
|
||||
# result = result[1:]
|
||||
print(f"result: {result}")
|
||||
except OSError as e:
|
||||
print("Error reading file:", e)
|
||||
|
||||
Reference in New Issue
Block a user