Skip to content

Commit b6c79c8

Browse files
quirc_decode: finally debug prints work!
1 parent a81157b commit b6c79c8

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

c_mpos/src/quirc_decode.c

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@
66
#include <string.h>
77
#include "../quirc/lib/quirc.h"
88

9-
9+
#define QRDECODE_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__);
1010

1111
static const char *TAG = "qrdecode";
1212

1313
// Function to decode a QR code from a grayscale image buffer
1414
static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
1515
printf("qrdecode: Starting\n");
1616
ESP_LOGI(TAG, "qrdecode starting");
17+
QRDECODE_DEBUG_PRINT("mp_printf works in qrdecode!");
1718
fflush(stdout);
1819

1920
// Check argument count (expecting buffer, width, height)
20-
printf("qrdecode: Checking argument count\n");
21+
QRDECODE_DEBUG_PRINT("qrdecode: Checking argument count\n");
2122
ESP_LOGI(TAG, "Checking argument count");
2223
fflush(stdout);
2324
if (n_args != 3) {
2425
mp_raise_ValueError(MP_ERROR_TEXT("quirc_decode expects 3 arguments: buffer, width, height"));
2526
}
2627

2728
// Extract buffer
28-
printf("qrdecode: Extracting buffer\n");
29+
QRDECODE_DEBUG_PRINT("qrdecode: Extracting buffer\n");
2930
ESP_LOGI(TAG, "Extracting buffer");
3031
fflush(stdout);
3132
mp_buffer_info_t bufinfo;
@@ -34,110 +35,111 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
3435
ESP_LOGI(TAG, "Buffer extracted, len=%zu", bufinfo.len);
3536

3637
// Extract width and height
37-
printf("qrdecode: Extracting width and height\n");
38+
QRDECODE_DEBUG_PRINT("qrdecode: Extracting width and height\n");
3839
fflush(stdout);
3940
mp_int_t width = mp_obj_get_int(args[1]);
4041
mp_int_t height = mp_obj_get_int(args[2]);
41-
printf("qrdecode: Width=%ld, Height=%ld\n", width, height);
42+
QRDECODE_DEBUG_PRINT("qrdecode: Width=%ld, Height=%ld\n", width, height);
4243
fflush(stdout);
4344

4445
// Validate dimensions
45-
printf("qrdecode: Validating dimensions\n");
46+
QRDECODE_DEBUG_PRINT("qrdecode: Validating dimensions\n");
4647
fflush(stdout);
4748
if (width <= 0 || height <= 0) {
4849
mp_raise_ValueError(MP_ERROR_TEXT("width and height must be positive"));
4950
}
5051
if (bufinfo.len != (size_t)(width * height)) {
5152
mp_raise_ValueError(MP_ERROR_TEXT("buffer size must match width * height"));
5253
}
53-
printf("qrdecode: Dimensions validated\n");
54+
QRDECODE_DEBUG_PRINT("qrdecode: Dimensions validated\n");
5455
fflush(stdout);
5556

5657
// Initialize quirc
57-
printf("qrdecode: Initializing quirc\n");
58+
QRDECODE_DEBUG_PRINT("qrdecode: Initializing quirc\n");
5859
fflush(stdout);
5960
struct quirc *qr = quirc_new();
6061
if (!qr) {
6162
mp_raise_OSError(MP_ENOMEM);
6263
}
63-
printf("qrdecode: quirc initialized\n");
64+
QRDECODE_DEBUG_PRINT("qrdecode: quirc initialized\n");
65+
QRDECODE_DEBUG_PRINT("mp_printf works in qrdecode!");
6466
fflush(stdout);
65-
/*
67+
6668
// Resize quirc for the image dimensions
67-
printf("qrdecode: Resizing quirc\n");
69+
QRDECODE_DEBUG_PRINT("qrdecode: Resizing quirc\n");
6870
fflush(stdout);
6971
if (quirc_resize(qr, width, height) < 0) {
7072
quirc_destroy(qr);
7173
mp_raise_OSError(MP_ENOMEM);
7274
}
73-
printf("qrdecode: quirc resized\n");
75+
QRDECODE_DEBUG_PRINT("qrdecode: quirc resized\n");
7476
fflush(stdout);
7577

7678
// Get quirc image buffer and copy grayscale data
77-
printf("qrdecode: Beginning quirc processing\n");
79+
QRDECODE_DEBUG_PRINT
7880
fflush(stdout);
7981
uint8_t *image;
8082
quirc_begin(qr, NULL, NULL);
8183
image = quirc_begin(qr, NULL, NULL); // Get pointer to quirc's image buffer
82-
printf("qrdecode: quirc image buffer obtained\n");
84+
QRDECODE_DEBUG_PRINT("qrdecode: quirc image buffer obtained\n");
8385
fflush(stdout);
84-
printf("qrdecode: Copying buffer, size=%zu\n", (size_t)(width * height));
86+
QRDECODE_DEBUG_PRINT("qrdecode: Copying buffer, size=%zu\n", (size_t)(width * height));
8587
fflush(stdout);
8688
memcpy(image, bufinfo.buf, width * height);
87-
printf("qrdecode: Buffer copied\n");
89+
QRDECODE_DEBUG_PRINT
8890
fflush(stdout);
8991
quirc_end(qr);
90-
printf("qrdecode: quirc processing ended\n");
92+
QRDECODE_DEBUG_PRINT("qrdecode: quirc processing ended\n");
9193
fflush(stdout);
9294

9395
// Check for QR codes
94-
printf("qrdecode: Counting QR codes\n");
96+
QRDECODE_DEBUG_PRINT("qrdecode: Counting QR codes\n");
9597
fflush(stdout);
9698
int count = quirc_count(qr);
97-
printf("qrdecode: Found %d QR codes\n", count);
99+
QRDECODE_DEBUG_PRINT("qrdecode: Found %d QR codes\n", count);
98100
fflush(stdout);
99101
if (count == 0) {
100102
quirc_destroy(qr);
101103
mp_raise_ValueError(MP_ERROR_TEXT("no QR code found"));
102104
}
103105

104106
// Extract and decode the first QR code
105-
printf("qrdecode: Extracting first QR code\n");
107+
QRDECODE_DEBUG_PRINT("qrdecode: Extracting first QR code\n");
106108
fflush(stdout);
107109
struct quirc_code code;
108110
quirc_extract(qr, 0, &code);
109-
printf("qrdecode: QR code extracted\n");
111+
QRDECODE_DEBUG_PRINT("qrdecode: QR code extracted\n");
110112
fflush(stdout);
111113

112114
// Decode the QR code
113-
printf("qrdecode: Decoding QR code\n");
115+
QRDECODE_DEBUG_PRINT("qrdecode: Decoding QR code\n");
114116
fflush(stdout);
115117
struct quirc_data data;
116118
int err = quirc_decode(&code, &data);
117119
if (err != QUIRC_SUCCESS) {
118120
quirc_destroy(qr);
119121
mp_raise_ValueError(MP_ERROR_TEXT("failed to decode QR code"));
120122
}
121-
printf("qrdecode: QR code decoded, payload_len=%d\n", data.payload_len);
123+
QRDECODE_DEBUG_PRINT("qrdecode: QR code decoded, payload_len=%d\n", data.payload_len);
122124
fflush(stdout);
123125

124126
// Convert decoded data to Python string
125-
printf("qrdecode: Creating Python string\n");
127+
QRDECODE_DEBUG_PRINT("qrdecode: Creating Python string\n");
126128
fflush(stdout);
127129
mp_obj_t result = mp_obj_new_str((const char *)data.payload, data.payload_len);
128130
printf("qrdecode: Python string created\n");
129131
fflush(stdout);
130132

131133
// Clean up
132-
printf("qrdecode: Cleaning up\n");
134+
QRDECODE_DEBUG_PRINT("qrdecode: Cleaning up\n");
133135
fflush(stdout);
134136
quirc_destroy(qr);
135-
printf("qrdecode: quirc destroyed\n");
137+
QRDECODE_DEBUG_PRINT("qrdecode: quirc destroyed\n");
136138
fflush(stdout);
137-
*/
139+
138140
printf("qrdecode: Returning result\n");
139-
//return result;
140-
return mp_const_none; // MicroPython functions typically return None
141+
return result;
142+
//return mp_const_none; // MicroPython functions typically return None
141143
}
142144

143145
// Wrapper function to fix incompatible pointer type warning

draft_code/qrdecode.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import qrdecode
2+
import random
23

4+
# Create a 240x240 byte buffer
5+
buffer = bytearray(240 * 240)
6+
7+
# Fill buffer with random bytes
38

49
# Image dimensions
510
width = 240
@@ -8,11 +13,13 @@
813
try:
914
# Allocate buffer for grayscale image
1015
buffer = bytearray(buffer_size)
16+
for i in range(240 * 240):
17+
buffer[i] = random.getrandbits(8)
1118
# Read the raw grayscale image file
12-
with open('qrcode2.raw', 'rb') as f:
13-
bytes_read = f.readinto(buffer)
14-
if bytes_read != buffer_size:
15-
raise ValueError("File size does not match expected 240x240 grayscale image")
19+
#with open('qrcode2.raw', 'rb') as f:
20+
# bytes_read = f.readinto(buffer)
21+
# if bytes_read != buffer_size:
22+
# raise ValueError("File size does not match expected 240x240 grayscale image")
1623
# Decode QR code using qrdecode module
1724
print("decoding...")
1825
print(f"buffer length: {len(buffer)}")

0 commit comments

Comments
 (0)