both Keys and KeyMatrix work
This commit is contained in:
parent
350652ee21
commit
1803a6a71d
@ -78,7 +78,9 @@ const mp_obj_property_t keypad_event_key_num_obj = {
|
||||
};
|
||||
|
||||
//| pressed: bool
|
||||
//| """True if event represents a key down (pressed) transition."""
|
||||
//| """True if event represents a key down (pressed) transition.
|
||||
//| The opposite of `released`.
|
||||
//| """
|
||||
//|
|
||||
STATIC mp_obj_t keypad_event_obj_get_pressed(mp_obj_t self_in) {
|
||||
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
@ -94,7 +96,9 @@ const mp_obj_property_t keypad_event_pressed_obj = {
|
||||
};
|
||||
|
||||
//| released: bool
|
||||
//| """True if event represents a key up (released) transition."""
|
||||
//| """True if event represents a key up (released) transition.
|
||||
//| The opposite of `pressed`.
|
||||
//| """
|
||||
//|
|
||||
STATIC mp_obj_t keypad_event_obj_get_released(mp_obj_t self_in) {
|
||||
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
@ -109,13 +113,48 @@ const mp_obj_property_t keypad_event_released_obj = {
|
||||
MP_ROM_NONE},
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = {
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_key_num), MP_ROM_PTR(&keypad_event_key_num_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_event_pressed_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_released), MP_ROM_PTR(&keypad_event_released_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table);
|
||||
//| def __eq__(self, other: object) -> bool:
|
||||
//| """Two Event objects are equal if their `key_num`
|
||||
//| and `pressed`/`released` values are equal.
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t keypad_event_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||
switch (op) {
|
||||
case MP_BINARY_OP_EQUAL:
|
||||
if (mp_obj_is_type(rhs_in, &keypad_event_type)) {
|
||||
keypad_event_obj_t *lhs = MP_OBJ_TO_PTR(lhs_in);
|
||||
keypad_event_obj_t *rhs = MP_OBJ_TO_PTR(rhs_in);
|
||||
return mp_obj_new_bool(
|
||||
(common_hal_keypad_event_get_key_num(lhs) ==
|
||||
common_hal_keypad_event_get_key_num(rhs)) &&
|
||||
(common_hal_keypad_event_get_pressed(lhs) ==
|
||||
common_hal_keypad_event_get_pressed(rhs)));
|
||||
} else {
|
||||
return mp_const_false;
|
||||
}
|
||||
|
||||
default:
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
}
|
||||
}
|
||||
|
||||
//| def __hash__(self) -> int:
|
||||
//| """Returns a hash for the Event, so it can be used in dictionaries, etc.."""
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t keypad_event_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
||||
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self);
|
||||
switch (op) {
|
||||
case MP_UNARY_OP_HASH: {
|
||||
const mp_int_t key_num = common_hal_keypad_event_get_key_num(self);
|
||||
const bool pressed = common_hal_keypad_event_get_pressed(self);
|
||||
return MP_OBJ_NEW_SMALL_INT((pressed << 15) + key_num);
|
||||
}
|
||||
default:
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void keypad_event_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
@ -124,10 +163,20 @@ STATIC void keypad_event_print(const mp_print_t *print, mp_obj_t self_in, mp_pri
|
||||
common_hal_keypad_event_get_pressed(self) ? "pressed" : "released");
|
||||
}
|
||||
|
||||
STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = {
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_key_num), MP_ROM_PTR(&keypad_event_key_num_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_event_pressed_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_released), MP_ROM_PTR(&keypad_event_released_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t keypad_event_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Event,
|
||||
.make_new = keypad_event_make_new,
|
||||
.print = keypad_event_print,
|
||||
.unary_op = keypad_event_unary_op,
|
||||
.binary_op = keypad_event_binary_op,
|
||||
.locals_dict = (mp_obj_dict_t *)&keypad_event_locals_dict,
|
||||
};
|
||||
|
@ -34,17 +34,20 @@
|
||||
//| class KeyMatrix:
|
||||
//| """Manage a 2D matrix of keys with row and column pins."""
|
||||
//|
|
||||
//| def __init__(self, row_pins: Sequence[microcontroller.Pin], col_pins: Sequence[microcontroller.Pin], max_events: int = 16) -> None:
|
||||
//| def __init__(self, row_pins: Sequence[microcontroller.Pin], col_pins: Sequence[microcontroller.Pin], max_events: int = 64) -> None:
|
||||
//| """
|
||||
//| Create a `Keys` object that will scan key matrix attached to the given row and column pins.
|
||||
//| Create a `Keys` object that will scan the key matrix attached to the given row and column pins.
|
||||
//| If the matrix uses diodes, the diode anodes should be connected to the column pins,
|
||||
//| and the cathodes should be connected to the row pins.
|
||||
//| and the cathodes should be connected to the row pins. If your diodes are reversed,
|
||||
//| simply exchange the row and column pin sequences.
|
||||
//|
|
||||
//| The keys are numbered sequentially from zero. A key number can be computed
|
||||
//| by ``col * len(row_pins) + row``.
|
||||
//| by ``row * len(col_pins) + col``.
|
||||
//|
|
||||
//| :param Sequence[microcontroller.Pin] row_pins: The pins attached to rows.
|
||||
//| :param Sequence[microcontroller.Pin] col_pins: The pins attached to rows.
|
||||
//| The keys are debounced by waiting about 20 msecs before reporting a transition.
|
||||
//|
|
||||
//| :param Sequence[microcontroller.Pin] row_pins: The pins attached to the rows.
|
||||
//| :param Sequence[microcontroller.Pin] col_pins: The pins attached to the colums.
|
||||
//| :param int max_events: Size of key event queue:
|
||||
//| maximum number of key transition events that are saved.
|
||||
//| Must be >= 1.
|
||||
@ -58,8 +61,8 @@ STATIC mp_obj_t keypad_keymatrix_make_new(const mp_obj_type_t *type, size_t n_ar
|
||||
enum { ARG_row_pins, ARG_col_pins, ARG_max_events };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_row_pins, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_col_pins, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
|
||||
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 16} },
|
||||
{ MP_QSTR_col_pins, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
@ -79,16 +82,16 @@ STATIC mp_obj_t keypad_keymatrix_make_new(const mp_obj_type_t *type, size_t n_ar
|
||||
mcu_pin_obj_t *row_pins_array[num_row_pins];
|
||||
mcu_pin_obj_t *col_pins_array[num_col_pins];
|
||||
|
||||
for (mp_uint_t i = 0; i < num_row_pins; i++) {
|
||||
for (size_t row = 0; row < num_row_pins; row++) {
|
||||
mcu_pin_obj_t *pin =
|
||||
validate_obj_is_free_pin(mp_obj_subscr(row_pins, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
|
||||
row_pins_array[i] = pin;
|
||||
validate_obj_is_free_pin(mp_obj_subscr(row_pins, MP_OBJ_NEW_SMALL_INT(row), MP_OBJ_SENTINEL));
|
||||
row_pins_array[row] = pin;
|
||||
}
|
||||
|
||||
for (mp_uint_t i = 0; i < num_col_pins; i++) {
|
||||
for (size_t col = 0; col < num_col_pins; col++) {
|
||||
mcu_pin_obj_t *pin =
|
||||
validate_obj_is_free_pin(mp_obj_subscr(col_pins, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
|
||||
col_pins_array[i] = pin;
|
||||
validate_obj_is_free_pin(mp_obj_subscr(col_pins, MP_OBJ_NEW_SMALL_INT(col), MP_OBJ_SENTINEL));
|
||||
col_pins_array[col] = pin;
|
||||
}
|
||||
|
||||
common_hal_keypad_keymatrix_construct(self, num_row_pins, row_pins_array, num_col_pins, col_pins_array, max_events);
|
||||
@ -131,7 +134,7 @@ STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) {
|
||||
}
|
||||
|
||||
//| def next_event(self) -> Optional[Event]:
|
||||
//| """Return the next key transition event. Return ``None` if no events are pending.
|
||||
//| """Return the next key transition event. Return ``None`` if no events are pending.
|
||||
//|
|
||||
//| Note that the queue size is limited; see ``max_events`` in the constructor.
|
||||
//| If a new event arrives when the queue is full, the oldest event is discarded.
|
||||
@ -141,13 +144,13 @@ STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) {
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t keypad_keymatrix_next_event(mp_obj_t self_in, mp_obj_t event_in) {
|
||||
STATIC mp_obj_t keypad_keymatrix_next_event(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_next_event(self);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(keypad_keymatrix_next_event_obj, keypad_keymatrix_next_event);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_next_event_obj, keypad_keymatrix_next_event);
|
||||
|
||||
//| def clear_events(self) -> None:
|
||||
//| """Clear any queued key transition events.
|
||||
@ -184,7 +187,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(keypad_keymatrix_pressed_obj, keypad_keymatrix_pressed
|
||||
|
||||
//| def key_num(self, row: int, col: int) -> int:
|
||||
//| """Return the key number for a given row and column.
|
||||
//| The key number is calculated by `row * number_of_columns + col`.
|
||||
//| The key number is calculated by ``row * len(col_pins) + col``.
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
|
@ -34,11 +34,13 @@
|
||||
//| class Keys:
|
||||
//| """Manage a set of independent keys."""
|
||||
//|
|
||||
//| def __init__(self, pins: Sequence[microcontroller.Pin], *, level_when_pressed: bool, pull: bool = True, max_events: int = 16) -> None:
|
||||
//| def __init__(self, pins: Sequence[microcontroller.Pin], *, level_when_pressed: bool, pull: bool = True, max_events: int = 64) -> None:
|
||||
//| """
|
||||
//| Create a `Keys` object that will scan keys attached to the given sequence of pins.
|
||||
//| Each key is independent and attached to its own pin.
|
||||
//|
|
||||
//| The keys are debounced by waiting about 20 msecs before reporting a transition.
|
||||
//|
|
||||
//| :param Sequence[microcontroller.Pin] pins: The pins attached to the keys.
|
||||
//| The key numbers correspond to indices into this sequence.
|
||||
//| :param bool value_when_pressed: ``True`` if the pin reads high when the key is pressed.
|
||||
@ -65,7 +67,7 @@ STATIC mp_obj_t keypad_keys_make_new(const mp_obj_type_t *type, size_t n_args, c
|
||||
{ MP_QSTR_pins, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_value_when_pressed, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
|
||||
{ MP_QSTR_pull, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
||||
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 16} },
|
||||
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
@ -131,7 +133,7 @@ STATIC void check_for_deinit(keypad_keys_obj_t *self) {
|
||||
}
|
||||
|
||||
//| def next_event(self) -> Optional[Event]:
|
||||
//| """Return the next key transition event. Return ``None` if no events are pending.
|
||||
//| """Return the next key transition event. Return ``None`` if no events are pending.
|
||||
//|
|
||||
//| Note that the queue size is limited; see ``max_events`` in the constructor.
|
||||
//| If a new event arrives when the queue is full, the oldest event is discarded.
|
||||
|
@ -50,24 +50,24 @@ static mp_uint_t row_col_to_key_num(keypad_keymatrix_obj_t *self, mp_uint_t row,
|
||||
void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint_t num_row_pins, mcu_pin_obj_t *row_pins[], mp_uint_t num_col_pins, mcu_pin_obj_t *col_pins[], size_t max_events) {
|
||||
|
||||
mp_obj_t row_dios[num_row_pins];
|
||||
for (size_t i = 0; i < num_row_pins; i++) {
|
||||
for (size_t row = 0; row < num_row_pins; row++) {
|
||||
digitalio_digitalinout_obj_t *dio = m_new_obj(digitalio_digitalinout_obj_t);
|
||||
dio->base.type = &digitalio_digitalinout_type;
|
||||
common_hal_digitalio_digitalinout_construct(dio, row_pins[i]);
|
||||
common_hal_digitalio_digitalinout_construct(dio, row_pins[row]);
|
||||
common_hal_digitalio_digitalinout_switch_to_input(dio, PULL_UP);
|
||||
row_dios[i] = dio;
|
||||
row_dios[row] = dio;
|
||||
}
|
||||
self->row_digitalinouts = mp_obj_new_tuple(num_col_pins, row_dios);
|
||||
self->row_digitalinouts = mp_obj_new_tuple(num_row_pins, row_dios);
|
||||
|
||||
mp_obj_t col_dios[num_col_pins];
|
||||
for (size_t i = 0; i < num_col_pins; i++) {
|
||||
for (size_t col = 0; col < num_col_pins; col++) {
|
||||
digitalio_digitalinout_obj_t *dio = m_new_obj(digitalio_digitalinout_obj_t);
|
||||
dio->base.type = &digitalio_digitalinout_type;
|
||||
common_hal_digitalio_digitalinout_construct(dio, col_pins[i]);
|
||||
common_hal_digitalio_digitalinout_construct(dio, col_pins[col]);
|
||||
common_hal_digitalio_digitalinout_switch_to_input(dio, PULL_UP);
|
||||
col_dios[i] = dio;
|
||||
col_dios[col] = dio;
|
||||
}
|
||||
self->col_digitalinouts = mp_obj_new_tuple(num_row_pins, col_dios);
|
||||
self->col_digitalinouts = mp_obj_new_tuple(num_col_pins, col_dios);
|
||||
|
||||
self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * num_row_pins * num_col_pins, false, false);
|
||||
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_row_pins * num_col_pins, false, false);
|
||||
@ -81,6 +81,8 @@ void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint
|
||||
self->next = MP_STATE_VM(keypad_keymatrix_linked_list);
|
||||
MP_STATE_VM(keypad_keymatrix_linked_list) = self;
|
||||
supervisor_release_lock(&keypad_keymatrix_linked_list_lock);
|
||||
|
||||
supervisor_enable_tick();
|
||||
}
|
||||
|
||||
void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self) {
|
||||
@ -145,7 +147,7 @@ mp_obj_t common_hal_keypad_keymatrix_next_event(keypad_keymatrix_obj_t *self) {
|
||||
}
|
||||
|
||||
keypad_event_obj_t *event = m_new_obj(keypad_event_obj_t);
|
||||
self->base.type = &keypad_event_type;
|
||||
event->base.type = &keypad_event_type;
|
||||
common_hal_keypad_event_construct(event, encoded_event & EVENT_KEY_NUM_MASK, encoded_event & EVENT_PRESSED);
|
||||
return MP_OBJ_FROM_PTR(event);
|
||||
}
|
||||
@ -179,8 +181,9 @@ static void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
|
||||
self->previously_pressed[key_num] = previous;
|
||||
|
||||
// Get the current state, by reading whether the col got pulled down or not.
|
||||
// If low, the key is pressed.
|
||||
const bool current =
|
||||
common_hal_digitalio_digitalinout_get_value(self->col_digitalinouts->items[key_num]);
|
||||
!common_hal_digitalio_digitalinout_get_value(self->col_digitalinouts->items[col]);
|
||||
self->currently_pressed[key_num] = current;
|
||||
|
||||
// Record any transitions.
|
||||
|
Loading…
Reference in New Issue
Block a user