Skip to content

Commit bd903f2

Browse files
committed
Add qrio: Decode QR codes with quirc lib
1 parent 38f392f commit bd903f2

File tree

14 files changed

+561
-11
lines changed

14 files changed

+561
-11
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,6 @@
188188
[submodule "frozen/Adafruit_CircuitPython_SimpleIO"]
189189
path = frozen/Adafruit_CircuitPython_SimpleIO
190190
url = https://github.com/adafruit/adafruit_circuitpython_simpleio
191+
[submodule "lib/quirc"]
192+
path = lib/quirc
193+
url = https://github.com/adafruit/quirc.git

lib/quirc

Submodule quirc added at 7f4001e

locale/circuitpython.pot

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,11 +1185,6 @@ msgstr ""
11851185
msgid "Input/output error"
11861186
msgstr ""
11871187

1188-
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
1189-
#, c-format
1190-
msgid "Missing jmp_pin. Instruction %d jumps on pin"
1191-
msgstr ""
1192-
11931188
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
11941189
#, c-format
11951190
msgid "Instruction %d shifts in more bits than pin count"
@@ -1506,6 +1501,11 @@ msgstr ""
15061501
msgid "Missing first_set_pin. Instruction %d sets pin(s)"
15071502
msgstr ""
15081503

1504+
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
1505+
#, c-format
1506+
msgid "Missing jmp_pin. Instruction %d jumps on pin"
1507+
msgstr ""
1508+
15091509
#: shared-bindings/busio/UART.c shared-bindings/displayio/Group.c
15101510
msgid "Must be a %q subclass."
15111511
msgstr ""
@@ -2063,7 +2063,6 @@ msgid "Size not supported"
20632063
msgstr ""
20642064

20652065
#: ports/raspberrypi/common-hal/alarm/SleepMemory.c
2066-
#: ports/stm/common-hal/alarm/SleepMemory.c
20672066
msgid "Sleep Memory not available"
20682067
msgstr ""
20692068

@@ -2510,7 +2509,7 @@ msgid "argument name reused"
25102509
msgstr ""
25112510

25122511
#: py/argcheck.c shared-bindings/_stage/__init__.c
2513-
#: shared-bindings/digitalio/DigitalInOut.c shared-bindings/gamepad/GamePad.c
2512+
#: shared-bindings/digitalio/DigitalInOut.c
25142513
msgid "argument num/types mismatch"
25152514
msgstr ""
25162515

@@ -3594,10 +3593,6 @@ msgstr ""
35943593
msgid "no active exception to reraise"
35953594
msgstr ""
35963595

3597-
#: shared-bindings/socket/__init__.c shared-module/network/__init__.c
3598-
msgid "no available NIC"
3599-
msgstr ""
3600-
36013596
#: py/compile.c
36023597
msgid "no binding for nonlocal found"
36033598
msgstr ""

py/circuitpy_defns.mk

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ endif
224224
ifeq ($(CIRCUITPY_PIXELBUF),1)
225225
SRC_PATTERNS += adafruit_pixelbuf/%
226226
endif
227+
ifeq ($(CIRCUITPY_QRIO),1)
228+
SRC_PATTERNS += qrio/%
229+
endif
227230
ifeq ($(CIRCUITPY_RAINBOWIO),1)
228231
SRC_PATTERNS += rainbowio/%
229232
endif
@@ -529,6 +532,8 @@ SRC_SHARED_MODULE_ALL = \
529532
network/__init__.c \
530533
msgpack/__init__.c \
531534
os/__init__.c \
535+
qrio/__init__.c \
536+
qrio/QRDecoder.c \
532537
rainbowio/__init__.c \
533538
random/__init__.c \
534539
rgbmatrix/RGBMatrix.c \
@@ -664,6 +669,12 @@ SRC_CIRCUITPY_COMMON = \
664669
lib/utils/stdout_helpers.c \
665670
lib/utils/sys_stdio_mphal.c
666671

672+
ifeq ($(CIRCUITPY_QRIO),1)
673+
SRC_CIRCUITPY_COMMON += lib/quirc/lib/decode.c lib/quirc/lib/identify.c lib/quirc/lib/quirc.c lib/quirc/lib/version_db.c
674+
$(BUILD)/lib/quirc/lib/%.o: CFLAGS += -Wno-shadow -Wno-sign-compare -include shared-module/qrio/quirc_alloc.h
675+
676+
endif
677+
667678
ifdef LD_TEMPLATE_FILE
668679
# Generate a linker script (.ld file) from a template, for those builds that use it.
669680
GENERATED_LD_FILE = $(BUILD)/$(notdir $(patsubst %.template.ld,%.ld,$(LD_TEMPLATE_FILE)))

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,13 @@ extern const struct _mp_obj_module_t pwmio_module;
621621
#define PWMIO_MODULE
622622
#endif
623623

624+
#if CIRCUITPY_QRIO
625+
extern const struct _mp_obj_module_t qrio_module;
626+
#define QRIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_qrio), (mp_obj_t)&qrio_module },
627+
#else
628+
#define QRIO_MODULE
629+
#endif
630+
624631
#if CIRCUITPY_RAINBOWIO
625632
extern const struct _mp_obj_module_t rainbowio_module;
626633
#define RAINBOWIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_rainbowio), (mp_obj_t)&rainbowio_module },
@@ -898,6 +905,7 @@ extern const struct _mp_obj_module_t msgpack_module;
898905
PS2IO_MODULE \
899906
PULSEIO_MODULE \
900907
PWMIO_MODULE \
908+
QRIO_MODULE \
901909
RAINBOWIO_MODULE \
902910
RANDOM_MODULE \
903911
RE_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ CFLAGS += -DCIRCUITPY_PULSEIO=$(CIRCUITPY_PULSEIO)
239239
CIRCUITPY_PWMIO ?= 1
240240
CFLAGS += -DCIRCUITPY_PWMIO=$(CIRCUITPY_PWMIO)
241241

242+
CIRCUITPY_QRIO ?= $(CIRCUITPY_IMAGECAPTURE)
243+
CFLAGS += -DCIRCUITPY_QRIO=$(CIRCUITPY_QRIO)
244+
242245
CIRCUITPY_RAINBOWIO ?= 1
243246
CFLAGS += -DCIRCUITPY_RAINBOWIO=$(CIRCUITPY_RAINBOWIO)
244247

shared-bindings/qrio/QRDecoder.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Jeff Epler
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/qrio/__init__.h"
28+
#include "shared-bindings/qrio/QRDecoder.h"
29+
#include "shared-module/qrio/QRDecoder.h"
30+
31+
#include "py/obj.h"
32+
#include "py/objproperty.h"
33+
#include "py/enum.h"
34+
35+
//| class QRDecoder:
36+
//|
37+
//| def __init__(self, width: int, height: int) -> None:
38+
//| """Construct a QRDecoder object"""
39+
//|
40+
41+
STATIC mp_obj_t qrio_qrdecoder_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
42+
enum { ARG_width, ARG_height };
43+
static const mp_arg_t allowed_args[] = {
44+
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} },
45+
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} },
46+
};
47+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
48+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
49+
50+
qrio_qrdecoder_obj_t *self = m_new_obj(qrio_qrdecoder_obj_t);
51+
self->base.type = &qrio_qrdecoder_type_obj;
52+
shared_module_qrio_qrdecoder_construct(self, args[ARG_width].u_int, args[ARG_height].u_int);
53+
54+
return self;
55+
}
56+
57+
//| def decode(self, buffer: ReadableBuffer) -> List[qrinfo]:
58+
//| """Decode zero or more QR codes from the given image in L8 format"""
59+
//|
60+
STATIC mp_obj_t qrio_qrdecoder_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
61+
qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
62+
63+
enum { ARG_buffer, ARG_pixel_policy };
64+
static const mp_arg_t allowed_args[] = {
65+
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_int = 0} },
66+
{ MP_QSTR_pixel_policy, MP_ARG_OBJ, {.u_int = 0} },
67+
};
68+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
69+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
70+
71+
mp_buffer_info_t bufinfo;
72+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
73+
74+
int width = shared_module_qrio_qrdecoder_get_width(self);
75+
int height = shared_module_qrio_qrdecoder_get_height(self);
76+
77+
// verify that the buffer is big enough
78+
int sz = width * height;
79+
qrio_pixel_policy_t policy = cp_enum_value(&qrio_pixel_policy_type, args[ARG_pixel_policy].u_obj);
80+
if (policy != QRIO_EVERY_BYTE) {
81+
sz *= 2;
82+
}
83+
mp_get_index(mp_obj_get_type(args[ARG_buffer].u_obj), bufinfo.len, MP_OBJ_NEW_SMALL_INT(sz - 1), false);
84+
85+
return shared_module_qrio_qrdecoder_decode(self, &bufinfo, policy);
86+
}
87+
MP_DEFINE_CONST_FUN_OBJ_KW(qrio_qrdecoder_decode_obj, 2, qrio_qrdecoder_decode);
88+
89+
//| width: int
90+
//| """The width of image the decoder expects"""
91+
//|
92+
STATIC mp_obj_t qrio_qrdecoder_get_width(mp_obj_t self_in) {
93+
qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in);
94+
return mp_obj_new_int(shared_module_qrio_qrdecoder_get_width(self));
95+
}
96+
MP_DEFINE_CONST_FUN_OBJ_1(qrio_qrdecoder_get_width_obj, qrio_qrdecoder_get_width);
97+
98+
STATIC mp_obj_t qrio_qrdecoder_set_width(mp_obj_t self_in, mp_obj_t width_in) {
99+
qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in);
100+
int width = mp_obj_get_int(width_in);
101+
shared_module_qrio_qrdecoder_set_width(self, width);
102+
return mp_const_none;
103+
}
104+
MP_DEFINE_CONST_FUN_OBJ_2(qrio_qrdecoder_set_width_obj, qrio_qrdecoder_set_width);
105+
106+
const mp_obj_property_t qrio_qrdecoder_width_obj = {
107+
.base.type = &mp_type_property,
108+
.proxy = {(mp_obj_t)&qrio_qrdecoder_get_width_obj,
109+
(mp_obj_t)&qrio_qrdecoder_set_width_obj,
110+
MP_ROM_NONE},
111+
};
112+
113+
//| height: int
114+
//| """The height of image the decoder expects"""
115+
//|
116+
STATIC mp_obj_t qrio_qrdecoder_get_height(mp_obj_t self_in) {
117+
qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in);
118+
return mp_obj_new_int(shared_module_qrio_qrdecoder_get_height(self));
119+
}
120+
MP_DEFINE_CONST_FUN_OBJ_1(qrio_qrdecoder_get_height_obj, qrio_qrdecoder_get_height);
121+
122+
STATIC mp_obj_t qrio_qrdecoder_set_height(mp_obj_t self_in, mp_obj_t height_in) {
123+
qrio_qrdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in);
124+
int height = mp_obj_get_int(height_in);
125+
shared_module_qrio_qrdecoder_set_height(self, height);
126+
return mp_const_none;
127+
}
128+
MP_DEFINE_CONST_FUN_OBJ_2(qrio_qrdecoder_set_height_obj, qrio_qrdecoder_set_height);
129+
130+
const mp_obj_property_t qrio_qrdecoder_height_obj = {
131+
.base.type = &mp_type_property,
132+
.proxy = {(mp_obj_t)&qrio_qrdecoder_get_height_obj,
133+
(mp_obj_t)&qrio_qrdecoder_set_height_obj,
134+
MP_ROM_NONE},
135+
};
136+
137+
STATIC const mp_rom_map_elem_t qrio_qrdecoder_locals_table[] = {
138+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_QRDecoder) },
139+
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&qrio_qrdecoder_width_obj) },
140+
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&qrio_qrdecoder_height_obj) },
141+
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&qrio_qrdecoder_decode_obj) },
142+
};
143+
144+
STATIC MP_DEFINE_CONST_DICT(qrio_qrdecoder_locals, qrio_qrdecoder_locals_table);
145+
146+
const mp_obj_type_t qrio_qrdecoder_type_obj = {
147+
{ &mp_type_type },
148+
.name = MP_QSTR_QRDecoder,
149+
.make_new = qrio_qrdecoder_make_new,
150+
.locals_dict = (mp_obj_dict_t *)&qrio_qrdecoder_locals,
151+
};

shared-bindings/qrio/QRDecoder.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#pragma once
28+
29+
#include "py/obj.h"
30+
31+
typedef struct qrio_qrdecoder_obj qrio_qrdecoder_obj_t;
32+
33+
extern const mp_obj_type_t qrio_qrdecoder_type_obj;
34+
35+
void common_hal_qrio_qrdecoder_construct(qrio_qrdecoder_obj_t *self);
36+
37+
mp_obj_t common_hal_qrio_qrdecoder_recognize(qrio_qrdecoder_obj_t *self, int width, int height, mp_buffer_info_t *buf);

0 commit comments

Comments
 (0)