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:
parent
203dad11bf
commit
d7c86c0e3e
@ -4067,63 +4067,8 @@ msgstr ""
|
||||
msgid "pow() with 3 arguments requires integers"
|
||||
msgstr ""
|
||||
|
||||
#: ports/espressif/boards/adafruit_esp32s2_camera/mpconfigboard.h
|
||||
#: ports/espressif/boards/adafruit_feather_esp32s2/mpconfigboard.h
|
||||
#: 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
|
||||
#: ports/espressif/boards/adafruit_qtpy_esp32c3/mpconfigboard.h
|
||||
#: supervisor/shared/safe_mode.c
|
||||
msgid "pressing boot button at start up.\n"
|
||||
msgstr ""
|
||||
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "py/binary.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/keypad/__init__.h"
|
||||
#include "shared-bindings/keypad/Event.h"
|
||||
#include "shared-bindings/keypad/KeyMatrix.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 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();
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
//| """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]:
|
||||
//| """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(
|
||||
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_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)
|
||||
//| """
|
||||
//|
|
||||
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[] = {
|
||||
{ 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___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_key_count), MP_ROM_PTR(&keypad_keymatrix_key_count_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_keymatrix_reset_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_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) },
|
||||
};
|
||||
|
@ -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_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);
|
||||
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_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
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "py/binary.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/keypad/__init__.h"
|
||||
#include "shared-bindings/keypad/Event.h"
|
||||
#include "shared-bindings/keypad/Keys.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 void check_for_deinit(keypad_keys_obj_t *self) {
|
||||
if (common_hal_keypad_keys_deinited(self)) {
|
||||
raise_deinited_error();
|
||||
}
|
||||
}
|
||||
|
||||
//| def reset(self) -> None:
|
||||
//| """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
|
||||
//| """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
|
||||
//| """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[] = {
|
||||
{ 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___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_key_count), MP_ROM_PTR(&keypad_keys_key_count_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_keys_reset_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) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(keypad_keys_locals_dict, keypad_keys_locals_dict_table);
|
||||
|
@ -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_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
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "py/binary.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/keypad/__init__.h"
|
||||
#include "shared-bindings/keypad/Event.h"
|
||||
#include "shared-bindings/keypad/ShiftRegisterKeys.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 void check_for_deinit(keypad_shiftregisterkeys_obj_t *self) {
|
||||
if (common_hal_keypad_shiftregisterkeys_deinited(self)) {
|
||||
raise_deinited_error();
|
||||
}
|
||||
}
|
||||
|
||||
//| def reset(self) -> None:
|
||||
//| """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
|
||||
@ -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
|
||||
//| """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
|
||||
//| """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[] = {
|
||||
{ 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___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_key_count), MP_ROM_PTR(&keypad_shiftregisterkeys_key_count_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_shiftregisterkeys_reset_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) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(keypad_shiftregisterkeys_locals_dict, keypad_shiftregisterkeys_locals_dict_table);
|
||||
|
@ -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_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
|
||||
|
@ -32,6 +32,53 @@
|
||||
#include "shared-bindings/keypad/KeyMatrix.h"
|
||||
#include "shared-bindings/keypad/Keys.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
|
||||
//|
|
||||
|
@ -28,14 +28,17 @@
|
||||
#define SHARED_BINDINGS_KEYPAD_H
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/objtuple.h"
|
||||
#include "py/objproperty.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);
|
||||
bool common_hal_keypad_enable(const mp_obj_t devices_seq);
|
||||
extern const mp_obj_property_t keypad_generic_events_obj;
|
||||
extern const mp_obj_property_t keypad_generic_key_count_obj;
|
||||
|
||||
#endif // SHARED_BINDINGS_KEYPAD_H
|
||||
|
@ -30,7 +30,7 @@
|
||||
#include "py/obj.h"
|
||||
#include "py/ringbuf.h"
|
||||
|
||||
typedef struct {
|
||||
typedef struct _keypad_eventqueue_obj_t {
|
||||
mp_obj_base_t base;
|
||||
ringbuf_t encoded_events;
|
||||
bool overflowed;
|
||||
|
@ -37,7 +37,13 @@
|
||||
#include "supervisor/port.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) {
|
||||
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->columns_to_anodes = columns_to_anodes;
|
||||
self->funcs = &keymatrix_funcs;
|
||||
|
||||
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;
|
||||
|
||||
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));
|
||||
keypad_construct_common((keypad_scanner_obj_t *)self, interval, max_events);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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]);
|
||||
}
|
||||
self->column_digitalinouts = MP_ROM_NONE;
|
||||
}
|
||||
|
||||
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);
|
||||
common_hal_keypad_deinit_core(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;
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self) {
|
||||
return MP_OBJ_FROM_PTR(self->events);
|
||||
static size_t keymatrix_get_key_count(void *self_in) {
|
||||
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) {
|
||||
const size_t key_count = common_hal_keypad_keymatrix_get_key_count(self);
|
||||
|
||||
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();
|
||||
static void keymatrix_scan_now(void *self_in, mp_obj_t timestamp) {
|
||||
keypad_keymatrix_obj_t *self = self_in;
|
||||
|
||||
// On entry, all pins are set to inputs with a pull-up or pull-down,
|
||||
// depending on the diode orientation.
|
||||
|
@ -35,16 +35,9 @@
|
||||
#include "shared-module/keypad/EventQueue.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
// All scanners have a next field here, to keep a linked list of active scanners.
|
||||
keypad_scanner_obj_t *next;
|
||||
KEYPAD_SCANNER_COMMON_FIELDS;
|
||||
mp_obj_tuple_t *row_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;
|
||||
} keypad_keymatrix_obj_t;
|
||||
|
||||
|
@ -36,7 +36,13 @@
|
||||
#include "supervisor/port.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) {
|
||||
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->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false);
|
||||
self->value_when_pressed = value_when_pressed;
|
||||
self->funcs = &keys_funcs;
|
||||
|
||||
self->interval_ticks = (mp_uint_t)(interval * 1024); // interval * 1000 * (1024/1000)
|
||||
self->last_scan_ticks = port_get_raw_ticks(NULL);
|
||||
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_keys_scan_now(self, port_get_raw_ticks(NULL));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Remove self from the list of active keypad scanners first.
|
||||
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]);
|
||||
}
|
||||
self->digitalinouts = MP_ROM_NONE;
|
||||
|
||||
common_hal_keypad_deinit_core(self);
|
||||
}
|
||||
|
||||
bool common_hal_keypad_keys_deinited(keypad_keys_obj_t *self) {
|
||||
return self->digitalinouts == MP_ROM_NONE;
|
||||
}
|
||||
|
||||
size_t common_hal_keypad_keys_get_key_count(keypad_keys_obj_t *self) {
|
||||
size_t keys_get_key_count(void *self_in) {
|
||||
keypad_keys_obj_t *self = self_in;
|
||||
return self->digitalinouts->len;
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_keypad_keys_get_events(keypad_keys_obj_t *self) {
|
||||
return MP_OBJ_FROM_PTR(self->events);
|
||||
}
|
||||
|
||||
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();
|
||||
static void keypad_keys_scan_now(void *self_in, mp_obj_t timestamp) {
|
||||
keypad_keys_obj_t *self = self_in;
|
||||
size_t key_count = keys_get_key_count(self);
|
||||
|
||||
for (mp_uint_t key_number = 0; key_number < key_count; key_number++) {
|
||||
// Remember the previous up/down state.
|
||||
|
@ -35,15 +35,8 @@
|
||||
#include "shared-module/keypad/EventQueue.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
// All scanners have a next field here, to keep a linked list of active scanners.
|
||||
keypad_scanner_obj_t *next;
|
||||
KEYPAD_SCANNER_COMMON_FIELDS;
|
||||
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;
|
||||
} keypad_keys_obj_t;
|
||||
|
||||
|
@ -36,7 +36,13 @@
|
||||
#include "supervisor/port.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) {
|
||||
|
||||
@ -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);
|
||||
latch->base.type = &digitalio_digitalinout_type;
|
||||
|
||||
common_hal_digitalio_digitalinout_construct(latch, latch_pin);
|
||||
common_hal_digitalio_digitalinout_switch_to_output(latch, true, DRIVE_MODE_PUSH_PULL);
|
||||
self->latch = 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->key_count = key_count;
|
||||
self->funcs = &shiftregisterkeys_funcs;
|
||||
|
||||
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((keypad_scanner_obj_t *)self);
|
||||
keypad_shiftregisterkeys_scan_now(self, port_get_raw_ticks(NULL));
|
||||
keypad_construct_common((keypad_scanner_obj_t *)self, interval, max_events);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -93,47 +90,22 @@ void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *
|
||||
|
||||
common_hal_digitalio_digitalinout_deinit(self->latch);
|
||||
self->latch = MP_ROM_NONE;
|
||||
|
||||
common_hal_keypad_deinit_core(self);
|
||||
}
|
||||
|
||||
bool common_hal_keypad_shiftregisterkeys_deinited(keypad_shiftregisterkeys_obj_t *self) {
|
||||
return self->clock == MP_ROM_NONE;
|
||||
}
|
||||
|
||||
size_t common_hal_keypad_shiftregisterkeys_get_key_count(keypad_shiftregisterkeys_obj_t *self) {
|
||||
size_t shiftregisterkeys_get_key_count(void *self_in) {
|
||||
keypad_shiftregisterkeys_obj_t *self = self_in;
|
||||
return self->key_count;
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys_obj_t *self) {
|
||||
return MP_OBJ_FROM_PTR(self->events);
|
||||
}
|
||||
|
||||
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();
|
||||
static void shiftregisterkeys_scan_now(void *self_in, mp_obj_t timestamp) {
|
||||
keypad_shiftregisterkeys_obj_t *self = self_in;
|
||||
|
||||
// Latch (freeze) the current state of the input pins.
|
||||
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++) {
|
||||
// Zero-th data appears on on the data pin immediately, without shifting.
|
||||
|
@ -35,18 +35,11 @@
|
||||
#include "shared-module/keypad/EventQueue.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
// All scanners have a next field here, to keep a linked list of active scanners.
|
||||
keypad_scanner_obj_t *next;
|
||||
KEYPAD_SCANNER_COMMON_FIELDS;
|
||||
digitalio_digitalinout_obj_t *clock;
|
||||
digitalio_digitalinout_obj_t *data;
|
||||
digitalio_digitalinout_obj_t *latch;
|
||||
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_to_latch;
|
||||
} keypad_shiftregisterkeys_obj_t;
|
||||
|
@ -24,13 +24,20 @@
|
||||
* 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/KeyMatrix.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/tick.h"
|
||||
|
||||
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) {
|
||||
// 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.
|
||||
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);
|
||||
while (scanner) {
|
||||
if (mp_obj_is_type(scanner, &keypad_keys_type)) {
|
||||
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);
|
||||
}
|
||||
|
||||
keypad_scan_maybe(scanner, now);
|
||||
scanner = ((keypad_scanner_obj_t *)scanner)->next;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -30,12 +30,26 @@
|
||||
#include "py/obj.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
|
||||
// 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 {
|
||||
mp_obj_base_t base;
|
||||
struct _keypad_scanner_obj_t *next;
|
||||
KEYPAD_SCANNER_COMMON_FIELDS;
|
||||
} keypad_scanner_obj_t;
|
||||
|
||||
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_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
|
||||
|
Loading…
Reference in New Issue
Block a user