@@ -44,30 +44,54 @@ static keypad_scanner_funcs_t shiftregisterkeys_funcs = {
44
44
.get_key_count = shiftregisterkeys_get_key_count ,
45
45
};
46
46
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 ) {
48
48
49
49
digitalio_digitalinout_obj_t * clock = m_new_obj (digitalio_digitalinout_obj_t );
50
50
clock -> base .type = & digitalio_digitalinout_type ;
51
51
common_hal_digitalio_digitalinout_construct (clock , clock_pin );
52
52
common_hal_digitalio_digitalinout_switch_to_output (clock , false, DRIVE_MODE_PUSH_PULL );
53
53
self -> clock = clock ;
54
54
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
-
61
55
digitalio_digitalinout_obj_t * latch = m_new_obj (digitalio_digitalinout_obj_t );
62
56
latch -> base .type = & digitalio_digitalinout_type ;
63
57
64
58
common_hal_digitalio_digitalinout_construct (latch , latch_pin );
65
59
common_hal_digitalio_digitalinout_switch_to_output (latch , true, DRIVE_MODE_PUSH_PULL );
66
60
self -> latch = latch ;
67
- self -> value_to_latch = value_to_latch ;
68
61
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 ;
69
94
self -> value_when_pressed = value_when_pressed ;
70
- self -> key_count = key_count ;
71
95
self -> funcs = & shiftregisterkeys_funcs ;
72
96
73
97
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 *
85
109
common_hal_digitalio_digitalinout_deinit (self -> clock );
86
110
self -> clock = MP_ROM_NONE ;
87
111
112
+ /*
88
113
common_hal_digitalio_digitalinout_deinit(self->data);
89
114
self->data = MP_ROM_NONE;
115
+ */
90
116
91
117
common_hal_digitalio_digitalinout_deinit (self -> latch );
92
118
self -> latch = MP_ROM_NONE ;
93
119
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
+
94
125
common_hal_keypad_deinit_core (self );
95
126
}
96
127
97
128
size_t shiftregisterkeys_get_key_count (void * self_in ) {
98
129
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 ;
100
139
}
101
140
102
141
static 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) {
105
144
// Latch (freeze) the current state of the input pins.
106
145
common_hal_digitalio_digitalinout_set_value (self -> latch , self -> value_to_latch );
107
146
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);
109
150
110
- for (mp_uint_t key_number = 0 ; key_number < key_count ; key_number ++ ) {
111
151
// Zero-th data appears on on the data pin immediately, without shifting.
112
- common_hal_digitalio_digitalinout_set_value (self -> clock , false);
113
152
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 ;
117
155
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
+ }
122
181
123
182
// Trigger a shift to get the next bit.
124
183
common_hal_digitalio_digitalinout_set_value (self -> clock , true);
125
184
126
- // Record any transitions.
127
- if (previous != current ) {
128
- keypad_eventqueue_record (self -> events , key_number , current , timestamp );
129
- }
130
185
}
131
186
132
187
// Start reading the input pins again.
0 commit comments