From 88b4e38625fe647cb98cc28255a25fef3b01a91f Mon Sep 17 00:00:00 2001 From: CDarius Date: Mon, 18 Mar 2024 17:11:34 +0100 Subject: [PATCH] Moved DemuxKeyMatrix in the keypaddemux module --- docs/shared_bindings_matrix.py | 2 +- ports/atmel-samd/mpconfigport.mk | 2 +- py/circuitpy_defns.mk | 8 ++- py/circuitpy_mpconfig.mk | 4 +- shared-bindings/keypad/__init__.c | 4 -- .../{keypad => keypaddemux}/DemuxKeyMatrix.c | 66 +++++++++---------- .../{keypad => keypaddemux}/DemuxKeyMatrix.h | 22 +++---- shared-bindings/keypaddemux/__init__.c | 52 +++++++++++++++ .../{keypad => keypaddemux}/DemuxKeyMatrix.c | 28 ++++---- .../{keypad => keypaddemux}/DemuxKeyMatrix.h | 10 +-- shared-module/keypaddemux/__init__.c | 0 11 files changed, 125 insertions(+), 73 deletions(-) rename shared-bindings/{keypad => keypaddemux}/DemuxKeyMatrix.c (75%) rename shared-bindings/{keypad => keypaddemux}/DemuxKeyMatrix.h (51%) create mode 100644 shared-bindings/keypaddemux/__init__.c rename shared-module/{keypad => keypaddemux}/DemuxKeyMatrix.c (78%) rename shared-module/{keypad => keypaddemux}/DemuxKeyMatrix.h (82%) create mode 100644 shared-module/keypaddemux/__init__.c diff --git a/docs/shared_bindings_matrix.py b/docs/shared_bindings_matrix.py index e9f1583fb2ca..b5cbc62aa6e4 100644 --- a/docs/shared_bindings_matrix.py +++ b/docs/shared_bindings_matrix.py @@ -78,7 +78,7 @@ "keypad.KeyMatrix": "CIRCUITPY_KEYPAD_KEYMATRIX", "keypad.Keys": "CIRCUITPY_KEYPAD_KEYS", "keypad.ShiftRegisterKeys": "CIRCUITPY_KEYPAD_SHIFTREGISTERKEYS", - "keypad.DemuxKeyMatrix": "CIRCUITPY_KEYPAD_DEMUXKEYMATRIX", + "keypaddemux.DemuxKeyMatrix": "CIRCUITPY_KEYPAD_DEMUX", "os.getenv": "CIRCUITPY_OS_GETENV", "select": "MICROPY_PY_SELECT_SELECT", "sys": "CIRCUITPY_SYS", diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index 8ea028a43b07..73656d01573a 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -11,7 +11,7 @@ CIRCUITPY_ROTARYIO_SOFTENCODER = 1 CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE ?= 1 CIRCUITPY_LTO = 1 -CIRCUITPY_KEYPAD_DEMUXKEYMATRIX ?= 0 +CIRCUITPY_KEYPAD_DEMUX ?= 0 ###################################################################### # Put samd21-only choices here. diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 5cf32f2b8e30..67d91f3b1668 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -252,6 +252,9 @@ endif ifeq ($(CIRCUITPY_KEYPAD),1) SRC_PATTERNS += keypad/% endif +ifeq ($(CIRCUITPY_KEYPAD_DEMUX),1) +SRC_PATTERNS += keypaddemux/% +endif ifeq ($(CIRCUITPY_LOCALE),1) SRC_PATTERNS += locale/% endif @@ -739,9 +742,10 @@ SRC_SHARED_MODULE_ALL += \ ssl/SSLSocket.c endif -ifeq ($(CIRCUITPY_KEYPAD_DEMUXKEYMATRIX),1) +ifeq ($(CIRCUITPY_KEYPAD_DEMUX),1) SRC_SHARED_MODULE_ALL += \ - keypad/DemuxKeyMatrix.c + keypaddemux/__init__.c \ + keypaddemux/DemuxKeyMatrix.c endif # If supporting _bleio via HCI, make devices/ble_hci/common-hal/_bleio be includable, diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 255041e25e7b..753ea8ab3199 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -345,8 +345,8 @@ CFLAGS += -DCIRCUITPY_KEYPAD_KEYMATRIX=$(CIRCUITPY_KEYPAD_KEYMATRIX) CIRCUITPY_KEYPAD_SHIFTREGISTERKEYS ?= $(CIRCUITPY_KEYPAD) CFLAGS += -DCIRCUITPY_KEYPAD_SHIFTREGISTERKEYS=$(CIRCUITPY_KEYPAD_SHIFTREGISTERKEYS) -CIRCUITPY_KEYPAD_DEMUXKEYMATRIX ?= $(CIRCUITPY_KEYPAD) -CFLAGS += -DCIRCUITPY_KEYPAD_DEMUXKEYMATRIX=$(CIRCUITPY_KEYPAD_DEMUXKEYMATRIX) +CIRCUITPY_KEYPAD_DEMUX ?= $(CIRCUITPY_KEYPAD) +CFLAGS += -DCIRCUITPY_KEYPAD_DEMUX=$(CIRCUITPY_KEYPAD_DEMUX) CIRCUITPY_LOCALE ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_LOCALE=$(CIRCUITPY_LOCALE) diff --git a/shared-bindings/keypad/__init__.c b/shared-bindings/keypad/__init__.c index 07ef82b7bacb..bf5b334d3166 100644 --- a/shared-bindings/keypad/__init__.c +++ b/shared-bindings/keypad/__init__.c @@ -32,7 +32,6 @@ #include "shared-bindings/keypad/KeyMatrix.h" #include "shared-bindings/keypad/Keys.h" #include "shared-bindings/keypad/ShiftRegisterKeys.h" -#include "shared-bindings/keypad/DemuxKeyMatrix.h" #include "shared-bindings/util.h" STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) { @@ -101,9 +100,6 @@ STATIC mp_rom_map_elem_t keypad_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_KeyMatrix), MP_OBJ_FROM_PTR(&keypad_keymatrix_type) }, { MP_ROM_QSTR(MP_QSTR_Keys), MP_OBJ_FROM_PTR(&keypad_keys_type) }, { MP_ROM_QSTR(MP_QSTR_ShiftRegisterKeys), MP_OBJ_FROM_PTR(&keypad_shiftregisterkeys_type) }, - #if CIRCUITPY_KEYPAD_DEMUXKEYMATRIX - { MP_ROM_QSTR(MP_QSTR_DemuxKeyMatrix), MP_OBJ_FROM_PTR(&keypad_demuxkeymatrix_type) }, - #endif }; STATIC MP_DEFINE_CONST_DICT(keypad_module_globals, keypad_module_globals_table); diff --git a/shared-bindings/keypad/DemuxKeyMatrix.c b/shared-bindings/keypaddemux/DemuxKeyMatrix.c similarity index 75% rename from shared-bindings/keypad/DemuxKeyMatrix.c rename to shared-bindings/keypaddemux/DemuxKeyMatrix.c index a7ca9b6325b9..6f49a641bcec 100644 --- a/shared-bindings/keypad/DemuxKeyMatrix.c +++ b/shared-bindings/keypaddemux/DemuxKeyMatrix.c @@ -30,7 +30,7 @@ #include "py/runtime.h" #include "shared-bindings/keypad/__init__.h" #include "shared-bindings/keypad/Event.h" -#include "shared-bindings/keypad/DemuxKeyMatrix.h" +#include "shared-bindings/keypaddemux/DemuxKeyMatrix.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/util.h" @@ -43,7 +43,7 @@ //|
//| Available on these boards //| @@ -80,8 +80,8 @@ //| """ //| ... -STATIC mp_obj_t keypad_demuxkeymatrix_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - keypad_demuxkeymatrix_obj_t *self = mp_obj_malloc(keypad_demuxkeymatrix_obj_t, &keypad_demuxkeymatrix_type); +STATIC mp_obj_t keypad_demux_demuxkeymatrix_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + keypad_demux_demuxkeymatrix_obj_t *self = mp_obj_malloc(keypad_demux_demuxkeymatrix_obj_t, &keypad_demux_demuxkeymatrix_type); enum { ARG_row_addr_pins, ARG_column_pins, ARG_interval, ARG_max_events }; static const mp_arg_t allowed_args[] = { { MP_QSTR_row_addr_pins, MP_ARG_REQUIRED | MP_ARG_OBJ }, @@ -120,19 +120,19 @@ STATIC mp_obj_t keypad_demuxkeymatrix_make_new(const mp_obj_type_t *type, size_t column_pins_array[column] = pin; } - common_hal_keypad_demuxkeymatrix_construct(self, num_row_addr_pins, row_addr_pins_array, num_column_pins, column_pins_array, interval, max_events); + common_hal_keypad_demux_demuxkeymatrix_construct(self, num_row_addr_pins, row_addr_pins_array, num_column_pins, column_pins_array, interval, max_events); return MP_OBJ_FROM_PTR(self); } //| def deinit(self) -> None: //| """Stop scanning and release the pins.""" //| ... -STATIC mp_obj_t keypad_demuxkeymatrix_deinit(mp_obj_t self_in) { - keypad_demuxkeymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_keypad_demuxkeymatrix_deinit(self); +STATIC mp_obj_t keypad_demux_demuxkeymatrix_deinit(mp_obj_t self_in) { + keypad_demux_demuxkeymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_keypad_demux_demuxkeymatrix_deinit(self); return MP_ROM_NONE; } -MP_DEFINE_CONST_FUN_OBJ_1(keypad_demuxkeymatrix_deinit_obj, keypad_demuxkeymatrix_deinit); +MP_DEFINE_CONST_FUN_OBJ_1(keypad_demux_demuxkeymatrix_deinit_obj, keypad_demux_demuxkeymatrix_deinit); //| def __enter__(self) -> KeyMatrix: //| """No-op used by Context Managers.""" @@ -143,14 +143,14 @@ MP_DEFINE_CONST_FUN_OBJ_1(keypad_demuxkeymatrix_deinit_obj, keypad_demuxkeymatri //| """Automatically deinitializes when exiting a context. See //| :ref:`lifetime-and-contextmanagers` for more info.""" //| ... -STATIC mp_obj_t keypad_demuxkeymatrix___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t keypad_demux_demuxkeymatrix___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_keypad_demuxkeymatrix_deinit(args[0]); + common_hal_keypad_demux_demuxkeymatrix_deinit(args[0]); return MP_ROM_NONE; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_demuxkeymatrix___exit___obj, 4, 4, keypad_demuxkeymatrix___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_demux_demuxkeymatrix___exit___obj, 4, 4, keypad_demux_demuxkeymatrix___exit__); -STATIC void check_for_deinit(keypad_demuxkeymatrix_obj_t *self) { +STATIC void check_for_deinit(keypad_demux_demuxkeymatrix_obj_t *self) { if (common_hal_keypad_deinited(self)) { raise_deinited_error(); } @@ -176,8 +176,8 @@ STATIC void check_for_deinit(keypad_demuxkeymatrix_obj_t *self) { //| :rtype: Tuple[int] //| """ //| ... -STATIC mp_obj_t keypad_demuxkeymatrix_key_number_to_row_column(mp_obj_t self_in, mp_obj_t key_number_in) { - keypad_demuxkeymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t keypad_demux_demuxkeymatrix_key_number_to_row_column(mp_obj_t self_in, mp_obj_t key_number_in) { + keypad_demux_demuxkeymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); const mp_uint_t key_number = (mp_uint_t)mp_arg_validate_int_range( @@ -187,7 +187,7 @@ STATIC mp_obj_t keypad_demuxkeymatrix_key_number_to_row_column(mp_obj_t self_in, mp_uint_t row; mp_uint_t column; - common_hal_keypad_demuxkeymatrix_key_number_to_row_column(self, key_number, &row, &column); + common_hal_keypad_demux_demuxkeymatrix_key_number_to_row_column(self, key_number, &row, &column); mp_obj_t row_column[2]; row_column[0] = MP_OBJ_NEW_SMALL_INT(row); @@ -195,51 +195,51 @@ STATIC mp_obj_t keypad_demuxkeymatrix_key_number_to_row_column(mp_obj_t self_in, return mp_obj_new_tuple(2, row_column); } -MP_DEFINE_CONST_FUN_OBJ_2(keypad_demuxkeymatrix_key_number_to_row_column_obj, keypad_demuxkeymatrix_key_number_to_row_column); +MP_DEFINE_CONST_FUN_OBJ_2(keypad_demux_demuxkeymatrix_key_number_to_row_column_obj, keypad_demux_demuxkeymatrix_key_number_to_row_column); //| def row_column_to_key_number(self, row: int, column: int) -> int: //| """Return the key number for a given row and column. //| The key number is ``row * len(column_pins) + column``. //| """ //| ... -STATIC mp_obj_t keypad_demuxkeymatrix_row_column_to_key_number(mp_obj_t self_in, mp_obj_t row_in, mp_obj_t column_in) { - keypad_demuxkeymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t keypad_demux_demuxkeymatrix_row_column_to_key_number(mp_obj_t self_in, mp_obj_t row_in, mp_obj_t column_in) { + keypad_demux_demuxkeymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); const mp_uint_t row = (mp_uint_t)mp_arg_validate_int_range( - mp_obj_get_int(row_in), 0, (mp_int_t)common_hal_keypad_demuxkeymatrix_get_row_count(self), MP_QSTR_row); + mp_obj_get_int(row_in), 0, (mp_int_t)common_hal_keypad_demux_demuxkeymatrix_get_row_count(self), MP_QSTR_row); const mp_int_t column = (mp_uint_t)mp_arg_validate_int_range( - mp_obj_get_int(column_in), 0, (mp_int_t)common_hal_keypad_demuxkeymatrix_get_column_count(self), MP_QSTR_column); + mp_obj_get_int(column_in), 0, (mp_int_t)common_hal_keypad_demux_demuxkeymatrix_get_column_count(self), MP_QSTR_column); return MP_OBJ_NEW_SMALL_INT( - (mp_int_t)common_hal_keypad_demuxkeymatrix_row_column_to_key_number(self, row, column)); + (mp_int_t)common_hal_keypad_demux_demuxkeymatrix_row_column_to_key_number(self, row, column)); } -MP_DEFINE_CONST_FUN_OBJ_3(keypad_demuxkeymatrix_row_column_to_key_number_obj, keypad_demuxkeymatrix_row_column_to_key_number); +MP_DEFINE_CONST_FUN_OBJ_3(keypad_demux_demuxkeymatrix_row_column_to_key_number_obj, keypad_demux_demuxkeymatrix_row_column_to_key_number); //| events: EventQueue //| """The `EventQueue` associated with this `Keys` object. (read-only) //| """ //| -STATIC const mp_rom_map_elem_t keypad_demuxkeymatrix_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_demuxkeymatrix_deinit_obj) }, +STATIC const mp_rom_map_elem_t keypad_demux_demuxkeymatrix_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_demux_demuxkeymatrix_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_demuxkeymatrix___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_demux_demuxkeymatrix___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_generic_events_obj) }, { MP_ROM_QSTR(MP_QSTR_key_count), MP_ROM_PTR(&keypad_generic_key_count_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_generic_reset_obj) }, - { MP_ROM_QSTR(MP_QSTR_key_number_to_row_column), MP_ROM_PTR(&keypad_demuxkeymatrix_key_number_to_row_column_obj) }, - { MP_ROM_QSTR(MP_QSTR_row_column_to_key_number), MP_ROM_PTR(&keypad_demuxkeymatrix_row_column_to_key_number_obj) }, + { MP_ROM_QSTR(MP_QSTR_key_number_to_row_column), MP_ROM_PTR(&keypad_demux_demuxkeymatrix_key_number_to_row_column_obj) }, + { MP_ROM_QSTR(MP_QSTR_row_column_to_key_number), MP_ROM_PTR(&keypad_demux_demuxkeymatrix_row_column_to_key_number_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(keypad_demuxkeymatrix_locals_dict, keypad_demuxkeymatrix_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(keypad_demux_demuxkeymatrix_locals_dict, keypad_demux_demuxkeymatrix_locals_dict_table); MP_DEFINE_CONST_OBJ_TYPE( - keypad_demuxkeymatrix_type, - MP_QSTR_KeyMatrix, + keypad_demux_demuxkeymatrix_type, + MP_QSTR_DemuxKeyMatrix, MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, - make_new, keypad_demuxkeymatrix_make_new, - locals_dict, &keypad_demuxkeymatrix_locals_dict + make_new, keypad_demux_demuxkeymatrix_make_new, + locals_dict, &keypad_demux_demuxkeymatrix_locals_dict ); diff --git a/shared-bindings/keypad/DemuxKeyMatrix.h b/shared-bindings/keypaddemux/DemuxKeyMatrix.h similarity index 51% rename from shared-bindings/keypad/DemuxKeyMatrix.h rename to shared-bindings/keypaddemux/DemuxKeyMatrix.h index e6adcfab2afe..fc45a39d78b6 100644 --- a/shared-bindings/keypad/DemuxKeyMatrix.h +++ b/shared-bindings/keypaddemux/DemuxKeyMatrix.h @@ -24,22 +24,22 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_DEMUXKEYMATRIX_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_DEMUXKEYMATRIX_H +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_DEMUX_DEMUXKEYMATRIX_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_DEMUX_DEMUXKEYMATRIX_H #include "py/objlist.h" -#include "shared-module/keypad/DemuxKeyMatrix.h" +#include "shared-module/keypaddemux/DemuxKeyMatrix.h" -extern const mp_obj_type_t keypad_demuxkeymatrix_type; +extern const mp_obj_type_t keypad_demux_demuxkeymatrix_type; -void common_hal_keypad_demuxkeymatrix_construct(keypad_demuxkeymatrix_obj_t *self, mp_uint_t num_row_addr_pins, const mcu_pin_obj_t *row_addr_pins[], mp_uint_t num_column_pins, const mcu_pin_obj_t *column_pins[], mp_float_t interval, size_t max_events); +void common_hal_keypad_demux_demuxkeymatrix_construct(keypad_demux_demuxkeymatrix_obj_t *self, mp_uint_t num_row_addr_pins, const mcu_pin_obj_t *row_addr_pins[], mp_uint_t num_column_pins, const mcu_pin_obj_t *column_pins[], mp_float_t interval, size_t max_events); -void common_hal_keypad_demuxkeymatrix_deinit(keypad_demuxkeymatrix_obj_t *self); +void common_hal_keypad_demux_demuxkeymatrix_deinit(keypad_demux_demuxkeymatrix_obj_t *self); -void common_hal_keypad_demuxkeymatrix_key_number_to_row_column(keypad_demuxkeymatrix_obj_t *self, mp_uint_t key_number, mp_uint_t *row, mp_uint_t *column); -mp_uint_t common_hal_keypad_demuxkeymatrix_row_column_to_key_number(keypad_demuxkeymatrix_obj_t *self, mp_uint_t row, mp_uint_t column); +void common_hal_keypad_demux_demuxkeymatrix_key_number_to_row_column(keypad_demux_demuxkeymatrix_obj_t *self, mp_uint_t key_number, mp_uint_t *row, mp_uint_t *column); +mp_uint_t common_hal_keypad_demux_demuxkeymatrix_row_column_to_key_number(keypad_demux_demuxkeymatrix_obj_t *self, mp_uint_t row, mp_uint_t column); -size_t common_hal_keypad_demuxkeymatrix_get_column_count(keypad_demuxkeymatrix_obj_t *self); -size_t common_hal_keypad_demuxkeymatrix_get_row_count(keypad_demuxkeymatrix_obj_t *self); +size_t common_hal_keypad_demux_demuxkeymatrix_get_column_count(keypad_demux_demuxkeymatrix_obj_t *self); +size_t common_hal_keypad_demux_demuxkeymatrix_get_row_count(keypad_demux_demuxkeymatrix_obj_t *self); -#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_DEMUXKEYMATRIX_H +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_DEMUX_DEMUXKEYMATRIX_H diff --git a/shared-bindings/keypaddemux/__init__.c b/shared-bindings/keypaddemux/__init__.c new file mode 100644 index 000000000000..c0c83a60cb05 --- /dev/null +++ b/shared-bindings/keypaddemux/__init__.c @@ -0,0 +1,52 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 CDarius + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/obj.h" + +#include "shared-bindings/keypaddemux/DemuxKeyMatrix.h" +#include "shared-bindings/util.h" + +//| """Support for scanning key matrices that use a demultiplexer +//| +//| The `keypaddemux` module provides native support to scan sets of keys or buttons, +//| connected in a row-and-column matrix. +//| +//| .. jinja +//| """ + +STATIC mp_rom_map_elem_t keypad_demux_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_keypaddemux) }, + { MP_ROM_QSTR(MP_QSTR_DemuxKeyMatrix), MP_OBJ_FROM_PTR(&keypad_demux_demuxkeymatrix_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(keypad_demux_module_globals, keypad_demux_module_globals_table); + +const mp_obj_module_t keypad_demux_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&keypad_demux_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_keypaddemux, keypad_demux_module); diff --git a/shared-module/keypad/DemuxKeyMatrix.c b/shared-module/keypaddemux/DemuxKeyMatrix.c similarity index 78% rename from shared-module/keypad/DemuxKeyMatrix.c rename to shared-module/keypaddemux/DemuxKeyMatrix.c index 3aef33084630..a343c6301d32 100644 --- a/shared-module/keypad/DemuxKeyMatrix.c +++ b/shared-module/keypaddemux/DemuxKeyMatrix.c @@ -30,7 +30,7 @@ #include "py/runtime.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/keypad/EventQueue.h" -#include "shared-bindings/keypad/DemuxKeyMatrix.h" +#include "shared-bindings/keypaddemux/DemuxKeyMatrix.h" #include "shared-bindings/keypad/__init__.h" #include "shared-bindings/supervisor/__init__.h" #include "shared-bindings/util.h" @@ -45,11 +45,11 @@ static keypad_scanner_funcs_t keymatrix_funcs = { .get_key_count = demuxkeymatrix_get_key_count, }; -static mp_uint_t row_column_to_key_number(keypad_demuxkeymatrix_obj_t *self, mp_uint_t row, mp_uint_t column) { +static mp_uint_t row_column_to_key_number(keypad_demux_demuxkeymatrix_obj_t *self, mp_uint_t row, mp_uint_t column) { return row * self->column_digitalinouts->len + column; } -void common_hal_keypad_demuxkeymatrix_construct(keypad_demuxkeymatrix_obj_t *self, mp_uint_t num_row_addr_pins, const mcu_pin_obj_t *row_addr_pins[], mp_uint_t num_column_pins, const mcu_pin_obj_t *column_pins[], mp_float_t interval, size_t max_events) { +void common_hal_keypad_demux_demuxkeymatrix_construct(keypad_demux_demuxkeymatrix_obj_t *self, mp_uint_t num_row_addr_pins, const mcu_pin_obj_t *row_addr_pins[], mp_uint_t num_column_pins, const mcu_pin_obj_t *column_pins[], mp_float_t interval, size_t max_events) { mp_obj_t row_addr_dios[num_row_addr_pins]; for (size_t row = 0; row < num_row_addr_pins; row++) { @@ -81,7 +81,7 @@ void common_hal_keypad_demuxkeymatrix_construct(keypad_demuxkeymatrix_obj_t *sel keypad_construct_common((keypad_scanner_obj_t *)self, interval, max_events); } -void common_hal_keypad_demuxkeymatrix_deinit(keypad_demuxkeymatrix_obj_t *self) { +void common_hal_keypad_demux_demuxkeymatrix_deinit(keypad_demux_demuxkeymatrix_obj_t *self) { if (common_hal_keypad_deinited(self)) { return; } @@ -101,33 +101,33 @@ void common_hal_keypad_demuxkeymatrix_deinit(keypad_demuxkeymatrix_obj_t *self) common_hal_keypad_deinit_core(self); } -size_t common_hal_keypad_demuxkeymatrix_get_row_count(keypad_demuxkeymatrix_obj_t *self) { +size_t common_hal_keypad_demux_demuxkeymatrix_get_row_count(keypad_demux_demuxkeymatrix_obj_t *self) { return 1 << self->row_addr_digitalinouts->len; } -size_t common_hal_keypad_demuxkeymatrix_get_column_count(keypad_demuxkeymatrix_obj_t *self) { +size_t common_hal_keypad_demux_demuxkeymatrix_get_column_count(keypad_demux_demuxkeymatrix_obj_t *self) { return self->column_digitalinouts->len; } -mp_uint_t common_hal_keypad_demuxkeymatrix_row_column_to_key_number(keypad_demuxkeymatrix_obj_t *self, mp_uint_t row, mp_uint_t column) { +mp_uint_t common_hal_keypad_demux_demuxkeymatrix_row_column_to_key_number(keypad_demux_demuxkeymatrix_obj_t *self, mp_uint_t row, mp_uint_t column) { return row_column_to_key_number(self, row, column); } -void common_hal_keypad_demuxkeymatrix_key_number_to_row_column(keypad_demuxkeymatrix_obj_t *self, mp_uint_t key_number, mp_uint_t *row, mp_uint_t *column) { - const size_t num_columns = common_hal_keypad_demuxkeymatrix_get_column_count(self); +void common_hal_keypad_demux_demuxkeymatrix_key_number_to_row_column(keypad_demux_demuxkeymatrix_obj_t *self, mp_uint_t key_number, mp_uint_t *row, mp_uint_t *column) { + const size_t num_columns = common_hal_keypad_demux_demuxkeymatrix_get_column_count(self); *row = key_number / num_columns; *column = key_number % num_columns; } static size_t demuxkeymatrix_get_key_count(void *self_in) { - keypad_demuxkeymatrix_obj_t *self = self_in; - return common_hal_keypad_demuxkeymatrix_get_column_count(self) * common_hal_keypad_demuxkeymatrix_get_row_count(self); + keypad_demux_demuxkeymatrix_obj_t *self = self_in; + return common_hal_keypad_demux_demuxkeymatrix_get_column_count(self) * common_hal_keypad_demux_demuxkeymatrix_get_row_count(self); } static void demuxkeymatrix_scan_now(void *self_in, mp_obj_t timestamp) { - keypad_demuxkeymatrix_obj_t *self = self_in; + keypad_demux_demuxkeymatrix_obj_t *self = self_in; - for (size_t row = 0; row < common_hal_keypad_demuxkeymatrix_get_row_count(self); row++) { + for (size_t row = 0; row < common_hal_keypad_demux_demuxkeymatrix_get_row_count(self); row++) { // Set the row address on demultiplexer size_t mask = 0b00000001; for (size_t row_addr_pin = 0; row_addr_pin < self->row_addr_digitalinouts->len; row_addr_pin++) { @@ -136,7 +136,7 @@ static void demuxkeymatrix_scan_now(void *self_in, mp_obj_t timestamp) { mask = mask << 1; } - for (size_t column = 0; column < common_hal_keypad_demuxkeymatrix_get_column_count(self); column++) { + for (size_t column = 0; column < common_hal_keypad_demux_demuxkeymatrix_get_column_count(self); column++) { mp_uint_t key_number = row_column_to_key_number(self, row, column); const bool previous = self->currently_pressed[key_number]; self->previously_pressed[key_number] = previous; diff --git a/shared-module/keypad/DemuxKeyMatrix.h b/shared-module/keypaddemux/DemuxKeyMatrix.h similarity index 82% rename from shared-module/keypad/DemuxKeyMatrix.h rename to shared-module/keypaddemux/DemuxKeyMatrix.h index b6a2955c0485..794b05781752 100644 --- a/shared-module/keypad/DemuxKeyMatrix.h +++ b/shared-module/keypaddemux/DemuxKeyMatrix.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_MODULE_KEYPADE_DEMUXKEYMATRIX_H -#define MICROPY_INCLUDED_SHARED_MODULE_KEYPADE_DEMUXKEYMATRIX_H +#ifndef MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_DEMUX_DEMUXKEYMATRIX_H +#define MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_DEMUX_DEMUXKEYMATRIX_H #include "py/obj.h" #include "py/objtuple.h" @@ -38,8 +38,8 @@ typedef struct { KEYPAD_SCANNER_COMMON_FIELDS; mp_obj_tuple_t *row_addr_digitalinouts; mp_obj_tuple_t *column_digitalinouts; -} keypad_demuxkeymatrix_obj_t; +} keypad_demux_demuxkeymatrix_obj_t; -void keypad_demuxkeymatrix_scan(keypad_demuxkeymatrix_obj_t *self); +void keypad_demux_demuxkeymatrix_scan(keypad_demux_demuxkeymatrix_obj_t *self); -#endif // MICROPY_INCLUDED_SHARED_MODULE_KEYPADE_DEMUXKEYMATRIX_H +#endif // MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_DEMUX_DEMUXKEYMATRIX_H diff --git a/shared-module/keypaddemux/__init__.c b/shared-module/keypaddemux/__init__.c new file mode 100644 index 000000000000..e69de29bb2d1