Skip to content

Commit

Permalink
Moved DemuxKeyMatrix in the keypaddemux module
Browse files Browse the repository at this point in the history
  • Loading branch information
CDarius committed Mar 18, 2024
1 parent d7bc2c2 commit 88b4e38
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 73 deletions.
2 changes: 1 addition & 1 deletion docs/shared_bindings_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion ports/atmel-samd/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions shared-bindings/keypad/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -43,7 +43,7 @@
//| <details>
//| <summary>Available on these boards</summary>
//| <ul>
//| {% for board in support_matrix_reverse["keypad.DemuxKeyMatrix"] %}
//| {% for board in support_matrix_reverse["keypaddemux.DemuxKeyMatrix"] %}
//| <li> {{ board }}
//| {% endfor %}
//| </ul>
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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."""
Expand All @@ -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();
}
Expand All @@ -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(
Expand All @@ -187,59 +187,59 @@ 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);
row_column[1] = MP_OBJ_NEW_SMALL_INT(column);

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
);
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 52 additions & 0 deletions shared-bindings/keypaddemux/__init__.c
Original file line number Diff line number Diff line change
@@ -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);
Loading

0 comments on commit 88b4e38

Please sign in to comment.