Keys works; more testing to do

This commit is contained in:
Dan Halbert 2021-06-14 22:59:17 -04:00
parent 32eec85230
commit 350652ee21
9 changed files with 41 additions and 30 deletions

View File

@ -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, "<Event: keynum %d %s>",
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,
};

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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) {

View File

@ -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));
}
}

View File

@ -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

View File

@ -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));
}
}
}

View File

@ -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