Skip to content

keypad: support for vector and matrix key scanning #4891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e09a771
Update translation files
weblate May 24, 2021
b8132c5
Translated using Weblate (Spanish)
jposada202020 May 24, 2021
3b11e6f
Translated using Weblate (Swedish)
bergdahl May 24, 2021
dd3e129
Translated using Weblate (Portuguese (Brazil))
wtuemura May 25, 2021
a7ce843
Translated using Weblate (Chinese (Pinyin))
hexthat May 24, 2021
51fc902
Remove non-product Adafruit boards
dhalbert May 26, 2021
050b5f4
fix check of usb_hid.Device out_report_length
dhalbert May 28, 2021
44b876f
Inital keypad work: Keys working: one pin per key
dhalbert May 29, 2021
abe29fc
merge from upstream
dhalbert Jun 8, 2021
7d23206
Inital keypad work: Keys working: one pin per key
dhalbert May 29, 2021
4deca98
Merge branch 'keypad-scanning' of https://github.com/dhalbert/circuit…
dhalbert Jun 10, 2021
bdb7066
fix bad merge
dhalbert Jun 10, 2021
d3d9e0a
add debouncing
dhalbert Jun 10, 2021
01a8a95
fix signed/unsigned compilation problem
dhalbert Jun 10, 2021
c6e2237
fix non-longint builds
dhalbert Jun 10, 2021
b815164
Turn off keypad on a few small builds
dhalbert Jun 10, 2021
627c426
wip
dhalbert Jun 14, 2021
32eec85
compiles
dhalbert Jun 15, 2021
350652e
Keys works; more testing to do
dhalbert Jun 15, 2021
1803a6a
both Keys and KeyMatrix work
dhalbert Jun 15, 2021
a192f1c
merge from upstream
dhalbert Jun 15, 2021
f97e0ec
fix ulab commit
dhalbert Jun 15, 2021
4655a71
Turn off keypad on some builds to avoid overflow
dhalbert Jun 15, 2021
3d18c5c
Use a single list of keypad scanners
dhalbert Jun 16, 2021
cd31136
factor out keypad.EventQueue
dhalbert Jun 16, 2021
a152bd3
add .num_keys and .store_states() to Keys and KeyMatrix
dhalbert Jun 17, 2021
04b69cd
turn off gamepad on most builds;turn off keypad where appropriate
dhalbert Jun 17, 2021
af66931
Added keypad.ShiftRegisterKeys
dhalbert Jun 18, 2021
8cd7e77
add KeyMatrix columns_to_anodes arg
dhalbert Jun 18, 2021
03adbbc
turn off keypad by default on all SAMD21, for now
dhalbert Jun 18, 2021
db297ad
fix other too-big builds
dhalbert Jun 18, 2021
154e91a
add EventQueue.store_next() to allow reusing event objects
dhalbert Jun 21, 2021
ed41acd
merge from upstream to fix translations
dhalbert Jun 21, 2021
a718d33
fix doc typos
dhalbert Jun 21, 2021
51c547a
add generic arg validation routines; add interval args to keypad
dhalbert Jun 21, 2021
8c74b4a
fix some typos
dhalbert Jun 21, 2021
f052dc4
ShiftRegisterKeys: allow specifying sense of latch
dhalbert Jun 21, 2021
acf90fb
many renamings; add overflowed flag to EventQuque
dhalbert Jun 23, 2021
7774b18
Add reset() to scanners. Clear .overflow on EventQueue.clear().
dhalbert Jun 23, 2021
4f538b6
remove pressed() and get_states_into()
dhalbert Jun 23, 2021
2e67d67
check_for_deinit() more places in scanners
dhalbert Jun 23, 2021
36472a0
Correct doc in EventQueue.c
dhalbert Jun 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,22 @@ msgstr ""
msgid "%q indices must be integers, not %s"
msgstr ""

#: py/argcheck.c
msgid "%q length must be %q"
msgstr ""

#: shared-bindings/vectorio/Polygon.c
msgid "%q list must be a list"
msgstr ""

#: py/argcheck.c
msgid "%q must <= %d"
msgstr ""

#: py/argcheck.c
msgid "%q must be %d-%d"
msgstr ""

#: shared-bindings/usb_hid/Device.c
msgid "%q must be 0-255"
msgstr ""
Expand All @@ -95,7 +107,11 @@ msgstr ""
msgid "%q must be 1-255"
msgstr ""

#: shared-bindings/memorymonitor/AllocationAlarm.c
#: py/argcheck.c
msgid "%q must be >= %d"
msgstr ""

#: py/argcheck.c shared-bindings/memorymonitor/AllocationAlarm.c
msgid "%q must be >= 0"
msgstr ""

Expand All @@ -111,6 +127,10 @@ msgstr ""
msgid "%q must be None or between 1 and len(report_descriptor)-1"
msgstr ""

#: py/argcheck.c
msgid "%q must be a string"
msgstr ""

#: shared-module/vectorio/Polygon.c
msgid "%q must be a tuple of length 2"
msgstr ""
Expand All @@ -119,6 +139,15 @@ msgstr ""
msgid "%q must be between %d and %d"
msgstr ""

#: py/argcheck.c
msgid "%q must of type %q"
msgstr ""

#: shared-bindings/keypad/KeyMatrix.c shared-bindings/keypad/Keys.c
#: shared-bindings/keypad/ShiftRegisterKeys.c
msgid "%q must store bytes"
msgstr ""

#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
#: shared-bindings/canio/Match.c
msgid "%q out of range"
Expand Down
10 changes: 10 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
#include "shared-module/displayio/__init__.h"
#endif

#if CIRCUITPY_KEYPAD
#include "shared-module/keypad/__init__.h"
#endif

#if CIRCUITPY_MEMORYMONITOR
#include "shared-module/memorymonitor/__init__.h"
#endif
Expand Down Expand Up @@ -230,9 +234,11 @@ STATIC void cleanup_after_vm(supervisor_allocation* heap) {
#if CIRCUITPY_DISPLAYIO
reset_displays();
#endif

#if CIRCUITPY_MEMORYMONITOR
memorymonitor_reset();
#endif

filesystem_flush();
stop_mp();
free_memory(heap);
Expand All @@ -242,6 +248,10 @@ STATIC void cleanup_after_vm(supervisor_allocation* heap) {
common_hal_canio_reset();
#endif

#if CIRCUITPY_KEYPAD
keypad_reset();
#endif

// reset_board_busses() first because it may release pins from the never_reset state, so that
// reset_port() can reset them.
#if CIRCUITPY_BOARD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LONGINT_IMPL = NONE

CIRCUITPY_BUSDEVICE = 1
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_KEYPAD = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ EXTERNAL_FLASH_DEVICES = "S25FL216K, GD25Q16C"

# Turn off features and optimizations for displayio build to make room for additional frozen libs.
LONGINT_IMPL = NONE
CIRCUITPY_KEYPAD = 0
CIRCUITPY_PIXELBUF = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ LONGINT_IMPL = NONE

# To keep the build small
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_GAMEPAD = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CIRCUITPY_AUDIOMP3 = 0
CIRCUITPY_BLEIO_HCI = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_KEYPAD = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_PS2IO = 0
CIRCUITPY_RGBMATRIX = 0
Expand Down
2 changes: 1 addition & 1 deletion ports/atmel-samd/boards/pycubed/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ CIRCUITPY_DRIVE_LABEL = "PYCUBED"
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_KEYPAD = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_RGBMATRIX = 0
CIRCUITPY_PS2IO = 0

Expand Down
2 changes: 1 addition & 1 deletion ports/atmel-samd/boards/pycubed_mram/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_KEYPAD = 0
CIRCUITPY_RGBMATRIX = 0
CIRCUITPY_PS2IO = 0

Expand Down
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/stringcar_m0_express/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ SPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = AT25SF161
LONGINT_IMPL = MPZ

CIRCUITPY_KEYPAD = 0

CIRCUITPY_BITBANG_APA102 = 1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_BLEIO = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_RGBMATRIX = 0
Expand Down
1 change: 0 additions & 1 deletion ports/atmel-samd/boards/winterbloom_sol/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_BLEIO_HCI = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_RGBMATRIX = 0
Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ CIRCUITPY_FRAMEBUFFERIO ?= 0
CIRCUITPY_FREQUENCYIO ?= 0
CIRCUITPY_I2CPERIPHERAL ?= 0
CIRCUITPY_JSON ?= 0
CIRCUITPY_KEYPAD ?= 0
CIRCUITPY_MSGPACK ?= 0
CIRCUITPY_RE ?= 0
CIRCUITPY_SDCARDIO ?= 0
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/pca10100/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_JSON = 0
CIRCUITPY_KEYPAD = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_NVM = 0
Expand Down
2 changes: 1 addition & 1 deletion ports/nrf/boards/simmel/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CIRCUITPY_COUNTIO = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_ERRNO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_KEYPAD = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_NVM = 0
Expand Down
1 change: 1 addition & 0 deletions ports/stm/boards/espruino_pico/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CIRCUITPY_AUDIOPWMIO = 0
CIRCUITPY_BUSDEVICE = 0
CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_KEYPAD = 0
CIRCUITPY_MIDI = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_ULAB = 0
Expand Down
1 change: 1 addition & 0 deletions ports/stm/boards/meowbit_v121/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ LD_FILE = boards/STM32F401xe_boot.ld
# For debugging - also comment BOOTLOADER_OFFSET and BOARD_VTOR_DEFER
# LD_FILE = boards/STM32F401xe_fs.ld

CIRCUITPY_BLEIO_HCI = 0
CIRCUITPY_ULAB = 0
1 change: 1 addition & 0 deletions ports/stm/boards/pyb_nano_v2/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ LD_FILE = boards/STM32F411_fs.ld
# Too big for the flash
CIRCUITPY_AUDIOCORE = 0
CIRCUITPY_AUDIOPWMIO = 0
CIRCUITPY_KEYPAD = 0
CIRCUITPY_MIDI = 0
CIRCUITPY_MSGPACK = 0
1 change: 1 addition & 0 deletions ports/stm/boards/stm32f411ve_discovery/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ LD_FILE = boards/STM32F411_fs.ld
# Too big for the flash
CIRCUITPY_AUDIOCORE = 0
CIRCUITPY_AUDIOPWMIO = 0
CIRCUITPY_KEYPAD = 0
CIRCUITPY_MIDI = 0
CIRCUITPY_MSGPACK = 0
53 changes: 53 additions & 0 deletions py/argcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,56 @@ NORETURN void mp_arg_error_unimpl_kw(void) {
mp_raise_NotImplementedError(MP_ERROR_TEXT("keyword argument(s) not yet implemented - use normal args instead"));
}
#endif


mp_int_t mp_arg_validate_int_min(mp_int_t i, mp_int_t min, qstr arg_name) {
if (i < min) {
mp_raise_ValueError_varg(translate("%q must be >= %d"), arg_name, min);
}
return i;
}

mp_int_t mp_arg_validate_int_max(mp_int_t i, mp_int_t max, qstr arg_name) {
if (i > max) {
mp_raise_ValueError_varg(translate("%q must <= %d"), arg_name, max);
}
return i;
}

mp_int_t mp_arg_validate_int_range(mp_int_t i, mp_int_t min, mp_int_t max, qstr arg_name) {
if (i < min || i > max) {
mp_raise_ValueError_varg(translate("%q must be %d-%d"), arg_name, min, max);
}
return i;
}

mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t default_for_null, qstr arg_name) {
const mp_float_t f = (float_in == MP_OBJ_NULL)
? default_for_null
: mp_obj_get_float(float_in);
if (f <= (mp_float_t)0.0) {
mp_raise_ValueError_varg(translate("%q must be >= 0"), arg_name);
}
return f;
}

size_t mp_arg_validate_length_with_name(mp_int_t i, size_t length, qstr arg_name, qstr length_name) {
if (i != (mp_int_t)length) {
mp_raise_ValueError_varg(translate("%q length must be %q"), MP_QSTR_pressed, MP_QSTR_num_keys);
}
return (size_t)i;
}

mp_obj_t mp_arg_validate_type(mp_obj_t obj, const mp_obj_type_t *type, qstr arg_name) {
if (!mp_obj_is_type(obj, type)) {
mp_raise_TypeError_varg(translate("%q must of type %q"), arg_name, type->name);
}
return obj;
}

mp_obj_t mp_arg_validate_string(mp_obj_t obj, qstr arg_name) {
if (!mp_obj_is_str(obj)) {
mp_raise_TypeError_varg(translate("%q must be a string"), arg_name);
}
return obj;
}
15 changes: 12 additions & 3 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ endif
ifeq ($(CIRCUITPY_FRAMEBUFFERIO),1)
SRC_PATTERNS += framebufferio/%
endif
ifeq ($(CIRCUITPY__EVE),1)
SRC_PATTERNS += _eve/%
endif
ifeq ($(CIRCUITPY_FREQUENCYIO),1)
SRC_PATTERNS += frequencyio/%
endif
Expand All @@ -196,12 +199,12 @@ endif
ifeq ($(CIRCUITPY_IPADDRESS),1)
SRC_PATTERNS += ipaddress/%
endif
ifeq ($(CIRCUITPY_KEYPAD),1)
SRC_PATTERNS += keypad/%
endif
ifeq ($(CIRCUITPY_MATH),1)
SRC_PATTERNS += math/%
endif
ifeq ($(CIRCUITPY__EVE),1)
SRC_PATTERNS += _eve/%
endif
ifeq ($(CIRCUITPY_MEMORYMONITOR),1)
SRC_PATTERNS += memorymonitor/%
endif
Expand Down Expand Up @@ -512,6 +515,12 @@ SRC_SHARED_MODULE_ALL = \
framebufferio/__init__.c \
ipaddress/IPv4Address.c \
ipaddress/__init__.c \
keypad/__init__.c \
keypad/Event.c \
keypad/EventQueue.c \
keypad/KeyMatrix.c \
keypad/ShiftRegisterKeys.c \
keypad/Keys.c \
sdcardio/SDCard.c \
sdcardio/__init__.c \
gamepad/GamePad.c \
Expand Down
35 changes: 27 additions & 8 deletions py/circuitpy_mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,13 @@ extern const struct _mp_obj_module_t espidf_module;
#define ESPIDF_MODULE
#endif

#if CIRCUITPY__EVE
extern const struct _mp_obj_module_t _eve_module;
#define _EVE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__eve), (mp_obj_t)&_eve_module },
#else
#define _EVE_MODULE
#endif

#if CIRCUITPY_FRAMEBUFFERIO
extern const struct _mp_obj_module_t framebufferio_module;
#define FRAMEBUFFERIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_framebufferio), (mp_obj_t)&framebufferio_module },
Expand Down Expand Up @@ -523,20 +530,30 @@ extern const struct _mp_obj_module_t ipaddress_module;
#define JSON_MODULE
#endif

#if CIRCUITPY_KEYPAD
extern const struct _mp_obj_module_t keypad_module;
#define KEYPAD_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_keypad), (mp_obj_t)&keypad_module },
#define KEYPAD_ROOT_POINTERS mp_obj_t keypad_scanners_linked_list;
#else
#define KEYPAD_MODULE
#define KEYPAD_ROOT_POINTERS
#endif

#if CIRCUITPY_GAMEPAD || CIRCUITPY_GAMEPADSHIFT
// Scan gamepad every 32ms
#define CIRCUITPY_GAMEPAD_TICKS 0x1f
#define GAMEPAD_ROOT_POINTERS mp_obj_t gamepad_singleton;
#else
#define GAMEPAD_ROOT_POINTERS
#endif

#if CIRCUITPY_MATH
extern const struct _mp_obj_module_t math_module;
#define MATH_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module },
#else
#define MATH_MODULE
#endif

#if CIRCUITPY__EVE
extern const struct _mp_obj_module_t _eve_module;
#define _EVE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__eve), (mp_obj_t)&_eve_module },
#else
#define _EVE_MODULE
#endif

#if CIRCUITPY_MEMORYMONITOR
extern const struct _mp_obj_module_t memorymonitor_module;
#define MEMORYMONITOR_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_memorymonitor), (mp_obj_t)&memorymonitor_module },
Expand Down Expand Up @@ -877,6 +894,7 @@ extern const struct _mp_obj_module_t msgpack_module;
VECTORIO_MODULE \
ERRNO_MODULE \
ESPIDF_MODULE \
_EVE_MODULE \
FRAMEBUFFERIO_MODULE \
FREQUENCYIO_MODULE \
GAMEPAD_MODULE \
Expand All @@ -886,8 +904,8 @@ extern const struct _mp_obj_module_t msgpack_module;
IPADDRESS_MODULE \
IMAGECAPTURE_MODULE \
JSON_MODULE \
KEYPAD_MODULE \
MATH_MODULE \
_EVE_MODULE \
MEMORYMONITOR_MODULE \
MICROCONTROLLER_MODULE \
MSGPACK_MODULE \
Expand Down Expand Up @@ -954,6 +972,7 @@ struct _supervisor_allocation_node;
vstr_t *repl_line; \
mp_obj_t rtc_time_source; \
GAMEPAD_ROOT_POINTERS \
KEYPAD_ROOT_POINTERS \
mp_obj_t pew_singleton; \
BOARD_UART_ROOT_POINTER \
FLASH_ROOT_POINTERS \
Expand Down
Loading