Skip to content

Commit 350652e

Browse files
committed
Keys works; more testing to do
1 parent 32eec85 commit 350652e

File tree

9 files changed

+41
-30
lines changed

9 files changed

+41
-30
lines changed

shared-bindings/keypad/Event.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,17 @@ STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = {
117117
};
118118
STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table);
119119

120+
STATIC void keypad_event_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
121+
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
122+
mp_printf(print, "<Event: keynum %d %s>",
123+
common_hal_keypad_event_get_key_num(self),
124+
common_hal_keypad_event_get_pressed(self) ? "pressed" : "released");
125+
}
126+
120127
const mp_obj_type_t keypad_event_type = {
121128
{ &mp_type_type },
122-
.name = MP_QSTR_UART,
129+
.name = MP_QSTR_Event,
123130
.make_new = keypad_event_make_new,
131+
.print = keypad_event_print,
124132
.locals_dict = (mp_obj_dict_t *)&keypad_event_locals_dict,
125133
};

shared-bindings/keypad/Event.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_event_type;
3434

35-
void common_hal_keypad_event_construct(keypad_event_obj_t *self, uint16_t key_num, bool pressed);
35+
void common_hal_keypad_event_construct(keypad_event_obj_t *self, mp_uint_t key_num, bool pressed);
3636
mp_int_t common_hal_keypad_event_get_key_num(keypad_event_obj_t *self);
3737
bool common_hal_keypad_event_get_pressed(keypad_event_obj_t *self);
3838
bool common_hal_keypad_event_get_released(keypad_event_obj_t *self);

shared-bindings/keypad/KeyMatrix.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ STATIC mp_obj_t keypad_keymatrix_pressed(mp_obj_t self_in, mp_obj_t key_num_in)
174174
check_for_deinit(self);
175175

176176
mp_int_t key_num = mp_obj_get_int(key_num_in);
177-
if (key_num < 0 || key_num >= common_hal_keypad_keymatrix_num_keys(self)) {
177+
if (key_num < 0 || (size_t)key_num >= common_hal_keypad_keymatrix_num_keys(self)) {
178178
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_key_num);
179179
}
180180

@@ -193,12 +193,12 @@ STATIC mp_obj_t keypad_keymatrix_key_num(mp_obj_t self_in, mp_obj_t row_in, mp_o
193193
check_for_deinit(self);
194194

195195
const mp_int_t row = mp_obj_get_int(row_in);
196-
if (row < 0 || row >= common_hal_keypad_keymatrix_num_rows(self)) {
196+
if (row < 0 || (size_t)row >= common_hal_keypad_keymatrix_num_rows(self)) {
197197
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_row_num);
198198
}
199199

200200
const mp_int_t col = mp_obj_get_int(col_in);
201-
if (col < 0 || col >= common_hal_keypad_keymatrix_num_cols(self)) {
201+
if (col < 0 || (size_t)col >= common_hal_keypad_keymatrix_num_cols(self)) {
202202
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_col_num);
203203
}
204204

shared-bindings/keypad/Keys.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ STATIC void check_for_deinit(keypad_keys_obj_t *self) {
141141
//| """
142142
//| ...
143143
//|
144-
STATIC mp_obj_t keypad_keys_next_event(mp_obj_t self_in, mp_obj_t event_in) {
144+
STATIC mp_obj_t keypad_keys_next_event(mp_obj_t self_in) {
145145
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
146146
check_for_deinit(self);
147147

148148
return common_hal_keypad_keys_next_event(self);
149149
}
150-
MP_DEFINE_CONST_FUN_OBJ_2(keypad_keys_next_event_obj, keypad_keys_next_event);
150+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_next_event_obj, keypad_keys_next_event);
151151

152152
//| def clear_events(self) -> None:
153153
//| """Clear any queued key transition events.
@@ -174,7 +174,7 @@ STATIC mp_obj_t keypad_keys_pressed(mp_obj_t self_in, mp_obj_t key_num_in) {
174174
check_for_deinit(self);
175175

176176
mp_int_t key_num = mp_obj_get_int(key_num_in);
177-
if (key_num < 0 || key_num >= common_hal_keypad_keys_num_keys(self)) {
177+
if (key_num < 0 || (size_t)key_num >= common_hal_keypad_keys_num_keys(self)) {
178178
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_key_num);
179179
}
180180

shared-module/keypad/Event.c

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

2929
void common_hal_keypad_event_construct(keypad_event_obj_t *self, mp_uint_t key_num, bool pressed) {
3030
self->key_num = key_num;
31-
self->pressed = true;
31+
self->pressed = pressed;
3232
}
3333

3434
mp_int_t common_hal_keypad_event_get_key_num(keypad_event_obj_t *self) {

shared-module/keypad/KeyMatrix.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint
7373
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_row_pins * num_col_pins, false, false);
7474

7575
// Event queue is 16-bit values.
76-
ringbuf_alloc(self->encoded_events, max_events * 2, false);
76+
ringbuf_alloc(&self->encoded_events, max_events * 2, false);
7777

7878
// Add self to the list of active Keys objects.
7979

@@ -139,7 +139,7 @@ bool common_hal_keypad_keymatrix_pressed(keypad_keymatrix_obj_t *self, mp_uint_t
139139
}
140140

141141
mp_obj_t common_hal_keypad_keymatrix_next_event(keypad_keymatrix_obj_t *self) {
142-
int encoded_event = ringbuf_get16(self->encoded_events);
142+
int encoded_event = ringbuf_get16(&self->encoded_events);
143143
if (encoded_event == -1) {
144144
return MP_ROM_NONE;
145145
}
@@ -151,7 +151,7 @@ mp_obj_t common_hal_keypad_keymatrix_next_event(keypad_keymatrix_obj_t *self) {
151151
}
152152

153153
void common_hal_keypad_keymatrix_clear_events(keypad_keymatrix_obj_t *self) {
154-
ringbuf_clear(self->encoded_events);
154+
ringbuf_clear(&self->encoded_events);
155155
}
156156

157157
mp_uint_t common_hal_keypad_keymatrix_key_num(keypad_keymatrix_obj_t *self, mp_uint_t row, mp_uint_t col) {
@@ -185,11 +185,11 @@ static void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
185185

186186
// Record any transitions.
187187
if (previous != current) {
188-
if (ringbuf_num_empty(self->encoded_events) == 0) {
188+
if (ringbuf_num_empty(&self->encoded_events) == 0) {
189189
// Discard oldest if full.
190-
ringbuf_get16(self->encoded_events);
190+
ringbuf_get16(&self->encoded_events);
191191
}
192-
ringbuf_put16(self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED));
192+
ringbuf_put16(&self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED));
193193
}
194194

195195
}

shared-module/keypad/KeyMatrix.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ typedef struct _keypad_keymatrix_obj_t {
4040
uint64_t last_scan_ticks;
4141
bool *previously_pressed;
4242
bool *currently_pressed;
43-
ringbuf_t *encoded_events;
43+
ringbuf_t encoded_events;
4444
// Keep a linked list of active KeyMatrix objects.
4545
struct _keypad_keymatrix_obj_t *next;
4646
} keypad_keymatrix_obj_t;
4747

48-
void keypad_keymatrix_tick();
49-
void keypad_keymatrix_reset();
48+
void keypad_keymatrix_tick(void);
49+
void keypad_keymatrix_reset(void);
5050

5151
#endif // MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_KEYMATRIX_H

shared-module/keypad/Keys.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,19 @@ void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pin
5959
self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false);
6060
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false);
6161
self->value_when_pressed = value_when_pressed;
62+
self->last_scan_ticks = port_get_raw_ticks(NULL);
6263

6364
// Event queue is 16-bit values.
64-
ringbuf_alloc(self->encoded_events, max_events * 2, false);
65+
ringbuf_alloc(&self->encoded_events, max_events * 2, false);
6566

6667
// Add self to the list of active Keys objects.
6768

6869
supervisor_acquire_lock(&keypad_keys_linked_list_lock);
6970
self->next = MP_STATE_VM(keypad_keys_linked_list);
7071
MP_STATE_VM(keypad_keys_linked_list) = self;
7172
supervisor_release_lock(&keypad_keys_linked_list_lock);
73+
74+
supervisor_enable_tick();
7275
}
7376

7477
void common_hal_keypad_keys_deinit(keypad_keys_obj_t *self) {
@@ -113,19 +116,19 @@ bool common_hal_keypad_keys_pressed(keypad_keys_obj_t *self, mp_uint_t key_num)
113116
}
114117

115118
mp_obj_t common_hal_keypad_keys_next_event(keypad_keys_obj_t *self) {
116-
int encoded_event = ringbuf_get16(self->encoded_events);
119+
int encoded_event = ringbuf_get16(&self->encoded_events);
117120
if (encoded_event == -1) {
118121
return MP_ROM_NONE;
119122
}
120123

121124
keypad_event_obj_t *event = m_new_obj(keypad_event_obj_t);
122-
self->base.type = &keypad_event_type;
125+
event->base.type = &keypad_event_type;
123126
common_hal_keypad_event_construct(event, encoded_event & EVENT_KEY_NUM_MASK, encoded_event & EVENT_PRESSED);
124127
return MP_OBJ_FROM_PTR(event);
125128
}
126129

127130
void common_hal_keypad_keys_clear_events(keypad_keys_obj_t *self) {
128-
ringbuf_clear(self->encoded_events);
131+
ringbuf_clear(&self->encoded_events);
129132
}
130133

131134
static void keypad_keys_scan(keypad_keys_obj_t *self) {
@@ -143,17 +146,17 @@ static void keypad_keys_scan(keypad_keys_obj_t *self) {
143146
self->previously_pressed[key_num] = previous;
144147

145148
// Get the current state.
146-
const bool current = common_hal_digitalio_digitalinout_get_value(self->digitalinouts->items[key_num]) ==
149+
const bool current =
150+
common_hal_digitalio_digitalinout_get_value(self->digitalinouts->items[key_num]) ==
147151
self->value_when_pressed;
148152
self->currently_pressed[key_num] = current;
149-
150153
// Record any transitions.
151154
if (previous != current) {
152-
if (ringbuf_num_empty(self->encoded_events) == 0) {
155+
if (ringbuf_num_empty(&self->encoded_events) == 0) {
153156
// Discard oldest if full.
154-
ringbuf_get16(self->encoded_events);
157+
ringbuf_get16(&self->encoded_events);
155158
}
156-
ringbuf_put16(self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED));
159+
ringbuf_put16(&self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED));
157160
}
158161
}
159162
}

shared-module/keypad/Keys.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ typedef struct _keypad_keys_obj_t {
4040
bool value_when_pressed;
4141
bool *previously_pressed;
4242
bool *currently_pressed;
43-
ringbuf_t *encoded_events;
43+
ringbuf_t encoded_events;
4444
// Keep a linked list of active Keys objects.
4545
struct _keypad_keys_obj_t *next;
4646
} keypad_keys_obj_t;
4747

48-
void keypad_keys_tick();
49-
void keypad_keys_reset();
48+
void keypad_keys_tick(void);
49+
void keypad_keys_reset(void);
5050

5151
#endif // MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_KEYS_H

0 commit comments

Comments
 (0)