2626
2727#include "py/gc.h"
2828#include "py/runtime.h"
29+ #include "shared-bindings/keypad/__init__.h"
2930#include "shared-bindings/keypad/Event.h"
3031#include "shared-bindings/keypad/KeyMatrix.h"
3132#include "shared-bindings/digitalio/DigitalInOut.h"
3233#include "shared-bindings/util.h"
3334#include "supervisor/port.h"
34- #include "supervisor/shared/lock.h"
3535#include "supervisor/shared/tick.h"
3636
37- static supervisor_lock_t keypad_keymatrix_linked_list_lock ;
38-
3937#define DEBOUNCE_TICKS (20)
4038
4139// Top bit of 16-bit event indicates pressed or released. Rest is key_num.
@@ -75,12 +73,8 @@ void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint
7573 // Event queue is 16-bit values.
7674 ringbuf_alloc (& self -> encoded_events , max_events * 2 , false);
7775
78- // Add self to the list of active Keys objects.
79-
80- supervisor_acquire_lock (& keypad_keymatrix_linked_list_lock );
81- self -> next = MP_STATE_VM (keypad_keymatrix_linked_list );
82- MP_STATE_VM (keypad_keymatrix_linked_list ) = self ;
83- supervisor_release_lock (& keypad_keymatrix_linked_list_lock );
76+ // Add self to the list of active keypad scanners.
77+ keypad_register_scanner ((keypad_scanner_obj_t * )self );
8478
8579 supervisor_enable_tick ();
8680}
@@ -90,6 +84,9 @@ void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self) {
9084 return ;
9185 }
9286
87+ // Remove self from the list of active keypad scanners first.
88+ keypad_deregister_scanner ((keypad_scanner_obj_t * )self );
89+
9390 for (size_t row = 0 ; row < common_hal_keypad_keymatrix_num_rows (self ); row ++ ) {
9491 common_hal_digitalio_digitalinout_deinit (self -> row_digitalinouts -> items [row ]);
9592 }
@@ -99,25 +96,6 @@ void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self) {
9996 common_hal_digitalio_digitalinout_deinit (self -> col_digitalinouts -> items [col ]);
10097 }
10198 self -> col_digitalinouts = MP_ROM_NONE ;
102-
103- // Remove self from the list of active KeyMatrix objects.
104-
105- supervisor_acquire_lock (& keypad_keymatrix_linked_list_lock );
106- if (MP_STATE_VM (keypad_keymatrix_linked_list ) == self ) {
107- // I'm at the front; splice myself out.
108- MP_STATE_VM (keypad_keymatrix_linked_list ) = self -> next ;
109- } else {
110- keypad_keymatrix_obj_t * current = MP_STATE_VM (keypad_keymatrix_linked_list );
111- while (current ) {
112- if (current -> next == self ) {
113- // Splice myself out.
114- current -> next = self -> next ;
115- break ;
116- }
117- current = current -> next ;
118- }
119- }
120- supervisor_release_lock (& keypad_keymatrix_linked_list_lock );
12199}
122100
123101bool common_hal_keypad_keymatrix_deinited (keypad_keymatrix_obj_t * self ) {
@@ -160,7 +138,7 @@ mp_uint_t common_hal_keypad_keymatrix_key_num(keypad_keymatrix_obj_t *self, mp_u
160138 return row_col_to_key_num (self , row , col );
161139}
162140
163- static void keypad_keymatrix_scan (keypad_keymatrix_obj_t * self ) {
141+ void keypad_keymatrix_scan (keypad_keymatrix_obj_t * self ) {
164142 uint64_t now = port_get_raw_ticks (NULL );
165143 if (now - self -> last_scan_ticks < DEBOUNCE_TICKS ) {
166144 // Too soon. Wait longer to debounce.
@@ -201,28 +179,3 @@ static void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
201179 common_hal_digitalio_digitalinout_switch_to_input (self -> row_digitalinouts -> items [row ], PULL_UP );
202180 }
203181}
204-
205- void keypad_keymatrix_tick (void ) {
206- // Fast path.
207- if (!MP_STATE_VM (keypad_keymatrix_linked_list )) {
208- return ;
209- }
210-
211- if (supervisor_try_lock (& keypad_keymatrix_linked_list_lock )) {
212- keypad_keymatrix_obj_t * keypad_keymatrix = MP_STATE_VM (keypad_keymatrix_linked_list );
213- while (keypad_keymatrix ) {
214- keypad_keymatrix_scan (keypad_keymatrix );
215- keypad_keymatrix = keypad_keymatrix -> next ;
216- }
217- supervisor_release_lock (& keypad_keymatrix_linked_list_lock );
218- }
219- }
220-
221- void keypad_keymatrix_reset (void ) {
222- if (MP_STATE_VM (keypad_keymatrix_linked_list )) {
223- supervisor_disable_tick ();
224- }
225-
226- MP_STATE_VM (keypad_keys_linked_list ) = NULL ;
227- keypad_keymatrix_linked_list_lock = false;
228- }
0 commit comments