Skip to content

Commit 1803a6a

Browse files
committed
both Keys and KeyMatrix work
1 parent 350652e commit 1803a6a

File tree

4 files changed

+97
-40
lines changed

4 files changed

+97
-40
lines changed

shared-bindings/keypad/Event.c

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ const mp_obj_property_t keypad_event_key_num_obj = {
7878
};
7979

8080
//| pressed: bool
81-
//| """True if event represents a key down (pressed) transition."""
81+
//| """True if event represents a key down (pressed) transition.
82+
//| The opposite of `released`.
83+
//| """
8284
//|
8385
STATIC mp_obj_t keypad_event_obj_get_pressed(mp_obj_t self_in) {
8486
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -94,7 +96,9 @@ const mp_obj_property_t keypad_event_pressed_obj = {
9496
};
9597

9698
//| released: bool
97-
//| """True if event represents a key up (released) transition."""
99+
//| """True if event represents a key up (released) transition.
100+
//| The opposite of `pressed`.
101+
//| """
98102
//|
99103
STATIC mp_obj_t keypad_event_obj_get_released(mp_obj_t self_in) {
100104
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -109,13 +113,48 @@ const mp_obj_property_t keypad_event_released_obj = {
109113
MP_ROM_NONE},
110114
};
111115

112-
STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = {
113-
// Properties
114-
{ MP_ROM_QSTR(MP_QSTR_key_num), MP_ROM_PTR(&keypad_event_key_num_obj) },
115-
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_event_pressed_obj) },
116-
{ MP_ROM_QSTR(MP_QSTR_released), MP_ROM_PTR(&keypad_event_released_obj) },
117-
};
118-
STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table);
116+
//| def __eq__(self, other: object) -> bool:
117+
//| """Two Event objects are equal if their `key_num`
118+
//| and `pressed`/`released` values are equal.
119+
//| """
120+
//| ...
121+
//|
122+
STATIC mp_obj_t keypad_event_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
123+
switch (op) {
124+
case MP_BINARY_OP_EQUAL:
125+
if (mp_obj_is_type(rhs_in, &keypad_event_type)) {
126+
keypad_event_obj_t *lhs = MP_OBJ_TO_PTR(lhs_in);
127+
keypad_event_obj_t *rhs = MP_OBJ_TO_PTR(rhs_in);
128+
return mp_obj_new_bool(
129+
(common_hal_keypad_event_get_key_num(lhs) ==
130+
common_hal_keypad_event_get_key_num(rhs)) &&
131+
(common_hal_keypad_event_get_pressed(lhs) ==
132+
common_hal_keypad_event_get_pressed(rhs)));
133+
} else {
134+
return mp_const_false;
135+
}
136+
137+
default:
138+
return MP_OBJ_NULL; // op not supported
139+
}
140+
}
141+
142+
//| def __hash__(self) -> int:
143+
//| """Returns a hash for the Event, so it can be used in dictionaries, etc.."""
144+
//| ...
145+
//|
146+
STATIC mp_obj_t keypad_event_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
147+
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self);
148+
switch (op) {
149+
case MP_UNARY_OP_HASH: {
150+
const mp_int_t key_num = common_hal_keypad_event_get_key_num(self);
151+
const bool pressed = common_hal_keypad_event_get_pressed(self);
152+
return MP_OBJ_NEW_SMALL_INT((pressed << 15) + key_num);
153+
}
154+
default:
155+
return MP_OBJ_NULL; // op not supported
156+
}
157+
}
119158

120159
STATIC void keypad_event_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
121160
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
124163
common_hal_keypad_event_get_pressed(self) ? "pressed" : "released");
125164
}
126165

166+
STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = {
167+
// Properties
168+
{ MP_ROM_QSTR(MP_QSTR_key_num), MP_ROM_PTR(&keypad_event_key_num_obj) },
169+
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_event_pressed_obj) },
170+
{ MP_ROM_QSTR(MP_QSTR_released), MP_ROM_PTR(&keypad_event_released_obj) },
171+
};
172+
STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table);
173+
127174
const mp_obj_type_t keypad_event_type = {
128175
{ &mp_type_type },
129176
.name = MP_QSTR_Event,
130177
.make_new = keypad_event_make_new,
131178
.print = keypad_event_print,
179+
.unary_op = keypad_event_unary_op,
180+
.binary_op = keypad_event_binary_op,
132181
.locals_dict = (mp_obj_dict_t *)&keypad_event_locals_dict,
133182
};

shared-bindings/keypad/KeyMatrix.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,20 @@
3434
//| class KeyMatrix:
3535
//| """Manage a 2D matrix of keys with row and column pins."""
3636
//|
37-
//| def __init__(self, row_pins: Sequence[microcontroller.Pin], col_pins: Sequence[microcontroller.Pin], max_events: int = 16) -> None:
37+
//| def __init__(self, row_pins: Sequence[microcontroller.Pin], col_pins: Sequence[microcontroller.Pin], max_events: int = 64) -> None:
3838
//| """
39-
//| Create a `Keys` object that will scan key matrix attached to the given row and column pins.
39+
//| Create a `Keys` object that will scan the key matrix attached to the given row and column pins.
4040
//| If the matrix uses diodes, the diode anodes should be connected to the column pins,
41-
//| and the cathodes should be connected to the row pins.
41+
//| and the cathodes should be connected to the row pins. If your diodes are reversed,
42+
//| simply exchange the row and column pin sequences.
4243
//|
4344
//| The keys are numbered sequentially from zero. A key number can be computed
44-
//| by ``col * len(row_pins) + row``.
45+
//| by ``row * len(col_pins) + col``.
4546
//|
46-
//| :param Sequence[microcontroller.Pin] row_pins: The pins attached to rows.
47-
//| :param Sequence[microcontroller.Pin] col_pins: The pins attached to rows.
47+
//| The keys are debounced by waiting about 20 msecs before reporting a transition.
48+
//|
49+
//| :param Sequence[microcontroller.Pin] row_pins: The pins attached to the rows.
50+
//| :param Sequence[microcontroller.Pin] col_pins: The pins attached to the colums.
4851
//| :param int max_events: Size of key event queue:
4952
//| maximum number of key transition events that are saved.
5053
//| 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
5861
enum { ARG_row_pins, ARG_col_pins, ARG_max_events };
5962
static const mp_arg_t allowed_args[] = {
6063
{ MP_QSTR_row_pins, MP_ARG_REQUIRED | MP_ARG_OBJ },
61-
{ MP_QSTR_col_pins, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
62-
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 16} },
64+
{ MP_QSTR_col_pins, MP_ARG_REQUIRED | MP_ARG_OBJ },
65+
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
6366
};
6467
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
6568
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
7982
mcu_pin_obj_t *row_pins_array[num_row_pins];
8083
mcu_pin_obj_t *col_pins_array[num_col_pins];
8184

82-
for (mp_uint_t i = 0; i < num_row_pins; i++) {
85+
for (size_t row = 0; row < num_row_pins; row++) {
8386
mcu_pin_obj_t *pin =
84-
validate_obj_is_free_pin(mp_obj_subscr(row_pins, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
85-
row_pins_array[i] = pin;
87+
validate_obj_is_free_pin(mp_obj_subscr(row_pins, MP_OBJ_NEW_SMALL_INT(row), MP_OBJ_SENTINEL));
88+
row_pins_array[row] = pin;
8689
}
8790

88-
for (mp_uint_t i = 0; i < num_col_pins; i++) {
91+
for (size_t col = 0; col < num_col_pins; col++) {
8992
mcu_pin_obj_t *pin =
90-
validate_obj_is_free_pin(mp_obj_subscr(col_pins, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
91-
col_pins_array[i] = pin;
93+
validate_obj_is_free_pin(mp_obj_subscr(col_pins, MP_OBJ_NEW_SMALL_INT(col), MP_OBJ_SENTINEL));
94+
col_pins_array[col] = pin;
9295
}
9396

9497
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) {
131134
}
132135

133136
//| def next_event(self) -> Optional[Event]:
134-
//| """Return the next key transition event. Return ``None` if no events are pending.
137+
//| """Return the next key transition event. Return ``None`` if no events are pending.
135138
//|
136139
//| Note that the queue size is limited; see ``max_events`` in the constructor.
137140
//| 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) {
141144
//| """
142145
//| ...
143146
//|
144-
STATIC mp_obj_t keypad_keymatrix_next_event(mp_obj_t self_in, mp_obj_t event_in) {
147+
STATIC mp_obj_t keypad_keymatrix_next_event(mp_obj_t self_in) {
145148
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
146149
check_for_deinit(self);
147150

148151
return common_hal_keypad_keymatrix_next_event(self);
149152
}
150-
MP_DEFINE_CONST_FUN_OBJ_2(keypad_keymatrix_next_event_obj, keypad_keymatrix_next_event);
153+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_next_event_obj, keypad_keymatrix_next_event);
151154

152155
//| def clear_events(self) -> None:
153156
//| """Clear any queued key transition events.
@@ -184,7 +187,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(keypad_keymatrix_pressed_obj, keypad_keymatrix_pressed
184187

185188
//| def key_num(self, row: int, col: int) -> int:
186189
//| """Return the key number for a given row and column.
187-
//| The key number is calculated by `row * number_of_columns + col`.
190+
//| The key number is calculated by ``row * len(col_pins) + col``.
188191
//| """
189192
//| ...
190193
//|

shared-bindings/keypad/Keys.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434
//| class Keys:
3535
//| """Manage a set of independent keys."""
3636
//|
37-
//| def __init__(self, pins: Sequence[microcontroller.Pin], *, level_when_pressed: bool, pull: bool = True, max_events: int = 16) -> None:
37+
//| def __init__(self, pins: Sequence[microcontroller.Pin], *, level_when_pressed: bool, pull: bool = True, max_events: int = 64) -> None:
3838
//| """
3939
//| Create a `Keys` object that will scan keys attached to the given sequence of pins.
4040
//| Each key is independent and attached to its own pin.
4141
//|
42+
//| The keys are debounced by waiting about 20 msecs before reporting a transition.
43+
//|
4244
//| :param Sequence[microcontroller.Pin] pins: The pins attached to the keys.
4345
//| The key numbers correspond to indices into this sequence.
4446
//| :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
6567
{ MP_QSTR_pins, MP_ARG_REQUIRED | MP_ARG_OBJ },
6668
{ MP_QSTR_value_when_pressed, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
6769
{ MP_QSTR_pull, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
68-
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 16} },
70+
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
6971
};
7072
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
7173
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) {
131133
}
132134

133135
//| def next_event(self) -> Optional[Event]:
134-
//| """Return the next key transition event. Return ``None` if no events are pending.
136+
//| """Return the next key transition event. Return ``None`` if no events are pending.
135137
//|
136138
//| Note that the queue size is limited; see ``max_events`` in the constructor.
137139
//| If a new event arrives when the queue is full, the oldest event is discarded.

shared-module/keypad/KeyMatrix.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,24 @@ static mp_uint_t row_col_to_key_num(keypad_keymatrix_obj_t *self, mp_uint_t row,
5050
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) {
5151

5252
mp_obj_t row_dios[num_row_pins];
53-
for (size_t i = 0; i < num_row_pins; i++) {
53+
for (size_t row = 0; row < num_row_pins; row++) {
5454
digitalio_digitalinout_obj_t *dio = m_new_obj(digitalio_digitalinout_obj_t);
5555
dio->base.type = &digitalio_digitalinout_type;
56-
common_hal_digitalio_digitalinout_construct(dio, row_pins[i]);
56+
common_hal_digitalio_digitalinout_construct(dio, row_pins[row]);
5757
common_hal_digitalio_digitalinout_switch_to_input(dio, PULL_UP);
58-
row_dios[i] = dio;
58+
row_dios[row] = dio;
5959
}
60-
self->row_digitalinouts = mp_obj_new_tuple(num_col_pins, row_dios);
60+
self->row_digitalinouts = mp_obj_new_tuple(num_row_pins, row_dios);
6161

6262
mp_obj_t col_dios[num_col_pins];
63-
for (size_t i = 0; i < num_col_pins; i++) {
63+
for (size_t col = 0; col < num_col_pins; col++) {
6464
digitalio_digitalinout_obj_t *dio = m_new_obj(digitalio_digitalinout_obj_t);
6565
dio->base.type = &digitalio_digitalinout_type;
66-
common_hal_digitalio_digitalinout_construct(dio, col_pins[i]);
66+
common_hal_digitalio_digitalinout_construct(dio, col_pins[col]);
6767
common_hal_digitalio_digitalinout_switch_to_input(dio, PULL_UP);
68-
col_dios[i] = dio;
68+
col_dios[col] = dio;
6969
}
70-
self->col_digitalinouts = mp_obj_new_tuple(num_row_pins, col_dios);
70+
self->col_digitalinouts = mp_obj_new_tuple(num_col_pins, col_dios);
7171

7272
self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * num_row_pins * num_col_pins, false, false);
7373
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
8181
self->next = MP_STATE_VM(keypad_keymatrix_linked_list);
8282
MP_STATE_VM(keypad_keymatrix_linked_list) = self;
8383
supervisor_release_lock(&keypad_keymatrix_linked_list_lock);
84+
85+
supervisor_enable_tick();
8486
}
8587

8688
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) {
145147
}
146148

147149
keypad_event_obj_t *event = m_new_obj(keypad_event_obj_t);
148-
self->base.type = &keypad_event_type;
150+
event->base.type = &keypad_event_type;
149151
common_hal_keypad_event_construct(event, encoded_event & EVENT_KEY_NUM_MASK, encoded_event & EVENT_PRESSED);
150152
return MP_OBJ_FROM_PTR(event);
151153
}
@@ -179,8 +181,9 @@ static void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
179181
self->previously_pressed[key_num] = previous;
180182

181183
// Get the current state, by reading whether the col got pulled down or not.
184+
// If low, the key is pressed.
182185
const bool current =
183-
common_hal_digitalio_digitalinout_get_value(self->col_digitalinouts->items[key_num]);
186+
!common_hal_digitalio_digitalinout_get_value(self->col_digitalinouts->items[col]);
184187
self->currently_pressed[key_num] = current;
185188

186189
// Record any transitions.

0 commit comments

Comments
 (0)