Skip to content

Commit 7d23206

Browse files
committed
Inital keypad work: Keys working: one pin per key
keypad.Buttons and keypad.State Buttons -> Keys; further work wip wip wip: compiles about to try keypad.Keys working
1 parent c1c101c commit 7d23206

File tree

15 files changed

+664
-16
lines changed

15 files changed

+664
-16
lines changed

locale/circuitpython.pot

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ msgid "%q must be a tuple of length 2"
116116
msgstr ""
117117

118118
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
119-
#: shared-bindings/canio/Match.c
119+
#: shared-bindings/canio/Match.c shared-bindings/keypad/Keys.c
120120
msgid "%q out of range"
121121
msgstr ""
122122

@@ -905,7 +905,8 @@ msgstr ""
905905

906906
#: ports/raspberrypi/bindings/rp2pio/StateMachine.c py/enum.c
907907
#: shared-bindings/_bleio/__init__.c shared-bindings/aesio/aes.c
908-
#: shared-bindings/busio/SPI.c shared-bindings/microcontroller/Pin.c
908+
#: shared-bindings/busio/SPI.c shared-bindings/keypad/Keys.c
909+
#: shared-bindings/microcontroller/Pin.c
909910
#: shared-bindings/neopixel_write/__init__.c
910911
#: shared-bindings/terminalio/Terminal.c
911912
msgid "Expected a %q"

py/circuitpy_defns.mk

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ endif
178178
ifeq ($(CIRCUITPY_FRAMEBUFFERIO),1)
179179
SRC_PATTERNS += framebufferio/%
180180
endif
181+
ifeq ($(CIRCUITPY__EVE),1)
182+
SRC_PATTERNS += _eve/%
183+
endif
181184
ifeq ($(CIRCUITPY_FREQUENCYIO),1)
182185
SRC_PATTERNS += frequencyio/%
183186
endif
@@ -196,12 +199,12 @@ endif
196199
ifeq ($(CIRCUITPY_IPADDRESS),1)
197200
SRC_PATTERNS += ipaddress/%
198201
endif
202+
ifeq ($(CIRCUITPY_KEYPAD),1)
203+
SRC_PATTERNS += keypad/%
204+
endif
199205
ifeq ($(CIRCUITPY_MATH),1)
200206
SRC_PATTERNS += math/%
201207
endif
202-
ifeq ($(CIRCUITPY__EVE),1)
203-
SRC_PATTERNS += _eve/%
204-
endif
205208
ifeq ($(CIRCUITPY_MEMORYMONITOR),1)
206209
SRC_PATTERNS += memorymonitor/%
207210
endif
@@ -512,6 +515,9 @@ SRC_SHARED_MODULE_ALL = \
512515
framebufferio/__init__.c \
513516
ipaddress/IPv4Address.c \
514517
ipaddress/__init__.c \
518+
keypad/__init__.c \
519+
keypad/Keys.c \
520+
keypad/State.c \
515521
sdcardio/SDCard.c \
516522
sdcardio/__init__.c \
517523
gamepad/GamePad.c \

py/circuitpy_mpconfig.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,13 @@ extern const struct _mp_obj_module_t espidf_module;
440440
#define ESPIDF_MODULE
441441
#endif
442442

443+
#if CIRCUITPY__EVE
444+
extern const struct _mp_obj_module_t _eve_module;
445+
#define _EVE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__eve), (mp_obj_t)&_eve_module },
446+
#else
447+
#define _EVE_MODULE
448+
#endif
449+
443450
#if CIRCUITPY_FRAMEBUFFERIO
444451
extern const struct _mp_obj_module_t framebufferio_module;
445452
#define FRAMEBUFFERIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_framebufferio), (mp_obj_t)&framebufferio_module },
@@ -523,20 +530,20 @@ extern const struct _mp_obj_module_t ipaddress_module;
523530
#define JSON_MODULE
524531
#endif
525532

533+
#if CIRCUITPY_KEYPAD
534+
extern const struct _mp_obj_module_t keypad_module;
535+
#define KEYPAD_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_keypad), (mp_obj_t)&keypad_module },
536+
#else
537+
#define KEYPAD_MODULE
538+
#endif
539+
526540
#if CIRCUITPY_MATH
527541
extern const struct _mp_obj_module_t math_module;
528542
#define MATH_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module },
529543
#else
530544
#define MATH_MODULE
531545
#endif
532546

533-
#if CIRCUITPY__EVE
534-
extern const struct _mp_obj_module_t _eve_module;
535-
#define _EVE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__eve), (mp_obj_t)&_eve_module },
536-
#else
537-
#define _EVE_MODULE
538-
#endif
539-
540547
#if CIRCUITPY_MEMORYMONITOR
541548
extern const struct _mp_obj_module_t memorymonitor_module;
542549
#define MEMORYMONITOR_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_memorymonitor), (mp_obj_t)&memorymonitor_module },
@@ -877,6 +884,7 @@ extern const struct _mp_obj_module_t msgpack_module;
877884
VECTORIO_MODULE \
878885
ERRNO_MODULE \
879886
ESPIDF_MODULE \
887+
_EVE_MODULE \
880888
FRAMEBUFFERIO_MODULE \
881889
FREQUENCYIO_MODULE \
882890
GAMEPAD_MODULE \
@@ -886,8 +894,8 @@ extern const struct _mp_obj_module_t msgpack_module;
886894
IPADDRESS_MODULE \
887895
IMAGECAPTURE_MODULE \
888896
JSON_MODULE \
897+
KEYPAD_MODULE \
889898
MATH_MODULE \
890-
_EVE_MODULE \
891899
MEMORYMONITOR_MODULE \
892900
MICROCONTROLLER_MODULE \
893901
MSGPACK_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ CFLAGS += -DCIRCUITPY_ERRNO=$(CIRCUITPY_ERRNO)
178178
CIRCUITPY_ESPIDF ?= 0
179179
CFLAGS += -DCIRCUITPY_ESPIDF=$(CIRCUITPY_ESPIDF)
180180

181+
CIRCUITPY__EVE ?= 0
182+
CFLAGS += -DCIRCUITPY__EVE=$(CIRCUITPY__EVE)
183+
181184
CIRCUITPY_FREQUENCYIO ?= $(CIRCUITPY_FULL_BUILD)
182185
CFLAGS += -DCIRCUITPY_FREQUENCYIO=$(CIRCUITPY_FREQUENCYIO)
183186

@@ -199,12 +202,12 @@ CFLAGS += -DCIRCUITPY_IPADDRESS=$(CIRCUITPY_IPADDRESS)
199202
CIRCUITPY_JSON ?= $(CIRCUITPY_FULL_BUILD)
200203
CFLAGS += -DCIRCUITPY_JSON=$(CIRCUITPY_JSON)
201204

205+
CIRCUITPY_KEYPAD ?= $(CIRCUITPY_FULL_BUILD)
206+
CFLAGS += -DCIRCUITPY_KEYPAD=$(CIRCUITPY_KEYPAD)
207+
202208
CIRCUITPY_MATH ?= 1
203209
CFLAGS += -DCIRCUITPY_MATH=$(CIRCUITPY_MATH)
204210

205-
CIRCUITPY__EVE ?= 0
206-
CFLAGS += -DCIRCUITPY__EVE=$(CIRCUITPY__EVE)
207-
208211
CIRCUITPY_MEMORYMONITOR ?= 0
209212
CFLAGS += -DCIRCUITPY_MEMORYMONITOR=$(CIRCUITPY_MEMORYMONITOR)
210213

shared-bindings/keypad/Keys.c

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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 Dan Halbert 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+
#include "py/enum.h"
28+
#include "py/objproperty.h"
29+
#include "shared-bindings/keypad/Keys.h"
30+
#include "shared-bindings/keypad/State.h"
31+
#include "shared-bindings/microcontroller/Pin.h"
32+
#include "py/runtime.h"
33+
34+
//| class Keys:
35+
//| """Manage a set of independent keys."""
36+
//|
37+
//| def __init__(self, pins: Sequence[microcontroller.Pin], *, level_when_pressed: bool, pull: bool = True) -> None:
38+
//| """
39+
//| Create a `Keys` object that will scan keys attached to the given sequence of pins.
40+
//| Each key is independent and attached to its own pin.
41+
//|
42+
//| :param Sequence[microcontroller.Pin] pins: The pins attached to the keys.
43+
//| The key numbers correspond to indices into this sequence.
44+
//| :param bool value_when_pressed: ``True`` if the pin reads high when the key is pressed.
45+
//| ``False`` if the pin reads low (is grounded) when the key is pressed.
46+
//| All the pins must be connected in the same way.
47+
//| :param bool pull: ``True`` if an internal pull-up or pull-down should be
48+
//| enabled on each pin. A pull-up will be used if ``value_when_pressed`` is ``False``;
49+
//| a pull-down will be used if it is ``True``.
50+
//| If an external pull is already provided for all the pins, you can set ``pull`` to ``False``.
51+
//| However, enabling an internal pull when an external one is already present is not a problem;
52+
//| it simply uses slightly more current.
53+
//|
54+
//| Calls `scan()` once before returning, to initialize internal state.
55+
//| """
56+
//| ...
57+
58+
STATIC mp_obj_t keypad_keys_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
59+
keypad_keys_obj_t *self = m_new_obj(keypad_keys_obj_t);
60+
self->base.type = &keypad_keys_type;
61+
enum { ARG_pins, ARG_value_when_pressed, ARG_pull };
62+
static const mp_arg_t allowed_args[] = {
63+
{ MP_QSTR_pins, MP_ARG_REQUIRED | MP_ARG_OBJ },
64+
{ MP_QSTR_value_when_pressed, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
65+
{ MP_QSTR_pull, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
66+
};
67+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
68+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
69+
70+
mp_obj_t pins = args[ARG_pins].u_obj;
71+
const mp_uint_t num_pins = mp_obj_int_get_uint_checked(mp_obj_len(pins));
72+
const bool value_when_pressed = args[ARG_value_when_pressed].u_bool;
73+
74+
mcu_pin_obj_t *pins_array[num_pins];
75+
76+
for (mp_uint_t i = 0; i < num_pins; i++) {
77+
mcu_pin_obj_t *pin =
78+
validate_obj_is_free_pin(mp_obj_subscr(pins, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
79+
pins_array[i] = pin;
80+
}
81+
82+
common_hal_keypad_keys_construct(self, num_pins, pins_array, value_when_pressed, args[ARG_pull].u_bool);
83+
return MP_OBJ_FROM_PTR(self);
84+
}
85+
86+
//| def scan(self) -> None:
87+
//| """Scan the keys and record which are newly pressed, still pressed,
88+
//| newly released, and still released. For convenient activity checking,
89+
//| """
90+
//| ...
91+
//|
92+
STATIC mp_obj_t keypad_keys_scan(mp_obj_t self_in) {
93+
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
94+
95+
common_hal_keypad_keys_scan(self);
96+
return MP_ROM_NONE;
97+
}
98+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_scan_obj, keypad_keys_scan);
99+
100+
//| def state(self, key_num: int) -> keypad.State:
101+
//| """Return the state for the given ``key_num``, based
102+
//| on the results of the most recent `scan()`.
103+
//|
104+
//| :param int key_num: Key number: corresponds to the sequence of pins
105+
//| :return: state of key number ``key_num``
106+
//| :rtype: keypad.State: One of `State.JUST_PRESSED`, `State.STILL_PRESSED`,
107+
//| `State.JUST_RELEASED`, or `State.STILL_RELEASED`.
108+
//| The inclusive states `State.PRESSED` and `State.RELEASED` will *not* be returned.
109+
//| """
110+
//| ...
111+
//|
112+
STATIC mp_obj_t keypad_keys_state(mp_obj_t self_in, mp_obj_t key_num_obj) {
113+
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
114+
mp_int_t key_num = mp_obj_int_get_checked(key_num_obj);
115+
if (key_num < 0 || key_num >= common_hal_keypad_keys_length(self)) {
116+
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_key_num);
117+
}
118+
119+
return cp_enum_find(&keypad_state_type, common_hal_keypad_keys_state(self, (mp_uint_t)key_num));
120+
}
121+
MP_DEFINE_CONST_FUN_OBJ_2(keypad_keys_state_obj, keypad_keys_state);
122+
123+
//| def keys_with_state(self, state: State, into_list: List[Optional[int]]) -> None:
124+
//| """Store key numbers of keys with state ``state`` in ``into_list``.
125+
//| The states checked are based on the results of the most recent `scan()`.
126+
//|
127+
//| You can use the inclusive states `State.PRESSED` and `State.RELEASED`.
128+
//| `State.PRESSED` includes states `State.JUST_PRESSED` and `State.STILL_PRESSED`.
129+
//| `State.RELEASED` includes `State.JUST_RELEASED` and `State.STILL_RELEASED`.
130+
//|
131+
//| The key numbers are stored in ``into_list`` consecutively, up to ``len(into_list)``.
132+
//| The ``into_list`` is not extended if there are more keys with the given
133+
//| state than list slots. Instead, leftover key numbers are discarded.
134+
//| If there are fewer keys with the given state, the rest of ``into_list``
135+
//| is padded with ``None``. For example,
136+
//| if four keys are being monitored, and only key numbers 0 and 2 have the given state,
137+
//| ``into_list`` will be set to ``[0, 2, None, None]``. You can iterate over
138+
//| ``into_list`` and stop when you find the first ``None``.
139+
//| """
140+
//| ...
141+
//|
142+
STATIC mp_obj_t keypad_keys_keys_with_state(mp_obj_t self_in, mp_obj_t state_in, mp_obj_t into_list_in) {
143+
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
144+
145+
if (!mp_obj_is_type(state_in, &keypad_state_type)) {
146+
mp_raise_ValueError_varg(translate("Expected a %q"), keypad_state_type.name);
147+
}
148+
149+
if (!mp_obj_is_type(into_list_in, &mp_type_list)) {
150+
mp_raise_ValueError_varg(translate("Expected a %q"), mp_type_list.name);
151+
}
152+
153+
int state = cp_enum_value(&keypad_state_type, state_in);
154+
mp_obj_list_t *into_list = MP_OBJ_TO_PTR(into_list_in);
155+
156+
common_hal_keypad_keys_keys_with_state(self, state, into_list);
157+
158+
return MP_ROM_NONE;
159+
}
160+
MP_DEFINE_CONST_FUN_OBJ_3(keypad_keys_keys_with_state_obj, keypad_keys_keys_with_state);
161+
162+
STATIC const mp_rom_map_elem_t keypad_keys_locals_dict_table[] = {
163+
{ MP_ROM_QSTR(MP_QSTR_keys_with_state), MP_ROM_PTR(&keypad_keys_keys_with_state_obj) },
164+
{ MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&keypad_keys_scan_obj) },
165+
{ MP_ROM_QSTR(MP_QSTR_state), MP_ROM_PTR(&keypad_keys_state_obj) },
166+
};
167+
168+
STATIC MP_DEFINE_CONST_DICT(keypad_keys_locals_dict, keypad_keys_locals_dict_table);
169+
170+
const mp_obj_type_t keypad_keys_type = {
171+
{ &mp_type_type },
172+
.name = MP_QSTR_Keys,
173+
.make_new = keypad_keys_make_new,
174+
.locals_dict = (mp_obj_t)&keypad_keys_locals_dict,
175+
};

shared-bindings/keypad/Keys.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 Dan Halbert 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+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H
29+
30+
#include "py/objlist.h"
31+
#include "shared-module/keypad/Keys.h"
32+
33+
extern const mp_obj_type_t keypad_keys_type;
34+
35+
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull);
36+
void common_hal_keypad_keys_keys_with_state(keypad_keys_obj_t *self, mp_int_t state, mp_obj_list_t *into);
37+
size_t common_hal_keypad_keys_length(keypad_keys_obj_t *self);
38+
void common_hal_keypad_keys_scan(keypad_keys_obj_t *self);
39+
mp_int_t common_hal_keypad_keys_state(keypad_keys_obj_t *self, mp_uint_t key_num);
40+
41+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H

0 commit comments

Comments
 (0)