Skip to content

Commit 203dad1

Browse files
committed
keypad: scan immediately on construct, .reset()
1 parent 47d3d0d commit 203dad1

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

shared-module/keypad/KeyMatrix.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include "supervisor/port.h"
3838
#include "supervisor/shared/tick.h"
3939

40+
static void keypad_keymatrix_scan_now(keypad_keymatrix_obj_t *self, uint64_t now);
41+
4042
static mp_uint_t row_column_to_key_number(keypad_keymatrix_obj_t *self, mp_uint_t row, mp_uint_t column) {
4143
return row * self->column_digitalinouts->len + column;
4244
}
@@ -74,10 +76,10 @@ void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint
7476
self->events = events;
7577

7678
self->interval_ticks = (mp_uint_t)(interval * 1024); // interval * 1000 * (1024/1000)
77-
self->last_scan_ticks = port_get_raw_ticks(NULL);
7879

7980
// Add self to the list of active keypad scanners.
8081
keypad_register_scanner((keypad_scanner_obj_t *)self);
82+
keypad_keymatrix_scan_now(self, port_get_raw_ticks(NULL));
8183
}
8284

8385
void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self) {
@@ -132,10 +134,9 @@ mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self) {
132134
void common_hal_keypad_keymatrix_reset(keypad_keymatrix_obj_t *self) {
133135
const size_t key_count = common_hal_keypad_keymatrix_get_key_count(self);
134136

135-
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
136137
memset(self->previously_pressed, false, key_count);
137138
memset(self->currently_pressed, false, key_count);
138-
supervisor_release_lock(&keypad_scanners_linked_list_lock);
139+
keypad_keymatrix_scan_now(self, port_get_raw_ticks(NULL));
139140
}
140141

141142
void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
@@ -145,6 +146,10 @@ void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
145146
return;
146147
}
147148

149+
keypad_keymatrix_scan_now(self, now);
150+
}
151+
152+
static void keypad_keymatrix_scan_now(keypad_keymatrix_obj_t *self, uint64_t now) {
148153
self->last_scan_ticks = now;
149154

150155
mp_obj_t timestamp = supervisor_ticks_ms();

shared-module/keypad/Keys.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "supervisor/port.h"
3737
#include "supervisor/shared/tick.h"
3838

39+
static void keypad_keys_scan_now(keypad_keys_obj_t *self, uint64_t now);
40+
3941
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, const mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull, mp_float_t interval, size_t max_events) {
4042
mp_obj_t dios[num_pins];
4143

@@ -64,6 +66,7 @@ void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pin
6466

6567
// Add self to the list of active keypad scanners.
6668
keypad_register_scanner((keypad_scanner_obj_t *)self);
69+
keypad_keys_scan_now(self, port_get_raw_ticks(NULL));
6770
}
6871

6972
void common_hal_keypad_keys_deinit(keypad_keys_obj_t *self) {
@@ -96,10 +99,9 @@ mp_obj_t common_hal_keypad_keys_get_events(keypad_keys_obj_t *self) {
9699
void common_hal_keypad_keys_reset(keypad_keys_obj_t *self) {
97100
const size_t key_count = common_hal_keypad_keys_get_key_count(self);
98101

99-
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
100102
memset(self->previously_pressed, false, key_count);
101103
memset(self->currently_pressed, false, key_count);
102-
supervisor_release_lock(&keypad_scanners_linked_list_lock);
104+
keypad_keys_scan_now(self, port_get_raw_ticks(NULL));
103105
}
104106

105107
void keypad_keys_scan(keypad_keys_obj_t *self) {
@@ -109,6 +111,10 @@ void keypad_keys_scan(keypad_keys_obj_t *self) {
109111
return;
110112
}
111113

114+
keypad_keys_scan_now(self, now);
115+
}
116+
117+
static void keypad_keys_scan_now(keypad_keys_obj_t *self, uint64_t now) {
112118
self->last_scan_ticks = now;
113119

114120
const size_t key_count = common_hal_keypad_keys_get_key_count(self);

shared-module/keypad/ShiftRegisterKeys.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "supervisor/port.h"
3737
#include "supervisor/shared/tick.h"
3838

39+
static void keypad_shiftregisterkeys_scan_now(keypad_shiftregisterkeys_obj_t *self, uint64_t now);
40+
3941
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) {
4042

4143
digitalio_digitalinout_obj_t *clock = m_new_obj(digitalio_digitalinout_obj_t);
@@ -63,7 +65,6 @@ void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_
6365
self->key_count = key_count;
6466

6567
self->interval_ticks = (mp_uint_t)(interval * 1024); // interval * 1000 * (1024/1000)
66-
self->last_scan_ticks = port_get_raw_ticks(NULL);
6768

6869
keypad_eventqueue_obj_t *events = m_new_obj(keypad_eventqueue_obj_t);
6970
events->base.type = &keypad_eventqueue_type;
@@ -72,6 +73,7 @@ void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_
7273

7374
// Add self to the list of active keypad scanners.
7475
keypad_register_scanner((keypad_scanner_obj_t *)self);
76+
keypad_shiftregisterkeys_scan_now(self, port_get_raw_ticks(NULL));
7577
}
7678

7779
void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self) {
@@ -108,10 +110,9 @@ mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys
108110
void common_hal_keypad_shiftregisterkeys_reset(keypad_shiftregisterkeys_obj_t *self) {
109111
const size_t key_count = common_hal_keypad_shiftregisterkeys_get_key_count(self);
110112

111-
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
112113
memset(self->previously_pressed, false, key_count);
113114
memset(self->currently_pressed, false, key_count);
114-
supervisor_release_lock(&keypad_scanners_linked_list_lock);
115+
keypad_shiftregisterkeys_scan_now(self, port_get_raw_ticks(NULL));
115116
}
116117

117118
void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self) {
@@ -121,6 +122,10 @@ void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self) {
121122
return;
122123
}
123124

125+
keypad_shiftregisterkeys_scan_now(self, now);
126+
}
127+
128+
static void keypad_shiftregisterkeys_scan_now(keypad_shiftregisterkeys_obj_t *self, uint64_t now) {
124129
self->last_scan_ticks = now;
125130

126131
mp_obj_t timestamp = supervisor_ticks_ms();

0 commit comments

Comments
 (0)