@@ -44,30 +44,54 @@ static keypad_scanner_funcs_t shiftregisterkeys_funcs = {
4444 .get_key_count = shiftregisterkeys_get_key_count ,
4545};
4646
47- 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 ) {
47+ void common_hal_keypad_shiftregisterkeys_construct (keypad_shiftregisterkeys_obj_t * self , const mcu_pin_obj_t * clock_pin , mp_uint_t num_data_pins , const mcu_pin_obj_t * data_pins [] , const mcu_pin_obj_t * latch_pin , bool value_to_latch , mp_uint_t num_key_counts , size_t key_counts [] , bool value_when_pressed , mp_float_t interval , size_t max_events ) {
4848
4949 digitalio_digitalinout_obj_t * clock = m_new_obj (digitalio_digitalinout_obj_t );
5050 clock -> base .type = & digitalio_digitalinout_type ;
5151 common_hal_digitalio_digitalinout_construct (clock , clock_pin );
5252 common_hal_digitalio_digitalinout_switch_to_output (clock , false, DRIVE_MODE_PUSH_PULL );
5353 self -> clock = clock ;
5454
55- digitalio_digitalinout_obj_t * data = m_new_obj (digitalio_digitalinout_obj_t );
56- data -> base .type = & digitalio_digitalinout_type ;
57- common_hal_digitalio_digitalinout_construct (data , data_pin );
58- common_hal_digitalio_digitalinout_switch_to_input (data , PULL_NONE );
59- self -> data = data ;
60-
6155 digitalio_digitalinout_obj_t * latch = m_new_obj (digitalio_digitalinout_obj_t );
6256 latch -> base .type = & digitalio_digitalinout_type ;
6357
6458 common_hal_digitalio_digitalinout_construct (latch , latch_pin );
6559 common_hal_digitalio_digitalinout_switch_to_output (latch , true, DRIVE_MODE_PUSH_PULL );
6660 self -> latch = latch ;
67- self -> value_to_latch = value_to_latch ;
6861
62+ mp_obj_t dios [num_data_pins ];
63+
64+ for (size_t i = 0 ; i < num_data_pins ; i ++ ) {
65+ digitalio_digitalinout_obj_t * dio = m_new_obj (digitalio_digitalinout_obj_t );
66+ dio -> base .type = & digitalio_digitalinout_type ;
67+ common_hal_digitalio_digitalinout_construct (dio , data_pins [i ]);
68+ common_hal_digitalio_digitalinout_switch_to_input (dio , PULL_NONE );
69+ dios [i ] = dio ;
70+ }
71+
72+ // Allocate a tuple object with the data pins
73+ self -> data = mp_obj_new_tuple (num_data_pins , dios );
74+
75+ self -> key_counts = (mp_uint_t * )gc_alloc (sizeof (mp_uint_t ) * num_key_counts , false, false);
76+ self -> num_key_counts = num_key_counts ;
77+
78+ // copy to a gc_alloc() and on the fly record pin with largest Shift register
79+ mp_uint_t max = 0 ;
80+
81+ for (mp_uint_t i = 0 ; i < self -> num_key_counts ; i ++ ) {
82+ mp_uint_t cnt = key_counts [i ];
83+
84+ if (cnt > max ) {
85+ max = cnt ;
86+ }
87+
88+ self -> key_counts [i ] = cnt ;
89+ }
90+
91+ self -> max_key_count = max ;
92+
93+ self -> value_to_latch = value_to_latch ;
6994 self -> value_when_pressed = value_when_pressed ;
70- self -> key_count = key_count ;
7195 self -> funcs = & shiftregisterkeys_funcs ;
7296
7397 keypad_construct_common ((keypad_scanner_obj_t * )self , interval , max_events );
@@ -85,18 +109,33 @@ void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *
85109 common_hal_digitalio_digitalinout_deinit (self -> clock );
86110 self -> clock = MP_ROM_NONE ;
87111
112+ /*
88113 common_hal_digitalio_digitalinout_deinit(self->data);
89114 self->data = MP_ROM_NONE;
115+ */
90116
91117 common_hal_digitalio_digitalinout_deinit (self -> latch );
92118 self -> latch = MP_ROM_NONE ;
93119
120+ for (size_t key = 0 ; key < self -> datas -> len ; key ++ ) {
121+ common_hal_digitalio_digitalinout_deinit (self -> datas -> items [key ]);
122+ }
123+ self -> data = MP_ROM_NONE ;
124+
94125 common_hal_keypad_deinit_core (self );
95126}
96127
97128size_t shiftregisterkeys_get_key_count (void * self_in ) {
98129 keypad_shiftregisterkeys_obj_t * self = self_in ;
99- return self -> key_count ;
130+
131+ size_t total = 0 ;
132+
133+ for (mp_uint_t i = 0 ; i < self -> num_key_counts ; i ++ )
134+ {
135+ total += self -> key_counts [i ];
136+ }
137+
138+ return total ;
100139}
101140
102141static void shiftregisterkeys_scan_now (void * self_in , mp_obj_t timestamp ) {
@@ -105,28 +144,44 @@ static void shiftregisterkeys_scan_now(void *self_in, mp_obj_t timestamp) {
105144 // Latch (freeze) the current state of the input pins.
106145 common_hal_digitalio_digitalinout_set_value (self -> latch , self -> value_to_latch );
107146
108- const size_t key_count = shiftregisterkeys_get_key_count (self );
147+ // Scan for max_key_count bit
148+ for (mp_uint_t scan_number = 0 ; scan_number < self -> max_key_count ; scan_number ++ ) {
149+ common_hal_digitalio_digitalinout_set_value (self -> clock , false);
109150
110- for (mp_uint_t key_number = 0 ; key_number < key_count ; key_number ++ ) {
111151 // Zero-th data appears on on the data pin immediately, without shifting.
112- common_hal_digitalio_digitalinout_set_value (self -> clock , false);
113152
114- // Remember the previous up/down state.
115- const bool previous = self -> currently_pressed [key_number ];
116- self -> previously_pressed [key_number ] = previous ;
153+ // Loop through all the data pins that share the latch
154+ mp_uint_t index = 0 ;
117155
118- // Get the current state.
119- const bool current =
120- common_hal_digitalio_digitalinout_get_value (self -> data ) == self -> value_when_pressed ;
121- self -> currently_pressed [key_number ] = current ;
156+ for (mp_uint_t i = 0 ; i < self -> datas -> len ; i ++ ) {
157+
158+ // When this data pin has less shiftable bits, ignore it
159+ if (scan_number >= self -> key_counts [i ]) {
160+ continue ;
161+ }
162+
163+ mp_uint_t key_number = scan_number + index ;
164+
165+ // Remember the previous up/down state.
166+ const bool previous = self -> currently_pressed [key_number ];
167+ self -> previously_pressed [key_number ] = previous ;
168+
169+ // Get the current state.
170+ const bool current =
171+ common_hal_digitalio_digitalinout_get_value (self -> datas -> items [i ]) == self -> value_when_pressed ;
172+ self -> currently_pressed [key_number ] = current ;
173+
174+ // Record any transitions.
175+ if (previous != current ) {
176+ keypad_eventqueue_record (self -> events , key_number , current , timestamp );
177+ }
178+
179+ index += self -> key_counts [i ];
180+ }
122181
123182 // Trigger a shift to get the next bit.
124183 common_hal_digitalio_digitalinout_set_value (self -> clock , true);
125184
126- // Record any transitions.
127- if (previous != current ) {
128- keypad_eventqueue_record (self -> events , key_number , current , timestamp );
129- }
130185 }
131186
132187 // Start reading the input pins again.
0 commit comments