refactor keypad to share more code

This saves 444 bytes on the pygamer build (both commits taken together)

testing performed:
 * pygamer (samd51) with ShiftRegisterKeys
 * macropad (rp2040) with Keys
 * UM feather s2 (esp32-s2) with KeyMatrix

Result:
 * all ports still worked nicely
 * keys held down at start always registered (>2 trials all boards, >100 trials esp32-s2)
 * keys held down are immediately registered after reset() (>100 trials esp32-s2)
 * double .reset() is OK, accessing .events throws (only tested esp32-s2)
This commit is contained in:
Jeff Epler 2022-04-05 11:08:44 -05:00
parent 203dad11bf
commit d7c86c0e3e
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
18 changed files with 212 additions and 380 deletions

View File

@ -4067,63 +4067,8 @@ msgstr ""
msgid "pow() with 3 arguments requires integers" msgid "pow() with 3 arguments requires integers"
msgstr "" msgstr ""
#: ports/espressif/boards/adafruit_esp32s2_camera/mpconfigboard.h #: ports/espressif/boards/adafruit_qtpy_esp32c3/mpconfigboard.h
#: ports/espressif/boards/adafruit_feather_esp32s2/mpconfigboard.h #: supervisor/shared/safe_mode.c
#: ports/espressif/boards/adafruit_feather_esp32s2_tft/mpconfigboard.h
#: ports/espressif/boards/adafruit_feather_esp32s2_tftback_nopsram/mpconfigboard.h
#: ports/espressif/boards/adafruit_funhouse/mpconfigboard.h
#: ports/espressif/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
#: ports/espressif/boards/adafruit_metro_esp32s2/mpconfigboard.h
#: ports/espressif/boards/adafruit_qtpy_esp32s2/mpconfigboard.h
#: ports/espressif/boards/adafruit_qtpy_esp32s3_nopsram/mpconfigboard.h
#: ports/espressif/boards/ai_thinker_esp32-c3s-2m/mpconfigboard.h
#: ports/espressif/boards/ai_thinker_esp32-c3s/mpconfigboard.h
#: ports/espressif/boards/ai_thinker_esp_12k_nodemcu/mpconfigboard.h
#: ports/espressif/boards/artisense_rd00/mpconfigboard.h
#: ports/espressif/boards/atmegazero_esp32s2/mpconfigboard.h
#: ports/espressif/boards/crumpspace_crumps2/mpconfigboard.h
#: ports/espressif/boards/electroniccats_bastwifi/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32c3_devkitm_1_n4/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s2_devkitc_1_n4/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s2_devkitc_1_n4r2/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s3_box/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s3_devkitc_1_n8/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s3_devkitc_1_n8r2/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s3_devkitc_1_n8r8/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s3_devkitm_1_n8/mpconfigboard.h
#: ports/espressif/boards/espressif_hmi_devkit_1/mpconfigboard.h
#: ports/espressif/boards/espressif_kaluga_1.3/mpconfigboard.h
#: ports/espressif/boards/espressif_kaluga_1/mpconfigboard.h
#: ports/espressif/boards/espressif_saola_1_wroom/mpconfigboard.h
#: ports/espressif/boards/espressif_saola_1_wrover/mpconfigboard.h
#: ports/espressif/boards/franzininho_wifi_wroom/mpconfigboard.h
#: ports/espressif/boards/franzininho_wifi_wrover/mpconfigboard.h
#: ports/espressif/boards/gravitech_cucumber_m/mpconfigboard.h
#: ports/espressif/boards/gravitech_cucumber_ms/mpconfigboard.h
#: ports/espressif/boards/gravitech_cucumber_r/mpconfigboard.h
#: ports/espressif/boards/gravitech_cucumber_rs/mpconfigboard.h
#: ports/espressif/boards/hexky_s2/mpconfigboard.h
#: ports/espressif/boards/hiibot_iots2/mpconfigboard.h
#: ports/espressif/boards/lilygo_ttgo_t8_esp32_s2_wroom/mpconfigboard.h
#: ports/espressif/boards/lilygo_ttgo_t8_s2/mpconfigboard.h
#: ports/espressif/boards/lilygo_ttgo_t8_s2_st7789/mpconfigboard.h
#: ports/espressif/boards/lolin_s2_mini/mpconfigboard.h
#: ports/espressif/boards/lolin_s2_pico/mpconfigboard.h
#: ports/espressif/boards/microdev_micro_c3/mpconfigboard.h
#: ports/espressif/boards/microdev_micro_s2/mpconfigboard.h
#: ports/espressif/boards/morpheans_morphesp-240/mpconfigboard.h
#: ports/espressif/boards/muselab_nanoesp32_s2_wroom/mpconfigboard.h
#: ports/espressif/boards/muselab_nanoesp32_s2_wrover/mpconfigboard.h
#: ports/espressif/boards/odt_pixelwing_esp32_s2/mpconfigboard.h
#: ports/espressif/boards/targett_module_clip_wroom/mpconfigboard.h
#: ports/espressif/boards/targett_module_clip_wrover/mpconfigboard.h
#: ports/espressif/boards/unexpectedmaker_feathers2/mpconfigboard.h
#: ports/espressif/boards/unexpectedmaker_feathers2_neo/mpconfigboard.h
#: ports/espressif/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.h
#: ports/espressif/boards/unexpectedmaker_feathers3/mpconfigboard.h
#: ports/espressif/boards/unexpectedmaker_pros3/mpconfigboard.h
#: ports/espressif/boards/unexpectedmaker_tinys2/mpconfigboard.h
#: ports/espressif/boards/unexpectedmaker_tinys3/mpconfigboard.h
msgid "pressing boot button at start up.\n" msgid "pressing boot button at start up.\n"
msgstr "" msgstr ""

View File

@ -28,6 +28,7 @@
#include "py/binary.h" #include "py/binary.h"
#include "py/objproperty.h" #include "py/objproperty.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/keypad/__init__.h"
#include "shared-bindings/keypad/Event.h" #include "shared-bindings/keypad/Event.h"
#include "shared-bindings/keypad/KeyMatrix.h" #include "shared-bindings/keypad/KeyMatrix.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
@ -138,7 +139,7 @@ STATIC mp_obj_t keypad_keymatrix___exit__(size_t n_args, const mp_obj_t *args) {
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_keymatrix___exit___obj, 4, 4, keypad_keymatrix___exit__); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_keymatrix___exit___obj, 4, 4, keypad_keymatrix___exit__);
STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) { STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) {
if (common_hal_keypad_keymatrix_deinited(self)) { if (common_hal_keypad_deinited(self)) {
raise_deinited_error(); raise_deinited_error();
} }
} }
@ -150,33 +151,11 @@ STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) {
//| """ //| """
//| ... //| ...
//| //|
STATIC mp_obj_t keypad_keymatrix_reset(mp_obj_t self_in) {
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_keypad_keymatrix_reset(self);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_reset_obj, keypad_keymatrix_reset);
//| key_count: int //| key_count: int
//| """The number of keys that are being scanned. (read-only) //| """The number of keys that are being scanned. (read-only)
//| """ //| """
//| //|
STATIC mp_obj_t keypad_keymatrix_get_key_count(mp_obj_t self_in) {
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_keymatrix_get_key_count(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_get_key_count_obj, keypad_keymatrix_get_key_count);
const mp_obj_property_t keypad_keymatrix_key_count_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_keymatrix_get_key_count_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
//| def key_number_to_row_column(self, row: int, column: int) -> Tuple[int]: //| def key_number_to_row_column(self, row: int, column: int) -> Tuple[int]:
//| """Return the row and column for the given key number. //| """Return the row and column for the given key number.
@ -194,7 +173,7 @@ STATIC mp_obj_t keypad_keymatrix_key_number_to_row_column(mp_obj_t self_in, mp_o
const mp_uint_t key_number = (mp_uint_t)mp_arg_validate_int_range( const mp_uint_t key_number = (mp_uint_t)mp_arg_validate_int_range(
mp_obj_get_int(key_number_in), mp_obj_get_int(key_number_in),
0, (mp_int_t)common_hal_keypad_keymatrix_get_key_count(self), 0, (mp_int_t)common_hal_keypad_generic_get_key_count(self),
MP_QSTR_key_number); MP_QSTR_key_number);
mp_uint_t row; mp_uint_t row;
@ -234,29 +213,15 @@ MP_DEFINE_CONST_FUN_OBJ_3(keypad_keymatrix_row_column_to_key_number_obj, keypad_
//| """The `EventQueue` associated with this `Keys` object. (read-only) //| """The `EventQueue` associated with this `Keys` object. (read-only)
//| """ //| """
//| //|
STATIC mp_obj_t keypad_keymatrix_get_events(mp_obj_t self_in) {
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_keypad_keymatrix_get_events(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_get_events_obj, keypad_keymatrix_get_events);
const mp_obj_property_t keypad_keymatrix_events_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_keymatrix_get_events_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
STATIC const mp_rom_map_elem_t keypad_keymatrix_locals_dict_table[] = { STATIC const mp_rom_map_elem_t keypad_keymatrix_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_keymatrix_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_keymatrix_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_keymatrix___exit___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_keymatrix___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_keymatrix_events_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_keymatrix_key_count_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_keymatrix_reset_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_keymatrix_key_number_to_row_column_obj) }, { MP_ROM_QSTR(MP_QSTR_key_number_to_row_column), MP_ROM_PTR(&keypad_keymatrix_key_number_to_row_column_obj) },
{ MP_ROM_QSTR(MP_QSTR_row_column_to_key_number), MP_ROM_PTR(&keypad_keymatrix_row_column_to_key_number_obj) }, { MP_ROM_QSTR(MP_QSTR_row_column_to_key_number), MP_ROM_PTR(&keypad_keymatrix_row_column_to_key_number_obj) },
}; };

View File

@ -35,16 +35,11 @@ extern const mp_obj_type_t keypad_keymatrix_type;
void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint_t num_row_pins, const mcu_pin_obj_t *row_pins[], mp_uint_t num_column_pins, const mcu_pin_obj_t *column_pins[], bool columns_to_anodes, mp_float_t interval, size_t max_events); void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint_t num_row_pins, const mcu_pin_obj_t *row_pins[], mp_uint_t num_column_pins, const mcu_pin_obj_t *column_pins[], bool columns_to_anodes, mp_float_t interval, size_t max_events);
void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self); void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self);
bool common_hal_keypad_keymatrix_deinited(keypad_keymatrix_obj_t *self);
void common_hal_keypad_keymatrix_key_number_to_row_column(keypad_keymatrix_obj_t *self, mp_uint_t key_number, mp_uint_t *row, mp_uint_t *column); void common_hal_keypad_keymatrix_key_number_to_row_column(keypad_keymatrix_obj_t *self, mp_uint_t key_number, mp_uint_t *row, mp_uint_t *column);
mp_uint_t common_hal_keypad_keymatrix_row_column_to_key_number(keypad_keymatrix_obj_t *self, mp_uint_t row, mp_uint_t column); mp_uint_t common_hal_keypad_keymatrix_row_column_to_key_number(keypad_keymatrix_obj_t *self, mp_uint_t row, mp_uint_t column);
size_t common_hal_keypad_keymatrix_get_key_count(keypad_keymatrix_obj_t *self);
size_t common_hal_keypad_keymatrix_get_column_count(keypad_keymatrix_obj_t *self); size_t common_hal_keypad_keymatrix_get_column_count(keypad_keymatrix_obj_t *self);
size_t common_hal_keypad_keymatrix_get_row_count(keypad_keymatrix_obj_t *self); size_t common_hal_keypad_keymatrix_get_row_count(keypad_keymatrix_obj_t *self);
mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self);
void common_hal_keypad_keymatrix_reset(keypad_keymatrix_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYMATRIX_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYMATRIX_H

View File

@ -28,6 +28,7 @@
#include "py/binary.h" #include "py/binary.h"
#include "py/objproperty.h" #include "py/objproperty.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/keypad/__init__.h"
#include "shared-bindings/keypad/Event.h" #include "shared-bindings/keypad/Event.h"
#include "shared-bindings/keypad/Keys.h" #include "shared-bindings/keypad/Keys.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
@ -129,11 +130,6 @@ STATIC mp_obj_t keypad_keys___exit__(size_t n_args, const mp_obj_t *args) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_keys___exit___obj, 4, 4, keypad_keys___exit__); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_keys___exit___obj, 4, 4, keypad_keys___exit__);
STATIC void check_for_deinit(keypad_keys_obj_t *self) {
if (common_hal_keypad_keys_deinited(self)) {
raise_deinited_error();
}
}
//| def reset(self) -> None: //| def reset(self) -> None:
//| """Reset the internal state of the scanner to assume that all keys are now released. //| """Reset the internal state of the scanner to assume that all keys are now released.
@ -142,61 +138,24 @@ STATIC void check_for_deinit(keypad_keys_obj_t *self) {
//| """ //| """
//| ... //| ...
//| //|
STATIC mp_obj_t keypad_keys_reset(mp_obj_t self_in) {
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_keypad_keys_reset(self);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_reset_obj, keypad_keys_reset);
//| key_count: int //| key_count: int
//| """The number of keys that are being scanned. (read-only) //| """The number of keys that are being scanned. (read-only)
//| """ //| """
//| //|
STATIC mp_obj_t keypad_keys_get_key_count(mp_obj_t self_in) {
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_keys_get_key_count(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_get_key_count_obj, keypad_keys_get_key_count);
const mp_obj_property_t keypad_keys_key_count_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_keys_get_key_count_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
//| events: EventQueue //| events: EventQueue
//| """The `EventQueue` associated with this `Keys` object. (read-only) //| """The `EventQueue` associated with this `Keys` object. (read-only)
//| """ //| """
//| //|
STATIC mp_obj_t keypad_keys_get_events(mp_obj_t self_in) {
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_keypad_keys_get_events(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_get_events_obj, keypad_keys_get_events);
const mp_obj_property_t keypad_keys_events_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_keys_get_events_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
STATIC const mp_rom_map_elem_t keypad_keys_locals_dict_table[] = { STATIC const mp_rom_map_elem_t keypad_keys_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_keys_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_keys_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_keys___exit___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_keys___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_keys_events_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_keys_key_count_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_keys_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_generic_reset_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(keypad_keys_locals_dict, keypad_keys_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(keypad_keys_locals_dict, keypad_keys_locals_dict_table);

View File

@ -35,10 +35,5 @@ extern const mp_obj_type_t keypad_keys_type;
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, const mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull, mp_float_t interval, size_t max_events); void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, const mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull, mp_float_t interval, size_t max_events);
void common_hal_keypad_keys_deinit(keypad_keys_obj_t *self); void common_hal_keypad_keys_deinit(keypad_keys_obj_t *self);
bool common_hal_keypad_keys_deinited(keypad_keys_obj_t *self);
mp_obj_t common_hal_keypad_keys_get_events(keypad_keys_obj_t *self);
size_t common_hal_keypad_keys_get_key_count(keypad_keys_obj_t *self);
void common_hal_keypad_keys_reset(keypad_keys_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H

View File

@ -28,6 +28,7 @@
#include "py/binary.h" #include "py/binary.h"
#include "py/objproperty.h" #include "py/objproperty.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/keypad/__init__.h"
#include "shared-bindings/keypad/Event.h" #include "shared-bindings/keypad/Event.h"
#include "shared-bindings/keypad/ShiftRegisterKeys.h" #include "shared-bindings/keypad/ShiftRegisterKeys.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
@ -133,12 +134,6 @@ STATIC mp_obj_t keypad_shiftregisterkeys___exit__(size_t n_args, const mp_obj_t
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_shiftregisterkeys___exit___obj, 4, 4, keypad_shiftregisterkeys___exit__); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_shiftregisterkeys___exit___obj, 4, 4, keypad_shiftregisterkeys___exit__);
STATIC void check_for_deinit(keypad_shiftregisterkeys_obj_t *self) {
if (common_hal_keypad_shiftregisterkeys_deinited(self)) {
raise_deinited_error();
}
}
//| def reset(self) -> None: //| def reset(self) -> None:
//| """Reset the internal state of the scanner to assume that all keys are now released. //| """Reset the internal state of the scanner to assume that all keys are now released.
//| Any key that is already pressed at the time of this call will therefore immediately cause //| Any key that is already pressed at the time of this call will therefore immediately cause
@ -146,61 +141,25 @@ STATIC void check_for_deinit(keypad_shiftregisterkeys_obj_t *self) {
//| """ //| """
//| ... //| ...
//| //|
STATIC mp_obj_t keypad_shiftregisterkeys_reset(mp_obj_t self_in) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_keypad_shiftregisterkeys_reset(self);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_shiftregisterkeys_reset_obj, keypad_shiftregisterkeys_reset);
//| key_count: int //| key_count: int
//| """The number of keys that are being scanned. (read-only) //| """The number of keys that are being scanned. (read-only)
//| """ //| """
//| //|
STATIC mp_obj_t keypad_shiftregisterkeys_get_key_count(mp_obj_t self_in) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_shiftregisterkeys_get_key_count(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_shiftregisterkeys_get_key_count_obj, keypad_shiftregisterkeys_get_key_count);
const mp_obj_property_t keypad_shiftregisterkeys_key_count_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_shiftregisterkeys_get_key_count_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
//| events: EventQueue //| events: EventQueue
//| """The `EventQueue` associated with this `Keys` object. (read-only) //| """The `EventQueue` associated with this `Keys` object. (read-only)
//| """ //| """
//| //|
STATIC mp_obj_t keypad_shiftregisterkeys_get_events(mp_obj_t self_in) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_keypad_shiftregisterkeys_get_events(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_shiftregisterkeys_get_events_obj, keypad_shiftregisterkeys_get_events);
const mp_obj_property_t keypad_shiftregisterkeys_events_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_shiftregisterkeys_get_events_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
STATIC const mp_rom_map_elem_t keypad_shiftregisterkeys_locals_dict_table[] = { STATIC const mp_rom_map_elem_t keypad_shiftregisterkeys_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_shiftregisterkeys_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_shiftregisterkeys_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_shiftregisterkeys___exit___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_shiftregisterkeys___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_shiftregisterkeys_events_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_shiftregisterkeys_key_count_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_shiftregisterkeys_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_generic_reset_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(keypad_shiftregisterkeys_locals_dict, keypad_shiftregisterkeys_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(keypad_shiftregisterkeys_locals_dict, keypad_shiftregisterkeys_locals_dict_table);

View File

@ -35,10 +35,5 @@ extern const mp_obj_type_t keypad_shiftregisterkeys_type;
void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_t *self, const mcu_pin_obj_t *clock_pin, const mcu_pin_obj_t *data_pin, const mcu_pin_obj_t *latch_pin, bool value_to_latch, size_t key_count, bool value_when_pressed, mp_float_t interval, size_t max_events); void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_t *self, const mcu_pin_obj_t *clock_pin, const mcu_pin_obj_t *data_pin, const mcu_pin_obj_t *latch_pin, bool value_to_latch, size_t key_count, bool value_when_pressed, mp_float_t interval, size_t max_events);
void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self); void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self);
bool common_hal_keypad_shiftregisterkeys_deinited(keypad_shiftregisterkeys_obj_t *self);
mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys_obj_t *self);
size_t common_hal_keypad_shiftregisterkeys_get_key_count(keypad_shiftregisterkeys_obj_t *self);
void common_hal_keypad_shiftregisterkeys_reset(keypad_shiftregisterkeys_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_SHIFTREGISTERKEYS_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_SHIFTREGISTERKEYS_H

View File

@ -32,6 +32,53 @@
#include "shared-bindings/keypad/KeyMatrix.h" #include "shared-bindings/keypad/KeyMatrix.h"
#include "shared-bindings/keypad/Keys.h" #include "shared-bindings/keypad/Keys.h"
#include "shared-bindings/keypad/ShiftRegisterKeys.h" #include "shared-bindings/keypad/ShiftRegisterKeys.h"
#include "shared-bindings/util.h"
STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) {
if (common_hal_keypad_deinited(self)) {
raise_deinited_error();
}
}
STATIC mp_obj_t keypad_generic_reset(mp_obj_t self_in) {
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_keypad_generic_reset(self);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_generic_reset_obj, keypad_generic_reset);
STATIC mp_obj_t keypad_generic_get_key_count(mp_obj_t self_in) {
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_generic_get_key_count(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_generic_get_key_count_obj, keypad_generic_get_key_count);
const mp_obj_property_t keypad_generic_key_count_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_generic_get_key_count_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
STATIC mp_obj_t keypad_generic_get_events(mp_obj_t self_in) {
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_keypad_generic_get_events(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_generic_get_events_obj, keypad_generic_get_events);
const mp_obj_property_t keypad_generic_events_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_generic_get_events_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
//| """Support for scanning keys and key matrices //| """Support for scanning keys and key matrices
//| //|

View File

@ -28,14 +28,17 @@
#define SHARED_BINDINGS_KEYPAD_H #define SHARED_BINDINGS_KEYPAD_H
#include "py/obj.h" #include "py/obj.h"
#include "py/objtuple.h" #include "py/objproperty.h"
#include "shared-module/keypad/__init__.h" #include "shared-module/keypad/__init__.h"
extern mp_obj_tuple_t common_hal_keypad_devices; bool common_hal_keypad_deinited(void *self);
void common_hal_keypad_generic_reset(void *self);
size_t common_hal_keypad_generic_get_key_count(void *self);
mp_obj_t common_hal_keypad_generic_get_events(void *self);
void keypad_set_devices(mp_obj_t devices); MP_DECLARE_CONST_FUN_OBJ_1(keypad_generic_reset_obj);
bool common_hal_keypad_disable(void); extern const mp_obj_property_t keypad_generic_events_obj;
bool common_hal_keypad_enable(const mp_obj_t devices_seq); extern const mp_obj_property_t keypad_generic_key_count_obj;
#endif // SHARED_BINDINGS_KEYPAD_H #endif // SHARED_BINDINGS_KEYPAD_H

View File

@ -30,7 +30,7 @@
#include "py/obj.h" #include "py/obj.h"
#include "py/ringbuf.h" #include "py/ringbuf.h"
typedef struct { typedef struct _keypad_eventqueue_obj_t {
mp_obj_base_t base; mp_obj_base_t base;
ringbuf_t encoded_events; ringbuf_t encoded_events;
bool overflowed; bool overflowed;

View File

@ -37,7 +37,13 @@
#include "supervisor/port.h" #include "supervisor/port.h"
#include "supervisor/shared/tick.h" #include "supervisor/shared/tick.h"
static void keypad_keymatrix_scan_now(keypad_keymatrix_obj_t *self, uint64_t now); static void keymatrix_scan_now(void *self_in, mp_obj_t timestamp);
static size_t keymatrix_get_key_count(void *self_in);
static keypad_scanner_funcs_t keymatrix_funcs = {
.scan_now = keymatrix_scan_now,
.get_key_count = keymatrix_get_key_count,
};
static mp_uint_t row_column_to_key_number(keypad_keymatrix_obj_t *self, mp_uint_t row, mp_uint_t column) { static mp_uint_t row_column_to_key_number(keypad_keymatrix_obj_t *self, mp_uint_t row, mp_uint_t column) {
return row * self->column_digitalinouts->len + column; return row * self->column_digitalinouts->len + column;
@ -69,21 +75,13 @@ void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_row_pins * num_column_pins, false, false); self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_row_pins * num_column_pins, false, false);
self->columns_to_anodes = columns_to_anodes; self->columns_to_anodes = columns_to_anodes;
self->funcs = &keymatrix_funcs;
keypad_eventqueue_obj_t *events = m_new_obj(keypad_eventqueue_obj_t); keypad_construct_common((keypad_scanner_obj_t *)self, interval, max_events);
events->base.type = &keypad_eventqueue_type;
common_hal_keypad_eventqueue_construct(events, max_events);
self->events = events;
self->interval_ticks = (mp_uint_t)(interval * 1024); // interval * 1000 * (1024/1000)
// Add self to the list of active keypad scanners.
keypad_register_scanner((keypad_scanner_obj_t *)self);
keypad_keymatrix_scan_now(self, port_get_raw_ticks(NULL));
} }
void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self) { void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self) {
if (common_hal_keypad_keymatrix_deinited(self)) { if (common_hal_keypad_deinited(self)) {
return; return;
} }
@ -99,14 +97,7 @@ void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self) {
common_hal_digitalio_digitalinout_deinit(self->column_digitalinouts->items[column]); common_hal_digitalio_digitalinout_deinit(self->column_digitalinouts->items[column]);
} }
self->column_digitalinouts = MP_ROM_NONE; self->column_digitalinouts = MP_ROM_NONE;
} common_hal_keypad_deinit_core(self);
bool common_hal_keypad_keymatrix_deinited(keypad_keymatrix_obj_t *self) {
return self->row_digitalinouts == MP_ROM_NONE;
}
size_t common_hal_keypad_keymatrix_get_key_count(keypad_keymatrix_obj_t *self) {
return common_hal_keypad_keymatrix_get_row_count(self) * common_hal_keypad_keymatrix_get_column_count(self);
} }
size_t common_hal_keypad_keymatrix_get_row_count(keypad_keymatrix_obj_t *self) { size_t common_hal_keypad_keymatrix_get_row_count(keypad_keymatrix_obj_t *self) {
@ -127,32 +118,13 @@ void common_hal_keypad_keymatrix_key_number_to_row_column(keypad_keymatrix_obj_t
*column = key_number % num_columns; *column = key_number % num_columns;
} }
mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self) { static size_t keymatrix_get_key_count(void *self_in) {
return MP_OBJ_FROM_PTR(self->events); keypad_keymatrix_obj_t *self = self_in;
return common_hal_keypad_keymatrix_get_column_count(self) * common_hal_keypad_keymatrix_get_row_count(self);
} }
void common_hal_keypad_keymatrix_reset(keypad_keymatrix_obj_t *self) { static void keymatrix_scan_now(void *self_in, mp_obj_t timestamp) {
const size_t key_count = common_hal_keypad_keymatrix_get_key_count(self); keypad_keymatrix_obj_t *self = self_in;
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
keypad_keymatrix_scan_now(self, port_get_raw_ticks(NULL));
}
void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
uint64_t now = port_get_raw_ticks(NULL);
if (now - self->last_scan_ticks < self->interval_ticks) {
// Too soon. Wait longer to debounce.
return;
}
keypad_keymatrix_scan_now(self, now);
}
static void keypad_keymatrix_scan_now(keypad_keymatrix_obj_t *self, uint64_t now) {
self->last_scan_ticks = now;
mp_obj_t timestamp = supervisor_ticks_ms();
// On entry, all pins are set to inputs with a pull-up or pull-down, // On entry, all pins are set to inputs with a pull-up or pull-down,
// depending on the diode orientation. // depending on the diode orientation.

View File

@ -35,16 +35,9 @@
#include "shared-module/keypad/EventQueue.h" #include "shared-module/keypad/EventQueue.h"
typedef struct { typedef struct {
mp_obj_base_t base; KEYPAD_SCANNER_COMMON_FIELDS;
// All scanners have a next field here, to keep a linked list of active scanners.
keypad_scanner_obj_t *next;
mp_obj_tuple_t *row_digitalinouts; mp_obj_tuple_t *row_digitalinouts;
mp_obj_tuple_t *column_digitalinouts; mp_obj_tuple_t *column_digitalinouts;
mp_uint_t interval_ticks;
uint64_t last_scan_ticks;
bool *previously_pressed;
bool *currently_pressed;
keypad_eventqueue_obj_t *events;
bool columns_to_anodes; bool columns_to_anodes;
} keypad_keymatrix_obj_t; } keypad_keymatrix_obj_t;

View File

@ -36,7 +36,13 @@
#include "supervisor/port.h" #include "supervisor/port.h"
#include "supervisor/shared/tick.h" #include "supervisor/shared/tick.h"
static void keypad_keys_scan_now(keypad_keys_obj_t *self, uint64_t now); static void keypad_keys_scan_now(void *self_in, mp_obj_t timestamp);
static size_t keys_get_key_count(void *self_in);
static keypad_scanner_funcs_t keys_funcs = {
.scan_now = keypad_keys_scan_now,
.get_key_count = keys_get_key_count,
};
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, const mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull, mp_float_t interval, size_t max_events) { void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, const mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull, mp_float_t interval, size_t max_events) {
mp_obj_t dios[num_pins]; mp_obj_t dios[num_pins];
@ -55,71 +61,36 @@ void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pin
self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false); self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false);
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false); self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false);
self->value_when_pressed = value_when_pressed; self->value_when_pressed = value_when_pressed;
self->funcs = &keys_funcs;
self->interval_ticks = (mp_uint_t)(interval * 1024); // interval * 1000 * (1024/1000) keypad_construct_common((keypad_scanner_obj_t *)self, interval, max_events);
self->last_scan_ticks = port_get_raw_ticks(NULL);
keypad_eventqueue_obj_t *events = m_new_obj(keypad_eventqueue_obj_t);
events->base.type = &keypad_eventqueue_type;
common_hal_keypad_eventqueue_construct(events, max_events);
self->events = events;
// Add self to the list of active keypad scanners.
keypad_register_scanner((keypad_scanner_obj_t *)self);
keypad_keys_scan_now(self, port_get_raw_ticks(NULL));
} }
void common_hal_keypad_keys_deinit(keypad_keys_obj_t *self) { void common_hal_keypad_keys_deinit(keypad_keys_obj_t *self) {
if (common_hal_keypad_keys_deinited(self)) { if (common_hal_keypad_deinited(self)) {
return; return;
} }
// Remove self from the list of active keypad scanners first. // Remove self from the list of active keypad scanners first.
keypad_deregister_scanner((keypad_scanner_obj_t *)self); keypad_deregister_scanner((keypad_scanner_obj_t *)self);
for (size_t key = 0; key < common_hal_keypad_keys_get_key_count(self); key++) { for (size_t key = 0; key < keys_get_key_count(self); key++) {
common_hal_digitalio_digitalinout_deinit(self->digitalinouts->items[key]); common_hal_digitalio_digitalinout_deinit(self->digitalinouts->items[key]);
} }
self->digitalinouts = MP_ROM_NONE; self->digitalinouts = MP_ROM_NONE;
common_hal_keypad_deinit_core(self);
} }
bool common_hal_keypad_keys_deinited(keypad_keys_obj_t *self) { size_t keys_get_key_count(void *self_in) {
return self->digitalinouts == MP_ROM_NONE; keypad_keys_obj_t *self = self_in;
}
size_t common_hal_keypad_keys_get_key_count(keypad_keys_obj_t *self) {
return self->digitalinouts->len; return self->digitalinouts->len;
} }
mp_obj_t common_hal_keypad_keys_get_events(keypad_keys_obj_t *self) { static void keypad_keys_scan_now(void *self_in, mp_obj_t timestamp) {
return MP_OBJ_FROM_PTR(self->events); keypad_keys_obj_t *self = self_in;
} size_t key_count = keys_get_key_count(self);
void common_hal_keypad_keys_reset(keypad_keys_obj_t *self) {
const size_t key_count = common_hal_keypad_keys_get_key_count(self);
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
keypad_keys_scan_now(self, port_get_raw_ticks(NULL));
}
void keypad_keys_scan(keypad_keys_obj_t *self) {
uint64_t now = port_get_raw_ticks(NULL);
if (now - self->last_scan_ticks < self->interval_ticks) {
// Too soon. Wait longer to debounce.
return;
}
keypad_keys_scan_now(self, now);
}
static void keypad_keys_scan_now(keypad_keys_obj_t *self, uint64_t now) {
self->last_scan_ticks = now;
const size_t key_count = common_hal_keypad_keys_get_key_count(self);
mp_obj_t timestamp = supervisor_ticks_ms();
for (mp_uint_t key_number = 0; key_number < key_count; key_number++) { for (mp_uint_t key_number = 0; key_number < key_count; key_number++) {
// Remember the previous up/down state. // Remember the previous up/down state.

View File

@ -35,15 +35,8 @@
#include "shared-module/keypad/EventQueue.h" #include "shared-module/keypad/EventQueue.h"
typedef struct { typedef struct {
mp_obj_base_t base; KEYPAD_SCANNER_COMMON_FIELDS;
// All scanners have a next field here, to keep a linked list of active scanners.
keypad_scanner_obj_t *next;
mp_obj_tuple_t *digitalinouts; mp_obj_tuple_t *digitalinouts;
mp_uint_t interval_ticks;
uint64_t last_scan_ticks;
bool *previously_pressed;
bool *currently_pressed;
keypad_eventqueue_obj_t *events;
bool value_when_pressed; bool value_when_pressed;
} keypad_keys_obj_t; } keypad_keys_obj_t;

View File

@ -36,7 +36,13 @@
#include "supervisor/port.h" #include "supervisor/port.h"
#include "supervisor/shared/tick.h" #include "supervisor/shared/tick.h"
static void keypad_shiftregisterkeys_scan_now(keypad_shiftregisterkeys_obj_t *self, uint64_t now); static void shiftregisterkeys_scan_now(void *self, mp_obj_t timestamp);
static size_t shiftregisterkeys_get_key_count(void *self);
static keypad_scanner_funcs_t shiftregisterkeys_funcs = {
.scan_now = shiftregisterkeys_scan_now,
.get_key_count = shiftregisterkeys_get_key_count,
};
void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_t *self, const mcu_pin_obj_t *clock_pin, const mcu_pin_obj_t *data_pin, const mcu_pin_obj_t *latch_pin, bool value_to_latch, size_t key_count, bool value_when_pressed, mp_float_t interval, size_t max_events) { void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_t *self, const mcu_pin_obj_t *clock_pin, const mcu_pin_obj_t *data_pin, const mcu_pin_obj_t *latch_pin, bool value_to_latch, size_t key_count, bool value_when_pressed, mp_float_t interval, size_t max_events) {
@ -54,30 +60,21 @@ void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_
digitalio_digitalinout_obj_t *latch = m_new_obj(digitalio_digitalinout_obj_t); digitalio_digitalinout_obj_t *latch = m_new_obj(digitalio_digitalinout_obj_t);
latch->base.type = &digitalio_digitalinout_type; latch->base.type = &digitalio_digitalinout_type;
common_hal_digitalio_digitalinout_construct(latch, latch_pin); common_hal_digitalio_digitalinout_construct(latch, latch_pin);
common_hal_digitalio_digitalinout_switch_to_output(latch, true, DRIVE_MODE_PUSH_PULL); common_hal_digitalio_digitalinout_switch_to_output(latch, true, DRIVE_MODE_PUSH_PULL);
self->latch = latch; self->latch = latch;
self->value_to_latch = value_to_latch; self->value_to_latch = value_to_latch;
self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * key_count, false, false);
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * key_count, false, false);
self->value_when_pressed = value_when_pressed; self->value_when_pressed = value_when_pressed;
self->key_count = key_count; self->key_count = key_count;
self->funcs = &shiftregisterkeys_funcs;
self->interval_ticks = (mp_uint_t)(interval * 1024); // interval * 1000 * (1024/1000) keypad_construct_common((keypad_scanner_obj_t *)self, interval, max_events);
keypad_eventqueue_obj_t *events = m_new_obj(keypad_eventqueue_obj_t);
events->base.type = &keypad_eventqueue_type;
common_hal_keypad_eventqueue_construct(events, max_events);
self->events = events;
// Add self to the list of active keypad scanners.
keypad_register_scanner((keypad_scanner_obj_t *)self);
keypad_shiftregisterkeys_scan_now(self, port_get_raw_ticks(NULL));
} }
void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self) { void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self) {
if (common_hal_keypad_shiftregisterkeys_deinited(self)) { if (common_hal_keypad_deinited(self)) {
return; return;
} }
@ -93,47 +90,22 @@ void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *
common_hal_digitalio_digitalinout_deinit(self->latch); common_hal_digitalio_digitalinout_deinit(self->latch);
self->latch = MP_ROM_NONE; self->latch = MP_ROM_NONE;
common_hal_keypad_deinit_core(self);
} }
bool common_hal_keypad_shiftregisterkeys_deinited(keypad_shiftregisterkeys_obj_t *self) { size_t shiftregisterkeys_get_key_count(void *self_in) {
return self->clock == MP_ROM_NONE; keypad_shiftregisterkeys_obj_t *self = self_in;
}
size_t common_hal_keypad_shiftregisterkeys_get_key_count(keypad_shiftregisterkeys_obj_t *self) {
return self->key_count; return self->key_count;
} }
mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys_obj_t *self) { static void shiftregisterkeys_scan_now(void *self_in, mp_obj_t timestamp) {
return MP_OBJ_FROM_PTR(self->events); keypad_shiftregisterkeys_obj_t *self = self_in;
}
void common_hal_keypad_shiftregisterkeys_reset(keypad_shiftregisterkeys_obj_t *self) {
const size_t key_count = common_hal_keypad_shiftregisterkeys_get_key_count(self);
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
keypad_shiftregisterkeys_scan_now(self, port_get_raw_ticks(NULL));
}
void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self) {
uint64_t now = port_get_raw_ticks(NULL);
if (now - self->last_scan_ticks < self->interval_ticks) {
// Too soon. Wait longer to debounce.
return;
}
keypad_shiftregisterkeys_scan_now(self, now);
}
static void keypad_shiftregisterkeys_scan_now(keypad_shiftregisterkeys_obj_t *self, uint64_t now) {
self->last_scan_ticks = now;
mp_obj_t timestamp = supervisor_ticks_ms();
// Latch (freeze) the current state of the input pins. // Latch (freeze) the current state of the input pins.
common_hal_digitalio_digitalinout_set_value(self->latch, self->value_to_latch); common_hal_digitalio_digitalinout_set_value(self->latch, self->value_to_latch);
const size_t key_count = common_hal_keypad_shiftregisterkeys_get_key_count(self); const size_t key_count = shiftregisterkeys_get_key_count(self);
for (mp_uint_t key_number = 0; key_number < key_count; key_number++) { for (mp_uint_t key_number = 0; key_number < key_count; key_number++) {
// Zero-th data appears on on the data pin immediately, without shifting. // Zero-th data appears on on the data pin immediately, without shifting.

View File

@ -35,18 +35,11 @@
#include "shared-module/keypad/EventQueue.h" #include "shared-module/keypad/EventQueue.h"
typedef struct { typedef struct {
mp_obj_base_t base; KEYPAD_SCANNER_COMMON_FIELDS;
// All scanners have a next field here, to keep a linked list of active scanners.
keypad_scanner_obj_t *next;
digitalio_digitalinout_obj_t *clock; digitalio_digitalinout_obj_t *clock;
digitalio_digitalinout_obj_t *data; digitalio_digitalinout_obj_t *data;
digitalio_digitalinout_obj_t *latch; digitalio_digitalinout_obj_t *latch;
size_t key_count; size_t key_count;
mp_uint_t interval_ticks;
uint64_t last_scan_ticks;
bool *previously_pressed;
bool *currently_pressed;
keypad_eventqueue_obj_t *events;
bool value_when_pressed; bool value_when_pressed;
bool value_to_latch; bool value_to_latch;
} keypad_shiftregisterkeys_obj_t; } keypad_shiftregisterkeys_obj_t;

View File

@ -24,13 +24,20 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include <string.h>
#include "shared-bindings/keypad/__init__.h"
#include "shared-bindings/keypad/EventQueue.h"
#include "shared-bindings/keypad/Keys.h" #include "shared-bindings/keypad/Keys.h"
#include "shared-bindings/keypad/KeyMatrix.h" #include "shared-bindings/keypad/KeyMatrix.h"
#include "shared-bindings/keypad/ShiftRegisterKeys.h" #include "shared-bindings/keypad/ShiftRegisterKeys.h"
#include "shared-bindings/supervisor/__init__.h"
#include "supervisor/port.h"
#include "supervisor/shared/lock.h" #include "supervisor/shared/lock.h"
#include "supervisor/shared/tick.h" #include "supervisor/shared/tick.h"
supervisor_lock_t keypad_scanners_linked_list_lock; supervisor_lock_t keypad_scanners_linked_list_lock;
static void keypad_scan_now(keypad_scanner_obj_t *self, uint64_t now);
static void keypad_scan_maybe(keypad_scanner_obj_t *self, uint64_t now);
void keypad_tick(void) { void keypad_tick(void) {
// Fast path. Return immediately there are no scanners. // Fast path. Return immediately there are no scanners.
@ -40,16 +47,10 @@ void keypad_tick(void) {
// Skip scanning if someone else has the lock. Don't wait for the lock. // Skip scanning if someone else has the lock. Don't wait for the lock.
if (supervisor_try_lock(&keypad_scanners_linked_list_lock)) { if (supervisor_try_lock(&keypad_scanners_linked_list_lock)) {
uint64_t now = port_get_raw_ticks(NULL);
mp_obj_t scanner = MP_STATE_VM(keypad_scanners_linked_list); mp_obj_t scanner = MP_STATE_VM(keypad_scanners_linked_list);
while (scanner) { while (scanner) {
if (mp_obj_is_type(scanner, &keypad_keys_type)) { keypad_scan_maybe(scanner, now);
keypad_keys_scan((keypad_keys_obj_t *)scanner);
} else if (mp_obj_is_type(scanner, &keypad_keymatrix_type)) {
keypad_keymatrix_scan((keypad_keymatrix_obj_t *)scanner);
} else if (mp_obj_is_type(scanner, &keypad_shiftregisterkeys_type)) {
keypad_shiftregisterkeys_scan((keypad_shiftregisterkeys_obj_t *)scanner);
}
scanner = ((keypad_scanner_obj_t *)scanner)->next; scanner = ((keypad_scanner_obj_t *)scanner)->next;
} }
supervisor_release_lock(&keypad_scanners_linked_list_lock); supervisor_release_lock(&keypad_scanners_linked_list_lock);
@ -97,3 +98,60 @@ void keypad_deregister_scanner(keypad_scanner_obj_t *scanner) {
} }
supervisor_release_lock(&keypad_scanners_linked_list_lock); supervisor_release_lock(&keypad_scanners_linked_list_lock);
} }
void keypad_construct_common(keypad_scanner_obj_t *self, mp_float_t interval, size_t max_events) {
size_t key_count = common_hal_keypad_generic_get_key_count(self);
self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * key_count, false, false);
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * key_count, false, false);
self->interval_ticks = (mp_uint_t)(interval * 1024); // interval * 1000 * (1024/1000)
keypad_eventqueue_obj_t *events = m_new_obj(keypad_eventqueue_obj_t);
events->base.type = &keypad_eventqueue_type;
common_hal_keypad_eventqueue_construct(events, max_events);
self->events = events;
// Add self to the list of active keypad scanners.
keypad_register_scanner(self);
keypad_scan_now(self, port_get_raw_ticks(NULL));
}
static void keypad_scan_now(keypad_scanner_obj_t *self, uint64_t now) {
self->next_scan_ticks = now + self->interval_ticks;
self->funcs->scan_now(self, supervisor_ticks_ms());
}
static void keypad_scan_maybe(keypad_scanner_obj_t *self, uint64_t now) {
if (now < self->next_scan_ticks) {
return;
}
keypad_scan_now(self, now);
}
void common_hal_keypad_generic_reset(void *self_in) {
keypad_scanner_obj_t *self = self_in;
size_t key_count = common_hal_keypad_generic_get_key_count(self);
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
keypad_scan_now(self, port_get_raw_ticks(NULL));
}
void common_hal_keypad_deinit_core(void *self_in) {
keypad_scanner_obj_t *self = self_in;
self->events = NULL;
}
bool common_hal_keypad_deinited(void *self_in) {
keypad_scanner_obj_t *self = self_in;
return !self->events;
}
size_t common_hal_keypad_generic_get_key_count(void *self_in) {
keypad_scanner_obj_t *self = self_in;
return self->funcs->get_key_count(self);
}
mp_obj_t common_hal_keypad_generic_get_events(void *self_in) {
keypad_scanner_obj_t *self = self_in;
return self->events;
}

View File

@ -30,12 +30,26 @@
#include "py/obj.h" #include "py/obj.h"
#include "supervisor/shared/lock.h" #include "supervisor/shared/lock.h"
// All scanners must have a next field immediately following base. typedef struct _keypad_scanner_funcs_t {
void (*scan_now)(void *self_in, mp_obj_t timestamp);
size_t (*get_key_count)(void *self_in);
} keypad_scanner_funcs_t;
// All scanners must begin with these common fields.
// This is an ad hoc "superclass" struct for scanners, though they do // This is an ad hoc "superclass" struct for scanners, though they do
// not actually have a superclass relationship. // not actually have a superclass relationship.
#define KEYPAD_SCANNER_COMMON_FIELDS \
mp_obj_base_t base; \
struct _keypad_scanner_obj_t *next; \
keypad_scanner_funcs_t *funcs; \
uint64_t next_scan_ticks; \
bool *previously_pressed; \
bool *currently_pressed; \
struct _keypad_eventqueue_obj_t *events; \
mp_uint_t interval_ticks
typedef struct _keypad_scanner_obj_t { typedef struct _keypad_scanner_obj_t {
mp_obj_base_t base; KEYPAD_SCANNER_COMMON_FIELDS;
struct _keypad_scanner_obj_t *next;
} keypad_scanner_obj_t; } keypad_scanner_obj_t;
extern supervisor_lock_t keypad_scanners_linked_list_lock; extern supervisor_lock_t keypad_scanners_linked_list_lock;
@ -45,6 +59,9 @@ void keypad_reset(void);
void keypad_register_scanner(keypad_scanner_obj_t *scanner); void keypad_register_scanner(keypad_scanner_obj_t *scanner);
void keypad_deregister_scanner(keypad_scanner_obj_t *scanner); void keypad_deregister_scanner(keypad_scanner_obj_t *scanner);
void keypad_construct_common(keypad_scanner_obj_t *scanner, mp_float_t interval, size_t max_events);
size_t common_hal_keypad_generic_get_key_count(void *scanner);
void common_hal_keypad_deinit_core(void *scanner);
#endif // SHARED_MODULE_KEYPAD_H #endif // SHARED_MODULE_KEYPAD_H