34
34
//| class KeyMatrix:
35
35
//| """Manage a 2D matrix of keys with row and column pins."""
36
36
//|
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:
38
38
//| """
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.
40
40
//| 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.
42
43
//|
43
44
//| 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 ``.
45
46
//|
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.
48
51
//| :param int max_events: Size of key event queue:
49
52
//| maximum number of key transition events that are saved.
50
53
//| 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
58
61
enum { ARG_row_pins , ARG_col_pins , ARG_max_events };
59
62
static const mp_arg_t allowed_args [] = {
60
63
{ 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 } },
63
66
};
64
67
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
65
68
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
79
82
mcu_pin_obj_t * row_pins_array [num_row_pins ];
80
83
mcu_pin_obj_t * col_pins_array [num_col_pins ];
81
84
82
- for (mp_uint_t i = 0 ; i < num_row_pins ; i ++ ) {
85
+ for (size_t row = 0 ; row < num_row_pins ; row ++ ) {
83
86
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 ;
86
89
}
87
90
88
- for (mp_uint_t i = 0 ; i < num_col_pins ; i ++ ) {
91
+ for (size_t col = 0 ; col < num_col_pins ; col ++ ) {
89
92
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 ;
92
95
}
93
96
94
97
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) {
131
134
}
132
135
133
136
//| 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.
135
138
//|
136
139
//| Note that the queue size is limited; see ``max_events`` in the constructor.
137
140
//| 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) {
141
144
//| """
142
145
//| ...
143
146
//|
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 ) {
145
148
keypad_keymatrix_obj_t * self = MP_OBJ_TO_PTR (self_in );
146
149
check_for_deinit (self );
147
150
148
151
return common_hal_keypad_keymatrix_next_event (self );
149
152
}
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 );
151
154
152
155
//| def clear_events(self) -> None:
153
156
//| """Clear any queued key transition events.
@@ -184,7 +187,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(keypad_keymatrix_pressed_obj, keypad_keymatrix_pressed
184
187
185
188
//| def key_num(self, row: int, col: int) -> int:
186
189
//| """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` `.
188
191
//| """
189
192
//| ...
190
193
//|
0 commit comments