Skip to content

Commit 74eb360

Browse files
authored
Merge pull request #8143 from kolkmvd/ShiftRegister-multi-data-pin
Extended ShiftRegisterKeys to support multiple data pins with shared clock and latch
2 parents 3ec9f8a + bdf9336 commit 74eb360

File tree

4 files changed

+136
-39
lines changed

4 files changed

+136
-39
lines changed

shared-bindings/keypad/ShiftRegisterKeys.c

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
//| self,
4242
//| *,
4343
//| clock: microcontroller.Pin,
44-
//| data: microcontroller.Pin,
44+
//| data: Union[microcontroller.Pin, Sequence[microcontroller.Pin]],
4545
//| latch: microcontroller.Pin,
4646
//| value_to_latch: bool = True,
47-
//| key_count: int,
47+
//| key_count: Union[int, Sequence[int]],
4848
//| value_when_pressed: bool,
4949
//| interval: float = 0.020,
5050
//| max_events: int = 64
@@ -53,23 +53,25 @@
5353
//| Create a `Keys` object that will scan keys attached to a parallel-in serial-out shift register
5454
//| like the 74HC165 or CD4021.
5555
//| Note that you may chain shift registers to load in as many values as you need.
56+
//| Furthermore, you can put multiple shift registers in parallel and share clock and latch.
5657
//|
5758
//| Key number 0 is the first (or more properly, the zero-th) bit read. In the
5859
//| 74HC165, this bit is labeled ``Q7``. Key number 1 will be the value of ``Q6``, etc.
60+
//| With multiple data pins, key numbers of the next pin are sequentially to the current pin.
5961
//|
6062
//| An `EventQueue` is created when this object is created and is available in the `events` attribute.
6163
//|
6264
//| :param microcontroller.Pin clock: The shift register clock pin.
6365
//| The shift register should clock on a low-to-high transition.
64-
//| :param microcontroller.Pin data: the incoming shift register data pin
66+
//| :param Union[microcontroller.Pin, Sequence[microcontroller.Pin]] data: the incoming shift register data pin(s)
6567
//| :param microcontroller.Pin latch:
6668
//| Pin used to latch parallel data going into the shift register.
6769
//| :param bool value_to_latch: Pin state to latch data being read.
6870
//| ``True`` if the data is latched when ``latch`` goes high
6971
//| ``False`` if the data is latched when ``latch`` goes low.
7072
//| The default is ``True``, which is how the 74HC165 operates. The CD4021 latch is the opposite.
7173
//| Once the data is latched, it will be shifted out by toggling the clock pin.
72-
//| :param int key_count: number of data lines to clock in
74+
//| :param Union[int, Sequence[int]] key_count: number of data lines to clock in (per data pin)
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
@@ -155,7 +200,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_shiftregisterkeys___exit___obj
155200
//| ...
156201

157202
//| key_count: int
158-
//| """The number of keys that are being scanned. (read-only)
203+
//| """The total number of keys that are being scanned. (read-only)
159204
//| """
160205

161206
//| events: EventQueue

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: 77 additions & 27 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_pins = 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,28 @@ 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

88-
common_hal_digitalio_digitalinout_deinit(self->data);
89-
self->data = MP_ROM_NONE;
90-
91112
common_hal_digitalio_digitalinout_deinit(self->latch);
92113
self->latch = MP_ROM_NONE;
93114

115+
for (size_t key = 0; key < self->data_pins->len; key++) {
116+
common_hal_digitalio_digitalinout_deinit(self->data_pins->items[key]);
117+
}
118+
self->data_pins = MP_ROM_NONE;
119+
self->key_counts = MP_ROM_NONE;
120+
94121
common_hal_keypad_deinit_core(self);
95122
}
96123

97124
size_t shiftregisterkeys_get_key_count(void *self_in) {
98125
keypad_shiftregisterkeys_obj_t *self = self_in;
99-
return self->key_count;
126+
127+
size_t total = 0;
128+
129+
for (mp_uint_t i = 0; i < self->num_key_counts; i++) {
130+
total += self->key_counts[i];
131+
}
132+
133+
return total;
100134
}
101135

102136
static void shiftregisterkeys_scan_now(void *self_in, mp_obj_t timestamp) {
@@ -105,28 +139,44 @@ static void shiftregisterkeys_scan_now(void *self_in, mp_obj_t timestamp) {
105139
// Latch (freeze) the current state of the input pins.
106140
common_hal_digitalio_digitalinout_set_value(self->latch, self->value_to_latch);
107141

108-
const size_t key_count = shiftregisterkeys_get_key_count(self);
142+
// Scan for max_key_count bit
143+
for (mp_uint_t scan_number = 0; scan_number < self->max_key_count; scan_number++) {
144+
common_hal_digitalio_digitalinout_set_value(self->clock, false);
109145

110-
for (mp_uint_t key_number = 0; key_number < key_count; key_number++) {
111146
// Zero-th data appears on on the data pin immediately, without shifting.
112-
common_hal_digitalio_digitalinout_set_value(self->clock, false);
113147

114-
// Remember the previous up/down state.
115-
const bool previous = self->currently_pressed[key_number];
116-
self->previously_pressed[key_number] = previous;
148+
// Loop through all the data pins that share the latch
149+
mp_uint_t index = 0;
117150

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;
151+
for (mp_uint_t i = 0; i < self->data_pins->len; i++) {
152+
153+
// When this data pin has less shiftable bits, ignore it
154+
if (scan_number >= self->key_counts[i]) {
155+
continue;
156+
}
157+
158+
mp_uint_t key_number = scan_number + index;
159+
160+
// Remember the previous up/down state.
161+
const bool previous = self->currently_pressed[key_number];
162+
self->previously_pressed[key_number] = previous;
163+
164+
// Get the current state.
165+
const bool current =
166+
common_hal_digitalio_digitalinout_get_value(self->data_pins->items[i]) == self->value_when_pressed;
167+
self->currently_pressed[key_number] = current;
168+
169+
// Record any transitions.
170+
if (previous != current) {
171+
keypad_eventqueue_record(self->events, key_number, current, timestamp);
172+
}
173+
174+
index += self->key_counts[i];
175+
}
122176

123177
// Trigger a shift to get the next bit.
124178
common_hal_digitalio_digitalinout_set_value(self->clock, true);
125179

126-
// Record any transitions.
127-
if (previous != current) {
128-
keypad_eventqueue_record(self->events, key_number, current, timestamp);
129-
}
130180
}
131181

132182
// 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_pins;
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)