From 350652ee214a0c3fed1c6c3394886728a48a9fa8 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 14 Jun 2021 22:59:17 -0400 Subject: [PATCH] Keys works; more testing to do --- shared-bindings/keypad/Event.c | 10 +++++++++- shared-bindings/keypad/Event.h | 2 +- shared-bindings/keypad/KeyMatrix.c | 6 +++--- shared-bindings/keypad/Keys.c | 6 +++--- shared-module/keypad/Event.c | 2 +- shared-module/keypad/KeyMatrix.c | 12 ++++++------ shared-module/keypad/KeyMatrix.h | 6 +++--- shared-module/keypad/Keys.c | 21 ++++++++++++--------- shared-module/keypad/Keys.h | 6 +++--- 9 files changed, 41 insertions(+), 30 deletions(-) diff --git a/shared-bindings/keypad/Event.c b/shared-bindings/keypad/Event.c index 218da5b82e..787b09d83b 100644 --- a/shared-bindings/keypad/Event.c +++ b/shared-bindings/keypad/Event.c @@ -117,9 +117,17 @@ STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table); +STATIC void keypad_event_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "", + common_hal_keypad_event_get_key_num(self), + common_hal_keypad_event_get_pressed(self) ? "pressed" : "released"); +} + const mp_obj_type_t keypad_event_type = { { &mp_type_type }, - .name = MP_QSTR_UART, + .name = MP_QSTR_Event, .make_new = keypad_event_make_new, + .print = keypad_event_print, .locals_dict = (mp_obj_dict_t *)&keypad_event_locals_dict, }; diff --git a/shared-bindings/keypad/Event.h b/shared-bindings/keypad/Event.h index 03e04eb6ee..23017f1dc5 100644 --- a/shared-bindings/keypad/Event.h +++ b/shared-bindings/keypad/Event.h @@ -32,7 +32,7 @@ extern const mp_obj_type_t keypad_event_type; -void common_hal_keypad_event_construct(keypad_event_obj_t *self, uint16_t key_num, bool pressed); +void common_hal_keypad_event_construct(keypad_event_obj_t *self, mp_uint_t key_num, bool pressed); mp_int_t common_hal_keypad_event_get_key_num(keypad_event_obj_t *self); bool common_hal_keypad_event_get_pressed(keypad_event_obj_t *self); bool common_hal_keypad_event_get_released(keypad_event_obj_t *self); diff --git a/shared-bindings/keypad/KeyMatrix.c b/shared-bindings/keypad/KeyMatrix.c index b6b09a9a0a..4e2038aa37 100644 --- a/shared-bindings/keypad/KeyMatrix.c +++ b/shared-bindings/keypad/KeyMatrix.c @@ -174,7 +174,7 @@ STATIC mp_obj_t keypad_keymatrix_pressed(mp_obj_t self_in, mp_obj_t key_num_in) check_for_deinit(self); mp_int_t key_num = mp_obj_get_int(key_num_in); - if (key_num < 0 || key_num >= common_hal_keypad_keymatrix_num_keys(self)) { + if (key_num < 0 || (size_t)key_num >= common_hal_keypad_keymatrix_num_keys(self)) { mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_key_num); } @@ -193,12 +193,12 @@ STATIC mp_obj_t keypad_keymatrix_key_num(mp_obj_t self_in, mp_obj_t row_in, mp_o check_for_deinit(self); const mp_int_t row = mp_obj_get_int(row_in); - if (row < 0 || row >= common_hal_keypad_keymatrix_num_rows(self)) { + if (row < 0 || (size_t)row >= common_hal_keypad_keymatrix_num_rows(self)) { mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_row_num); } const mp_int_t col = mp_obj_get_int(col_in); - if (col < 0 || col >= common_hal_keypad_keymatrix_num_cols(self)) { + if (col < 0 || (size_t)col >= common_hal_keypad_keymatrix_num_cols(self)) { mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_col_num); } diff --git a/shared-bindings/keypad/Keys.c b/shared-bindings/keypad/Keys.c index a54cff0ba9..849e2a639e 100644 --- a/shared-bindings/keypad/Keys.c +++ b/shared-bindings/keypad/Keys.c @@ -141,13 +141,13 @@ STATIC void check_for_deinit(keypad_keys_obj_t *self) { //| """ //| ... //| -STATIC mp_obj_t keypad_keys_next_event(mp_obj_t self_in, mp_obj_t event_in) { +STATIC mp_obj_t keypad_keys_next_event(mp_obj_t self_in) { keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); return common_hal_keypad_keys_next_event(self); } -MP_DEFINE_CONST_FUN_OBJ_2(keypad_keys_next_event_obj, keypad_keys_next_event); +MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_next_event_obj, keypad_keys_next_event); //| def clear_events(self) -> None: //| """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) { check_for_deinit(self); mp_int_t key_num = mp_obj_get_int(key_num_in); - if (key_num < 0 || key_num >= common_hal_keypad_keys_num_keys(self)) { + if (key_num < 0 || (size_t)key_num >= common_hal_keypad_keys_num_keys(self)) { mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_key_num); } diff --git a/shared-module/keypad/Event.c b/shared-module/keypad/Event.c index 6556d1f5a3..21b4ad18d3 100644 --- a/shared-module/keypad/Event.c +++ b/shared-module/keypad/Event.c @@ -28,7 +28,7 @@ void common_hal_keypad_event_construct(keypad_event_obj_t *self, mp_uint_t key_num, bool pressed) { self->key_num = key_num; - self->pressed = true; + self->pressed = pressed; } mp_int_t common_hal_keypad_event_get_key_num(keypad_event_obj_t *self) { diff --git a/shared-module/keypad/KeyMatrix.c b/shared-module/keypad/KeyMatrix.c index 4164de050f..04cb84facb 100644 --- a/shared-module/keypad/KeyMatrix.c +++ b/shared-module/keypad/KeyMatrix.c @@ -73,7 +73,7 @@ void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_row_pins * num_col_pins, false, false); // Event queue is 16-bit values. - ringbuf_alloc(self->encoded_events, max_events * 2, false); + ringbuf_alloc(&self->encoded_events, max_events * 2, false); // Add self to the list of active Keys objects. @@ -139,7 +139,7 @@ bool common_hal_keypad_keymatrix_pressed(keypad_keymatrix_obj_t *self, mp_uint_t } mp_obj_t common_hal_keypad_keymatrix_next_event(keypad_keymatrix_obj_t *self) { - int encoded_event = ringbuf_get16(self->encoded_events); + int encoded_event = ringbuf_get16(&self->encoded_events); if (encoded_event == -1) { return MP_ROM_NONE; } @@ -151,7 +151,7 @@ mp_obj_t common_hal_keypad_keymatrix_next_event(keypad_keymatrix_obj_t *self) { } void common_hal_keypad_keymatrix_clear_events(keypad_keymatrix_obj_t *self) { - ringbuf_clear(self->encoded_events); + ringbuf_clear(&self->encoded_events); } 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) { // Record any transitions. if (previous != current) { - if (ringbuf_num_empty(self->encoded_events) == 0) { + if (ringbuf_num_empty(&self->encoded_events) == 0) { // Discard oldest if full. - ringbuf_get16(self->encoded_events); + ringbuf_get16(&self->encoded_events); } - ringbuf_put16(self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED)); + ringbuf_put16(&self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED)); } } diff --git a/shared-module/keypad/KeyMatrix.h b/shared-module/keypad/KeyMatrix.h index d598f3941e..2051536898 100644 --- a/shared-module/keypad/KeyMatrix.h +++ b/shared-module/keypad/KeyMatrix.h @@ -40,12 +40,12 @@ typedef struct _keypad_keymatrix_obj_t { uint64_t last_scan_ticks; bool *previously_pressed; bool *currently_pressed; - ringbuf_t *encoded_events; + ringbuf_t encoded_events; // Keep a linked list of active KeyMatrix objects. struct _keypad_keymatrix_obj_t *next; } keypad_keymatrix_obj_t; -void keypad_keymatrix_tick(); -void keypad_keymatrix_reset(); +void keypad_keymatrix_tick(void); +void keypad_keymatrix_reset(void); #endif // MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_KEYMATRIX_H diff --git a/shared-module/keypad/Keys.c b/shared-module/keypad/Keys.c index d7e0288907..84e77e91eb 100644 --- a/shared-module/keypad/Keys.c +++ b/shared-module/keypad/Keys.c @@ -59,9 +59,10 @@ void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pin self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false); self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false); self->value_when_pressed = value_when_pressed; + self->last_scan_ticks = port_get_raw_ticks(NULL); // Event queue is 16-bit values. - ringbuf_alloc(self->encoded_events, max_events * 2, false); + ringbuf_alloc(&self->encoded_events, max_events * 2, false); // Add self to the list of active Keys objects. @@ -69,6 +70,8 @@ void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pin self->next = MP_STATE_VM(keypad_keys_linked_list); MP_STATE_VM(keypad_keys_linked_list) = self; supervisor_release_lock(&keypad_keys_linked_list_lock); + + supervisor_enable_tick(); } 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) } mp_obj_t common_hal_keypad_keys_next_event(keypad_keys_obj_t *self) { - int encoded_event = ringbuf_get16(self->encoded_events); + int encoded_event = ringbuf_get16(&self->encoded_events); if (encoded_event == -1) { return MP_ROM_NONE; } keypad_event_obj_t *event = m_new_obj(keypad_event_obj_t); - self->base.type = &keypad_event_type; + event->base.type = &keypad_event_type; common_hal_keypad_event_construct(event, encoded_event & EVENT_KEY_NUM_MASK, encoded_event & EVENT_PRESSED); return MP_OBJ_FROM_PTR(event); } void common_hal_keypad_keys_clear_events(keypad_keys_obj_t *self) { - ringbuf_clear(self->encoded_events); + ringbuf_clear(&self->encoded_events); } static void keypad_keys_scan(keypad_keys_obj_t *self) { @@ -143,17 +146,17 @@ static void keypad_keys_scan(keypad_keys_obj_t *self) { self->previously_pressed[key_num] = previous; // Get the current state. - const bool current = common_hal_digitalio_digitalinout_get_value(self->digitalinouts->items[key_num]) == + const bool current = + common_hal_digitalio_digitalinout_get_value(self->digitalinouts->items[key_num]) == self->value_when_pressed; self->currently_pressed[key_num] = current; - // Record any transitions. if (previous != current) { - if (ringbuf_num_empty(self->encoded_events) == 0) { + if (ringbuf_num_empty(&self->encoded_events) == 0) { // Discard oldest if full. - ringbuf_get16(self->encoded_events); + ringbuf_get16(&self->encoded_events); } - ringbuf_put16(self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED)); + ringbuf_put16(&self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED)); } } } diff --git a/shared-module/keypad/Keys.h b/shared-module/keypad/Keys.h index 556f465a98..ecf8165b9e 100644 --- a/shared-module/keypad/Keys.h +++ b/shared-module/keypad/Keys.h @@ -40,12 +40,12 @@ typedef struct _keypad_keys_obj_t { bool value_when_pressed; bool *previously_pressed; bool *currently_pressed; - ringbuf_t *encoded_events; + ringbuf_t encoded_events; // Keep a linked list of active Keys objects. struct _keypad_keys_obj_t *next; } keypad_keys_obj_t; -void keypad_keys_tick(); -void keypad_keys_reset(); +void keypad_keys_tick(void); +void keypad_keys_reset(void); #endif // MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_KEYS_H