Skip to content

Commit 5fcd90d

Browse files
author
Marco van der Kolk
committed
Extended to support multiple data pins
Signed-off-by: Marco van der Kolk <[email protected]>
1 parent 65f4106 commit 5fcd90d

File tree

4 files changed

+133
-31
lines changed

4 files changed

+133
-31
lines changed

shared-bindings/keypad/ShiftRegisterKeys.c

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
//| :param microcontroller.Pin clock: The shift register clock pin.
6363
//| The shift register should clock on a low-to-high transition.
6464
//| :param microcontroller.Pin data: the incoming shift register data pin
65+
//| :param Sequence[microcontroller.Pin] data: a list of incoming shift register data pins
6566
//| :param microcontroller.Pin latch:
6667
//| Pin used to latch parallel data going into the shift register.
6768
//| :param bool value_to_latch: Pin state to latch data being read.
@@ -70,6 +71,7 @@
7071
//| The default is ``True``, which is how the 74HC165 operates. The CD4021 latch is the opposite.
7172
//| Once the data is latched, it will be shifted out by toggling the clock pin.
7273
//| :param int key_count: number of data lines to clock in
74+
//| :param Sequence[int] key_count: a list of key_counts equal sized to data pins
7375
//| :param bool value_when_pressed: ``True`` if the pin reads high when the key is pressed.
7476
//| ``False`` if the pin reads low (is grounded) when the key is pressed.
7577
//| :param float interval: Scan keys no more often than ``interval`` to allow for debouncing.
@@ -91,29 +93,72 @@ STATIC mp_obj_t keypad_shiftregisterkeys_make_new(const mp_obj_type_t *type, siz
9193
{ MP_QSTR_data, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
9294
{ MP_QSTR_latch, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
9395
{ MP_QSTR_value_to_latch, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
94-
{ MP_QSTR_key_count, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
96+
{ MP_QSTR_key_count, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
9597
{ MP_QSTR_value_when_pressed, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
9698
{ MP_QSTR_interval, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
9799
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
98100
};
99101
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
100102
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
101103

104+
size_t num_data_pins;
105+
106+
if (mp_obj_is_type(args[ARG_data].u_obj, &mcu_pin_type)) {
107+
num_data_pins = 1;
108+
} else {
109+
num_data_pins = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(args[ARG_data].u_obj));
110+
}
111+
112+
const mcu_pin_obj_t *data_pins_array[num_data_pins];
113+
114+
if (mp_obj_is_type(args[ARG_data].u_obj, &mcu_pin_type)) {
115+
const mcu_pin_obj_t *datapin = validate_obj_is_free_pin(args[ARG_data].u_obj, MP_QSTR_data);
116+
data_pins_array[0] = datapin;
117+
} else {
118+
for (size_t pin = 0; pin < num_data_pins; pin++) {
119+
const mcu_pin_obj_t *datapin =
120+
validate_obj_is_free_pin(mp_obj_subscr(args[ARG_data].u_obj, MP_OBJ_NEW_SMALL_INT(pin), MP_OBJ_SENTINEL), MP_QSTR_data);
121+
data_pins_array[pin] = datapin;
122+
}
123+
}
124+
125+
size_t num_key_counts;
126+
127+
if (mp_obj_is_int(args[ARG_key_count].u_obj)) {
128+
num_key_counts = 1;
129+
} else {
130+
num_key_counts = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(args[ARG_key_count].u_obj));
131+
}
132+
133+
mp_arg_validate_length(num_key_counts, num_data_pins, MP_QSTR_key_count);
134+
135+
size_t key_count_array[num_key_counts];
136+
137+
if (mp_obj_is_int(args[ARG_key_count].u_obj)) {
138+
const size_t key_count = (size_t)mp_arg_validate_int_min(args[ARG_key_count].u_int, 1, MP_QSTR_key_count);
139+
key_count_array[0] = key_count;
140+
} else {
141+
for (size_t kc = 0; kc < num_key_counts; kc++) {
142+
mp_int_t mpint = mp_obj_get_int(mp_obj_subscr(args[ARG_key_count].u_obj, MP_OBJ_NEW_SMALL_INT(kc), MP_OBJ_SENTINEL));
143+
const size_t key_count = (size_t)mp_arg_validate_int_min(mpint, 1, MP_QSTR_key_count);
144+
key_count_array[kc] = key_count;
145+
}
146+
}
147+
102148
const mcu_pin_obj_t *clock = validate_obj_is_free_pin(args[ARG_clock].u_obj, MP_QSTR_clock);
103-
const mcu_pin_obj_t *data = validate_obj_is_free_pin(args[ARG_data].u_obj, MP_QSTR_data);
104149
const mcu_pin_obj_t *latch = validate_obj_is_free_pin(args[ARG_latch].u_obj, MP_QSTR_latch);
105150
const bool value_to_latch = args[ARG_value_to_latch].u_bool;
106151

107-
const size_t key_count = (size_t)mp_arg_validate_int_min(args[ARG_key_count].u_int, 1, MP_QSTR_key_count);
108152
const bool value_when_pressed = args[ARG_value_when_pressed].u_bool;
109153
const mp_float_t interval =
110154
mp_arg_validate_obj_float_non_negative(args[ARG_interval].u_obj, 0.020f, MP_QSTR_interval);
111155
const size_t max_events = (size_t)mp_arg_validate_int_min(args[ARG_max_events].u_int, 1, MP_QSTR_max_events);
112156

113157
common_hal_keypad_shiftregisterkeys_construct(
114-
self, clock, data, latch, value_to_latch, key_count, value_when_pressed, interval, max_events);
158+
self, clock, num_data_pins, data_pins_array, latch, value_to_latch, num_key_counts, key_count_array, value_when_pressed, interval, max_events);
115159

116160
return MP_OBJ_FROM_PTR(self);
161+
117162
#else
118163
mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_ShiftRegisterKeys);
119164
#endif

shared-bindings/keypad/ShiftRegisterKeys.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
extern const mp_obj_type_t keypad_shiftregisterkeys_type;
3434

35-
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);
35+
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, size_t num_key_count, size_t key_counts[], bool value_when_pressed, mp_float_t interval, size_t max_events);
3636

3737
void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self);
3838

shared-module/keypad/ShiftRegisterKeys.c

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

97128
size_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

102141
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) {
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.

shared-module/keypad/ShiftRegisterKeys.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
typedef struct {
3838
KEYPAD_SCANNER_COMMON_FIELDS;
3939
digitalio_digitalinout_obj_t *clock;
40-
digitalio_digitalinout_obj_t *data;
4140
digitalio_digitalinout_obj_t *latch;
42-
size_t key_count;
41+
mp_obj_tuple_t *data;
42+
mp_uint_t *key_counts;
43+
mp_uint_t num_key_counts;
44+
mp_uint_t max_key_count;
4345
bool value_when_pressed;
4446
bool value_to_latch;
4547
} keypad_shiftregisterkeys_obj_t;

0 commit comments

Comments
 (0)