From 81f0763b804d88ed5584465e4f46e94396b0f263 Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Sat, 3 Feb 2024 12:26:04 -0500 Subject: [PATCH 1/5] Partially created module. --- components/modules/CMakeLists.txt | 1 + components/modules/Kconfig | 7 + components/modules/matrix.c | 574 ++++++++++++++++++++++++++++++ docs/modules/matrix.md | 79 ++++ 4 files changed, 661 insertions(+) create mode 100644 components/modules/matrix.c create mode 100644 docs/modules/matrix.md diff --git a/components/modules/CMakeLists.txt b/components/modules/CMakeLists.txt index c5e77a2a0..3719a39fc 100644 --- a/components/modules/CMakeLists.txt +++ b/components/modules/CMakeLists.txt @@ -17,6 +17,7 @@ set(module_srcs "i2c_hw_master.c" "i2c_hw_slave.c" "ledc.c" + "matrix.c" "mqtt.c" "net.c" "node.c" diff --git a/components/modules/Kconfig b/components/modules/Kconfig index 7741e37ba..510c6fdc2 100644 --- a/components/modules/Kconfig +++ b/components/modules/Kconfig @@ -164,6 +164,13 @@ menu "NodeMCU modules" help Includes the LEDC module. + config NODEMCU_CMODULE_MATRIX + bool "MATRIX module" + default "n" + select NODEMCU_CMODULE_GPIO + help + The matrix module provides support for cheap matrixed keypads like a 3x4 telephone keypad. + config NODEMCU_CMODULE_MQTT bool "MQTT module" default "n" diff --git a/components/modules/matrix.c b/components/modules/matrix.c new file mode 100644 index 000000000..12c988b79 --- /dev/null +++ b/components/modules/matrix.c @@ -0,0 +1,574 @@ +/* + * Module for interfacing with cheap matrix keyboards like telephone keypads + * + * The idea is to have pullups on all the rows, and drive the columns low. + * WHen a key is pressed, one of the rows will go low and trigger an interrupt. Disable + * all the row interrupts. + * Then we disable all the columns and then drive each column low in turn. Hopefully + * one of the rows will go low. This is a keypress. We only report the first keypress found. + * we start a timer to handle debounce. + * On timer expiry, see if any key is pressed, if so, just wait agin (maybe should use interrupts) + * If no key is pressed, run timer again. On timer expiry, re-enable interrupts. + * + * Philip Gladstone, N1DQ + */ + +#include "module.h" +#include "lauxlib.h" +#include "platform.h" +#include "task/task.h" +#include "esp_timer.h" +#include +#include +#include + + +#include + +#include "driver/gpio.h" + + + +#define MATRIX_PRESS_INDEX 0 +#define MATRIX_RELEASE_INDEX 1 + +#define MATRIX_ALL 0x3 + +#define CALLBACK_COUNT 2 +#define QUEUE_SIZE 8 + +typedef struct { + uint8_t column_count; + uint8_t row_count; + uint8_t *columns; + uint8_t *rows; + int character_ref; + int callback[CALLBACK_COUNT]; + esp_timer_handle_t timer_handle; + int8_t task_queued; + uint32_t read_offset; // Accessed by task + uint32_t write_offset; // Accessed by ISR + uint32_t last_press_change_time; + int tasknumber; + matrix_event_t queue[QUEUE_SIZE]; + void *callback_arg; +} DATA; + +static task_handle_t tasknumber; +static void lmatrix_timer_done(void *param); +static void lmatrix_check_timer(DATA *d, uint32_t time_us, bool dotimer); +// +// Queue is empty if read == write. +// However, we always want to keep the previous value +// so writing is only allowed if write - read < QUEUE_SIZE - 1 + +#define GET_LAST_STATUS(d) (d->queue[(d->write_offset - 1) & (QUEUE_SIZE - 1)]) +#define GET_PREV_STATUS(d) (d->queue[(d->write_offset - 2) & (QUEUE_SIZE - 1)]) +#define HAS_QUEUED_DATA(d) (d->read_offset < d->write_offset) +#define HAS_QUEUE_SPACE(d) (d->read_offset + QUEUE_SIZE - 1 > d->write_offset) + +#define REPLACE_STATUS(d, x) \ + (d->queue[(d->write_offset - 1) & (QUEUE_SIZE - 1)] = \ + (matrix_event_t){(x), esp_timer_get_time()}) +#define QUEUE_STATUS(d, x) \ + (d->queue[(d->write_offset++) & (QUEUE_SIZE - 1)] = \ + (matrix_event_t){(x), esp_timer_get_time()}) + +#define GET_READ_STATUS(d) (d->queue[d->read_offset & (QUEUE_SIZE - 1)]) +#define ADVANCE_IF_POSSIBLE(d) \ + if (d->read_offset < d->write_offset) { \ + d->read_offset++; \ + } + +typedef struct matrix_driver_handle { + int8_t phase_a_pin; + int8_t phase_b_pin; + int8_t press_pin; + int8_t task_queued; + uint32_t read_offset; // Accessed by task + uint32_t write_offset; // Accessed by ISR + uint32_t last_press_change_time; + int tasknumber; + matrix_event_t queue[QUEUE_SIZE]; + void *callback_arg; +} *matrix_driver_handle_t; + +static void set_gpio_mode_input(int pin, gpio_int_type_t intr) { + gpio_config_t config = {.pin_bit_mask = 1LL << pin, + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = intr}; + + gpio_config(&config); +} + +static void set_gpio_mode_output(int pin) { + gpio_config_t config = {.pin_bit_mask = 1LL << pin, + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE + }; + + gpio_config(&config); +} + +static void matrix_clear_pin(int pin) { + if (pin >= 0) { + gpio_isr_handler_remove(pin); + set_gpio_mode_input(pin, GPIO_INTR_DISABLE); + } +} + +static void set_row_interrupts(DATA *d, bool enable) +{ + for (int i = 0; i < d->row_count; i++) { + set_gpio_mode_input(d->row[i], enable ? GPIO_INTR_NEGEDGE : GPIO_INTR_DISABLE); + } +} + +static int set_columns_as_input(DATA *d) +{ + for (int i = 0; i < d->column_count; i++) { + set_gpio_mode_input(d->column[i], GPIO_INTR_DISABLE); + } +} + +// Just takes the channel number. Cleans up the resources used. +int matrix_close(DATA *d) { + if (!d) { + return 0; + } + + for (int i = 0; i < d->row_count; i++) { + matrix_clear_pin(d->row[i]); + } + + set_columns_as_input(d); + + return 0; +} + +static void matrix_interrupt(void *arg) { + // This function runs with high priority + DATA *d = (DATA *)arg; + + uint32_t now = esp_timer_get_time(); + + int i; + + set_columns_as_input(d); + set_row_interrupts(d, false); + + int character = -1; + + for (int i = 0; i < d->column_count && character < 0; i++) { + set_gpio_mode_output(d->columns[i]); + gpio_set_level(d->columns[i], 0); + + for (int j = 0; i < d->row_count && character < 0; j++) { + if (gpio_get_level(d->rows[j]) == 0) { + // We found a keypress + character = j * d->column_count + i; + } + } + + set_gpio_mode_input(d->columns[i], GPIO_INTR_DISABLE); + } + + + + // If character is >= 0 then we have found the character -- so send it. + + if (last_status != new_status) { + // Either we overwrite the status or we add a new one + if (!HAS_QUEUED_DATA(d) || STATUS_IS_PRESSED(last_status ^ new_status) || + STATUS_IS_PRESSED(last_status ^ GET_PREV_STATUS(d).pos)) { + if (HAS_QUEUE_SPACE(d)) { + QUEUE_STATUS(d, new_status); + if (!d->task_queued) { + if (task_post_medium(d->tasknumber, (task_param_t)d->callback_arg)) { + d->task_queued = 1; + } + } + } else { + REPLACE_STATUS(d, new_status); + } + } else { + REPLACE_STATUS(d, new_status); + } + } +} + +void matrix_event_handled(matrix_driver_handle_t d) { d->task_queued = 0; } + +// The pin numbers are actual platform GPIO numbers +matrix_driver_handle_t matrix_setup(int phase_a, int phase_b, int press, + task_handle_t tasknumber, void *arg) { + matrix_driver_handle_t d = (matrix_driver_handle_t)calloc(1, sizeof(*d)); + if (!d) { + return NULL; + } + + d->tasknumber = tasknumber; + d->callback_arg = arg; + + set_gpio_mode(phase_a, GPIO_INTR_ANYEDGE); + gpio_isr_handler_add(phase_a, matrix_interrupt, d); + d->phase_a_pin = phase_a; + + set_gpio_mode(phase_b, GPIO_INTR_ANYEDGE); + gpio_isr_handler_add(phase_b, matrix_interrupt, d); + d->phase_b_pin = phase_b; + + if (press >= 0) { + set_gpio_mode(press, GPIO_INTR_ANYEDGE); + gpio_isr_handler_add(press, matrix_interrupt, d); + } + d->press_pin = press; + + return d; +} + +bool matrix_has_queued_event(matrix_driver_handle_t d) { + if (!d) { + return false; + } + + return HAS_QUEUED_DATA(d); +} + +// Get the oldest event in the queue and remove it (if possible) +bool matrix_getevent(matrix_driver_handle_t d, matrix_event_t *resultp) { + matrix_event_t result = {0}; + + if (!d) { + return false; + } + + bool status = false; + + if (HAS_QUEUED_DATA(d)) { + result = GET_READ_STATUS(d); + d->read_offset++; + status = true; + } else { + result = GET_LAST_STATUS(d); + } + + *resultp = result; + + return status; +} + +int matrix_getpos(matrix_driver_handle_t d) { + if (!d) { + return -1; + } + + return GET_LAST_STATUS(d).pos; +} + +static void callback_free_one(lua_State *L, int *cb_ptr) +{ + if (*cb_ptr != LUA_NOREF) { + luaL_unref(L, LUA_REGISTRYINDEX, *cb_ptr); + *cb_ptr = LUA_NOREF; + } +} + +static void callback_free(lua_State* L, DATA *d, int mask) +{ + if (d) { + int i; + for (i = 0; i < CALLBACK_COUNT; i++) { + if (mask & (1 << i)) { + callback_free_one(L, &d->callback[i]); + } + } + } +} + +static int callback_setOne(lua_State* L, int *cb_ptr, int arg_number) +{ + if (lua_isfunction(L, arg_number)) { + lua_pushvalue(L, arg_number); // copy argument (func) to the top of stack + callback_free_one(L, cb_ptr); + *cb_ptr = luaL_ref(L, LUA_REGISTRYINDEX); + return 0; + } + + return -1; +} + +static int callback_set(lua_State* L, DATA *d, int mask, int arg_number) +{ + int result = 0; + + int i; + for (i = 0; i < CALLBACK_COUNT; i++) { + if (mask & (1 << i)) { + result |= callback_setOne(L, &d->callback[i], arg_number); + } + } + + return result; +} + +static void callback_callOne(lua_State* L, int cb, int mask, int arg, uint32_t time) +{ + if (cb != LUA_NOREF) { + lua_rawgeti(L, LUA_REGISTRYINDEX, cb); + + lua_pushinteger(L, mask); + lua_pushvalue(L, arg - 2); + lua_pushinteger(L, time); + + luaL_pcallx(L, 3, 0); + } +} + +static void callback_call(lua_State* L, DATA *d, int cbnum, int key, uint32_t time) +{ + if (d) { + lua_rawgeti(L, LUA_REGISTRYINDEX, d->character_ref); + lua_rawgeti(L, -1, key); + callback_callOne(L, d->callback[cbnum], 1 << cbnum, -1, time); + lua_pop(L, 2); + } +} + +// Lua: setup({cols}, {rows}, {characters}) +static int lmatrix_setup( lua_State* L ) +{ + int nargs = lua_gettop(L); + + // Get the sizes of the first two tables + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checktype(L, 2, LUA_TTABLE); + luaL_checktype(L, 3, LUA_TTABLE); + + size_t columns = lua_rawlen(L, 1); + size_t rows = lua_rawlen(L, 2); + + if (columns > 255 || rows > 255) { + return luaL_error(L, "Too many rows or columns"); + } + + + DATA *d = (DATA *)lua_newuserdata(L, sizeof(DATA) + rows + columns); + if (!d) return luaL_error(L, "not enough memory"); + memset(d, 0, sizeof(*d) + rows + columns); + luaL_getmetatable(L, "matrix.keyboard"); + lua_setmetatable(L, -2); + + d->columns = (uint8_t *) (d + 1); + d->rows = d->columns + columns; + d->column_count = columns; + d->row_count = rows; + + esp_timer_create_args_t timer_args = { + .callback = lmatrix_timer_done, + .dispatch_method = ESP_TIMER_TASK, + .name = "matrix_timer", + .arg = d + }; + + esp_timer_create(&timer_args, &d->timer_handle); + + int i; + for (i = 0; i < CALLBACK_COUNT; i++) { + d->callback[i] = LUA_NOREF; + } + + getpins(L, 1, columns, &d->columns); + getpins(L, 2, rows, &d->rows); + lua_pushvalue(L, 3); + d->character_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + d->handle = matrix_setup(phase_a, phase_b, press, tasknumber, d); + if (!d->handle) { + return luaL_error(L, "Unable to setup matrix switch."); + } + return 1; +} + +// Lua: close( ) +static int lmatrix_close( lua_State* L ) +{ + DATA *d = (DATA *)luaL_checkudata(L, 1, "matrix.keyboard"); + + if (d->handle) { + callback_free(L, d, MATRIX_ALL); + + if (matrix_close( d->handle )) { + return luaL_error( L, "Unable to close switch." ); + } + + d->handle = NULL; + } + return 0; +} + +// Lua: on( mask[, cb] ) +static int lmatrix_on( lua_State* L ) +{ + DATA *d = (DATA *)luaL_checkudata(L, 1, "matrix.keyboard"); + + int mask = luaL_checkinteger(L, 2); + + if (lua_gettop(L) >= 3) { + if (callback_set(L, d, mask, 3)) { + return luaL_error( L, "Unable to set callback." ); + } + } else { + callback_free(L, d, mask); + } + + return 0; +} + +// Returns TRUE if there maybe/is more stuff to do +static bool lmatrix_dequeue_single(lua_State* L, DATA *d) +{ + bool something_pending = false; + + if (d) { + matrix_event_t result; + + if (matrix_getevent(d->handle, &result)) { + int pos = result.pos; + + lmatrix_check_timer(d, result.time_us, 0); + + if (pos != d->lastpos) { + // We have something to enqueue + if ((pos ^ d->lastpos) & 0x7fffffff) { + // Some turning has happened + callback_call(L, d, matrix_TURN_INDEX, (pos << 1) >> 1, result.time_us); + } + if ((pos ^ d->lastpos) & 0x80000000) { + // pressing or releasing has happened + callback_call(L, d, (pos & 0x80000000) ? matrix_PRESS_INDEX : matrix_RELEASE_INDEX, (pos << 1) >> 1, result.time_us); + if (pos & 0x80000000) { + // Press + if (d->last_recent_event_was_release && result.time_us - d->last_event_time < d->click_delay_us) { + d->possible_dbl_click = 1; + } + d->last_recent_event_was_press = 1; + d->last_recent_event_was_release = 0; + } else { + // Release + d->last_recent_event_was_press = 0; + if (d->possible_dbl_click) { + callback_call(L, d, matrix_DBLCLICK_INDEX, (pos << 1) >> 1, result.time_us); + d->possible_dbl_click = 0; + // Do this to suppress the CLICK event + d->last_recent_event_was_release = 0; + } else { + d->last_recent_event_was_release = 1; + } + } + d->last_event_time = result.time_us; + } + + d->lastpos = pos; + } + + matrix_event_handled(d->handle); + something_pending = matrix_has_queued_event(d->handle); + } + + lmatrix_check_timer(d, esp_timer_get_time(), 1); + } + + return something_pending; +} + +static void lmatrix_timer_done(void *param) +{ + DATA *d = (DATA *) param; + + d->timer_running = 0; + + lmatrix_check_timer(d, esp_timer_get_time(), 1); +} + +static void lmatrix_check_timer(DATA *d, uint32_t time_us, bool dotimer) +{ + uint32_t delay = time_us - d->last_event_time; + if (d->timer_running) { + esp_timer_stop(d->timer_handle); + d->timer_running = 0; + } + + int timeout = -1; + + if (d->last_recent_event_was_press) { + if (delay > d->longpress_delay_us) { + callback_call(lua_getstate(), d, matrix_LONGPRESS_INDEX, (d->lastpos << 1) >> 1, d->last_event_time + d->longpress_delay_us); + d->last_recent_event_was_press = 0; + } else { + timeout = (d->longpress_delay_us - delay) / 1000; + } + } + if (d->last_recent_event_was_release) { + if (delay > d->click_delay_us) { + callback_call(lua_getstate(), d, matrix_CLICK_INDEX, (d->lastpos << 1) >> 1, d->last_event_time + d->click_delay_us); + d->last_recent_event_was_release = 0; + } else { + timeout = (d->click_delay_us - delay) / 1000; + } + } + + if (dotimer && timeout >= 0) { + d->timer_running = 1; + esp_timer_start_once(d->timer_handle, timeout + 1); + } +} + +static void lmatrix_task(task_param_t param, task_prio_t prio) +{ + (void) prio; + + bool need_to_post = false; + lua_State *L = lua_getstate(); + + DATA *d = (DATA *) param; + if (d) { + if (lmatrix_dequeue_single(L, d)) { + need_to_post = true; + } + } + + if (need_to_post) { + // If there is pending stuff, queue another task + task_post_medium(tasknumber, param); + } +} + + +// Module function map +LROT_BEGIN(matrix, NULL, 0) + LROT_FUNCENTRY( setup, lmatrix_setup ) + LROT_NUMENTRY( PRESS, MASK(PRESS) ) + LROT_NUMENTRY( RELEASE, MASK(RELEASE) ) + LROT_NUMENTRY( ALL, MATRIX_ALL ) +LROT_END(matrix, NULL, 0) + +// Module function map +LROT_BEGIN(matrix_keyboard, NULL, LROT_MASK_GC_INDEX) + LROT_FUNCENTRY(__gc, lmatrix_close) + LROT_TABENTRY(__index, matrix_keyboard) + LROT_FUNCENTRY(on, lmatrix_on) + LROT_FUNCENTRY(close, lmatrix_close) +LROT_END(matrix_keyboard, NULL, LROT_MASK_GC_INDEX) + +static int matrix_open(lua_State *L) { + luaL_rometatable(L, "matrix.keyboard", + LROT_TABLEREF(matrix_keyboard)); // create metatable + tasknumber = task_get_id(lmatrix_task); + return 0; +} + +NODEMCU_MODULE(matrix, "matrix", matrix, matrix_open); diff --git a/docs/modules/matrix.md b/docs/modules/matrix.md new file mode 100644 index 000000000..defac1af1 --- /dev/null +++ b/docs/modules/matrix.md @@ -0,0 +1,79 @@ +# matrix Module +| Since | Origin / Contributor | Maintainer | Source | +| :----- | :-------------------- | :---------- | :------ | +| 2024-02-01 | [Philip Gladstone](https://github.com/pjsg) | [Philip Gladstone](https://github.com/pjsg) | [matrix.c](../../components/modules/matrix.c)| + + +This module processes key presses on matrixed keyboards such as cheap numeric keypads with the # and * keys. These are organized as a 3x4 matrix with 7 connections +in all. + +## Sources for parts + +- Amazon: This [search](http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Dindustrial&field-keywords=rotary+encoder+push+button&rh=n%3A16310091%2Ck%3Arotary+encoder+push+button) shows a variety. +- Ebay: Somewhat cheaper in this [search](http://www.ebay.com/sch/i.html?_from=R40&_trksid=p2050601.m570.l1313.TR0.TRC0.H0.Xrotary+encoder+push+button.TRS0&_nkw=rotary+encoder+push+button&_sacat=0) +- Adafruit: [rotary encoder](https://www.adafruit.com/products/377) +- Aliexpress: This [search](http://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20160217173657&SearchText=rotary+encoder+push+button) reveals all sorts of shapes and sizes. + +## Constants +- `matrix.PRESS = 1` The eventtype for a keyboard key press +- `matrix.RELEASE = 2` The eventtype for keyboard key release. +- `matrix.ALL = 3` Covers all event types + +## matrix.setup() +Initialize the nodemcu to talk to a matrixed keyboard. + +#### Syntax +`keyboard = matrix.setup({column pins}, {row pins}, {key characters})` + +#### Parameters +- `column pins` These are the GPIO numbers of the pins connected to the columns of the keyboard +- `row pins` These are the GPIO numbers of the pins connected to the rows of the keyboard +- `key characters` These are the characters (or strings) to be returned when a key is pressed. The first character corresponds to the first row and first column. The next character is the second column and first row, etc. + +#### Returns +The keyboard object. + + +#### Example + + keyboard = matrix.setup({5,6,7}, {8,9,10,11}, { "1", "2", "3", "4", "5", "6", "7", "8", "9", "#", "0", "*"}) + +#### Notes +If an entry in the key characters table is nil, then that key press will not be reported. + +## keyboard:on() +Sets a callback on specific events. + +#### Syntax +`keyboard:on(eventtype[, callback])` + +#### Parameters +- `eventtype` This defines the type of event being registered. This can be one or more of `matrix.PRESS` and `matrix.RELEASE`. +- `callback` This is a function that will be invoked when the specified event happens. + +If the callback is None or omitted, then the registration is cancelled. + +The callback will be invoked with three arguments when the event happens. The first argument is the eventtype, +the second is the character, and the third is the time when the event happened. + +The time is the number of microseconds represented in a 32-bit integer. Note that this wraps every hour or so. + +#### Example + + keyboard:on(matrix.ALL, function (type, char, when) + print("Character=" .. char .. " event type=" .. type .. " time=" .. when) + end) + +#### Errors +If an invalid `eventtype` is supplied, then an error will be thrown. + +## keyboard:close() +Releases the resources associated with the matrix keyboard. + +#### Syntax +`keyboard:close()` + +#### Example + + keyboard:close() + From e0c44391f722ea9e43ab340533579453c5f01916 Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Sat, 3 Feb 2024 17:26:57 -0500 Subject: [PATCH 2/5] Some keys work -- not clear what is going on. --- components/modules/matrix.c | 304 +++++++++++++++--------------------- 1 file changed, 123 insertions(+), 181 deletions(-) diff --git a/components/modules/matrix.c b/components/modules/matrix.c index 12c988b79..9d5cec748 100644 --- a/components/modules/matrix.c +++ b/components/modules/matrix.c @@ -27,36 +27,43 @@ #include "driver/gpio.h" - +#define M_DEBUG printf #define MATRIX_PRESS_INDEX 0 #define MATRIX_RELEASE_INDEX 1 +#define MASK(x) (1 << MATRIX_##x##_INDEX) + #define MATRIX_ALL 0x3 #define CALLBACK_COUNT 2 #define QUEUE_SIZE 8 +typedef struct { + int32_t character; // 1 + character for press, -1 - character for release + uint32_t time_us; +} matrix_event_t; + typedef struct { uint8_t column_count; uint8_t row_count; uint8_t *columns; uint8_t *rows; + bool waiting_for_release; + bool open; int character_ref; int callback[CALLBACK_COUNT]; esp_timer_handle_t timer_handle; int8_t task_queued; uint32_t read_offset; // Accessed by task uint32_t write_offset; // Accessed by ISR - uint32_t last_press_change_time; - int tasknumber; + uint8_t last_character; matrix_event_t queue[QUEUE_SIZE]; void *callback_arg; } DATA; static task_handle_t tasknumber; static void lmatrix_timer_done(void *param); -static void lmatrix_check_timer(DATA *d, uint32_t time_us, bool dotimer); // // Queue is empty if read == write. // However, we always want to keep the previous value @@ -67,10 +74,10 @@ static void lmatrix_check_timer(DATA *d, uint32_t time_us, bool dotimer); #define HAS_QUEUED_DATA(d) (d->read_offset < d->write_offset) #define HAS_QUEUE_SPACE(d) (d->read_offset + QUEUE_SIZE - 1 > d->write_offset) -#define REPLACE_STATUS(d, x) \ +#define REPLACE_IT(d, x) \ (d->queue[(d->write_offset - 1) & (QUEUE_SIZE - 1)] = \ (matrix_event_t){(x), esp_timer_get_time()}) -#define QUEUE_STATUS(d, x) \ +#define QUEUE_IT(d, x) \ (d->queue[(d->write_offset++) & (QUEUE_SIZE - 1)] = \ (matrix_event_t){(x), esp_timer_get_time()}) @@ -80,19 +87,6 @@ static void lmatrix_check_timer(DATA *d, uint32_t time_us, bool dotimer); d->read_offset++; \ } -typedef struct matrix_driver_handle { - int8_t phase_a_pin; - int8_t phase_b_pin; - int8_t press_pin; - int8_t task_queued; - uint32_t read_offset; // Accessed by task - uint32_t write_offset; // Accessed by ISR - uint32_t last_press_change_time; - int tasknumber; - matrix_event_t queue[QUEUE_SIZE]; - void *callback_arg; -} *matrix_driver_handle_t; - static void set_gpio_mode_input(int pin, gpio_int_type_t intr) { gpio_config_t config = {.pin_bit_mask = 1LL << pin, .mode = GPIO_MODE_INPUT, @@ -105,7 +99,7 @@ static void set_gpio_mode_input(int pin, gpio_int_type_t intr) { static void set_gpio_mode_output(int pin) { gpio_config_t config = {.pin_bit_mask = 1LL << pin, - .mode = GPIO_MODE_OUTPUT, + .mode = GPIO_MODE_OUTPUT_OD, .pull_up_en = GPIO_PULLUP_DISABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE }; @@ -113,24 +107,28 @@ static void set_gpio_mode_output(int pin) { gpio_config(&config); } -static void matrix_clear_pin(int pin) { - if (pin >= 0) { - gpio_isr_handler_remove(pin); - set_gpio_mode_input(pin, GPIO_INTR_DISABLE); +static void set_columns(DATA *d, int level) { + for (int i = 0; i < d->column_count; i++) { + gpio_set_level(d->columns[i], level); } } -static void set_row_interrupts(DATA *d, bool enable) -{ +static void initialize_pins(DATA *d) { + for (int i = 0; i < d->column_count; i++) { + set_gpio_mode_output(d->columns[i]); + } + + set_columns(d, 0); + for (int i = 0; i < d->row_count; i++) { - set_gpio_mode_input(d->row[i], enable ? GPIO_INTR_NEGEDGE : GPIO_INTR_DISABLE); + set_gpio_mode_input(d->rows[i], d->waiting_for_release ? GPIO_INTR_POSEDGE + : GPIO_INTR_NEGEDGE); } } -static int set_columns_as_input(DATA *d) -{ - for (int i = 0; i < d->column_count; i++) { - set_gpio_mode_input(d->column[i], GPIO_INTR_DISABLE); +static void disable_row_interrupts(DATA *d) { + for (int i = 0; i < d->row_count; i++) { + gpio_set_intr_type(d->rows[i], GPIO_INTR_DISABLE); } } @@ -140,97 +138,90 @@ int matrix_close(DATA *d) { return 0; } + disable_row_interrupts(d); + for (int i = 0; i < d->row_count; i++) { - matrix_clear_pin(d->row[i]); + gpio_isr_handler_remove(d->rows[i]); } - set_columns_as_input(d); + for (int i = 0; i < d->column_count; i++) { + set_gpio_mode_input(d->columns[i], GPIO_INTR_DISABLE); + } return 0; } -static void matrix_interrupt(void *arg) { - // This function runs with high priority - DATA *d = (DATA *)arg; - - uint32_t now = esp_timer_get_time(); +// Character returned is 0 .. max if pressed. -1 if not. +static int matrix_get_character(DATA *d, bool trace) +{ + set_columns(d, 1); + disable_row_interrupts(d); - int i; + int character = -1; - set_columns_as_input(d); - set_row_interrupts(d, false); + // We are either waiting for a negative edge (keypress) or a positive edge + // (keyrelease) - int character = -1; + //M_DEBUG("matrix_get_character called\n"); for (int i = 0; i < d->column_count && character < 0; i++) { - set_gpio_mode_output(d->columns[i]); gpio_set_level(d->columns[i], 0); - for (int j = 0; i < d->row_count && character < 0; j++) { + for (int j = 0; j < d->row_count && character < 0; j++) { if (gpio_get_level(d->rows[j]) == 0) { + if (trace) { + M_DEBUG("Found keypress at %d %d\n", i, j); + } // We found a keypress character = j * d->column_count + i; } } - set_gpio_mode_input(d->columns[i], GPIO_INTR_DISABLE); + gpio_set_level(d->columns[i], 1); } - + //M_DEBUG("returning %d\n", character); + + return character; +} +static void matrix_queue_character(DATA *d, int character) +{ // If character is >= 0 then we have found the character -- so send it. + // M_DEBUG("Skipping queuing\n"); - if (last_status != new_status) { - // Either we overwrite the status or we add a new one - if (!HAS_QUEUED_DATA(d) || STATUS_IS_PRESSED(last_status ^ new_status) || - STATUS_IS_PRESSED(last_status ^ GET_PREV_STATUS(d).pos)) { - if (HAS_QUEUE_SPACE(d)) { - QUEUE_STATUS(d, new_status); - if (!d->task_queued) { - if (task_post_medium(d->tasknumber, (task_param_t)d->callback_arg)) { - d->task_queued = 1; - } + if (d->waiting_for_release == (character < 0)) { + if (character >= 0) { + character++; + d->last_character = character; + } else { + character = -d->last_character; + } + + if (HAS_QUEUE_SPACE(d)) { + QUEUE_IT(d, character); + if (!d->task_queued) { + if (task_post_medium(tasknumber, (task_param_t)d)) { + d->task_queued = 1; } - } else { - REPLACE_STATUS(d, new_status); } - } else { - REPLACE_STATUS(d, new_status); } } } -void matrix_event_handled(matrix_driver_handle_t d) { d->task_queued = 0; } - -// The pin numbers are actual platform GPIO numbers -matrix_driver_handle_t matrix_setup(int phase_a, int phase_b, int press, - task_handle_t tasknumber, void *arg) { - matrix_driver_handle_t d = (matrix_driver_handle_t)calloc(1, sizeof(*d)); - if (!d) { - return NULL; - } - - d->tasknumber = tasknumber; - d->callback_arg = arg; - - set_gpio_mode(phase_a, GPIO_INTR_ANYEDGE); - gpio_isr_handler_add(phase_a, matrix_interrupt, d); - d->phase_a_pin = phase_a; +static void matrix_interrupt(void *arg) { + // This function runs with high priority + DATA *d = (DATA *)arg; - set_gpio_mode(phase_b, GPIO_INTR_ANYEDGE); - gpio_isr_handler_add(phase_b, matrix_interrupt, d); - d->phase_b_pin = phase_b; + int character = matrix_get_character(d, false); - if (press >= 0) { - set_gpio_mode(press, GPIO_INTR_ANYEDGE); - gpio_isr_handler_add(press, matrix_interrupt, d); - } - d->press_pin = press; + matrix_queue_character(d, character); - return d; + d->waiting_for_release = character >= 0; + esp_timer_start_once(d->timer_handle, 5000); } -bool matrix_has_queued_event(matrix_driver_handle_t d) { +bool matrix_has_queued_event(DATA *d) { if (!d) { return false; } @@ -239,7 +230,7 @@ bool matrix_has_queued_event(matrix_driver_handle_t d) { } // Get the oldest event in the queue and remove it (if possible) -bool matrix_getevent(matrix_driver_handle_t d, matrix_event_t *resultp) { +static bool matrix_getevent(DATA *d, matrix_event_t *resultp) { matrix_event_t result = {0}; if (!d) { @@ -261,14 +252,6 @@ bool matrix_getevent(matrix_driver_handle_t d, matrix_event_t *resultp) { return status; } -int matrix_getpos(matrix_driver_handle_t d) { - if (!d) { - return -1; - } - - return GET_LAST_STATUS(d).pos; -} - static void callback_free_one(lua_State *L, int *cb_ptr) { if (*cb_ptr != LUA_NOREF) { @@ -338,11 +321,18 @@ static void callback_call(lua_State* L, DATA *d, int cbnum, int key, uint32_t ti } } +static void getpins(lua_State *L, int argno, int count, uint8_t *dest) +{ + for (int i = 1; i <= count; i++) { + lua_rawgeti(L, argno, i); + *dest++ = lua_tonumber(L, -1); + lua_pop(L, 1); + } +} + // Lua: setup({cols}, {rows}, {characters}) static int lmatrix_setup( lua_State* L ) { - int nargs = lua_gettop(L); - // Get the sizes of the first two tables luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TTABLE); @@ -355,7 +345,6 @@ static int lmatrix_setup( lua_State* L ) return luaL_error(L, "Too many rows or columns"); } - DATA *d = (DATA *)lua_newuserdata(L, sizeof(DATA) + rows + columns); if (!d) return luaL_error(L, "not enough memory"); memset(d, 0, sizeof(*d) + rows + columns); @@ -376,20 +365,21 @@ static int lmatrix_setup( lua_State* L ) esp_timer_create(&timer_args, &d->timer_handle); - int i; - for (i = 0; i < CALLBACK_COUNT; i++) { + for (int i = 0; i < CALLBACK_COUNT; i++) { d->callback[i] = LUA_NOREF; } - - getpins(L, 1, columns, &d->columns); - getpins(L, 2, rows, &d->rows); + getpins(L, 1, columns, d->columns); + getpins(L, 2, rows, d->rows); lua_pushvalue(L, 3); d->character_ref = luaL_ref(L, LUA_REGISTRYINDEX); - d->handle = matrix_setup(phase_a, phase_b, press, tasknumber, d); - if (!d->handle) { - return luaL_error(L, "Unable to setup matrix switch."); + d->open = true; + + for (int i = 0; i < d->row_count; i++) { + gpio_isr_handler_add(d->rows[i], matrix_interrupt, d); } + initialize_pins(d); + return 1; } @@ -398,14 +388,18 @@ static int lmatrix_close( lua_State* L ) { DATA *d = (DATA *)luaL_checkudata(L, 1, "matrix.keyboard"); - if (d->handle) { + if (d->open) { callback_free(L, d, MATRIX_ALL); - if (matrix_close( d->handle )) { + if (matrix_close( d )) { return luaL_error( L, "Unable to close switch." ); } - d->handle = NULL; + esp_timer_stop(d->timer_handle); + esp_timer_delete(d->timer_handle); + luaL_unref(L, LUA_REGISTRYINDEX, d->character_ref); + + d->open = false; } return 0; } @@ -436,50 +430,14 @@ static bool lmatrix_dequeue_single(lua_State* L, DATA *d) if (d) { matrix_event_t result; - if (matrix_getevent(d->handle, &result)) { - int pos = result.pos; + if (matrix_getevent(d, &result)) { + int character = result.character; - lmatrix_check_timer(d, result.time_us, 0); - - if (pos != d->lastpos) { - // We have something to enqueue - if ((pos ^ d->lastpos) & 0x7fffffff) { - // Some turning has happened - callback_call(L, d, matrix_TURN_INDEX, (pos << 1) >> 1, result.time_us); - } - if ((pos ^ d->lastpos) & 0x80000000) { - // pressing or releasing has happened - callback_call(L, d, (pos & 0x80000000) ? matrix_PRESS_INDEX : matrix_RELEASE_INDEX, (pos << 1) >> 1, result.time_us); - if (pos & 0x80000000) { - // Press - if (d->last_recent_event_was_release && result.time_us - d->last_event_time < d->click_delay_us) { - d->possible_dbl_click = 1; - } - d->last_recent_event_was_press = 1; - d->last_recent_event_was_release = 0; - } else { - // Release - d->last_recent_event_was_press = 0; - if (d->possible_dbl_click) { - callback_call(L, d, matrix_DBLCLICK_INDEX, (pos << 1) >> 1, result.time_us); - d->possible_dbl_click = 0; - // Do this to suppress the CLICK event - d->last_recent_event_was_release = 0; - } else { - d->last_recent_event_was_release = 1; - } - } - d->last_event_time = result.time_us; - } + callback_call(L, d, character > 0 ? MATRIX_PRESS_INDEX : MATRIX_RELEASE_INDEX, character < 0 ? -character : character, result.time_us); - d->lastpos = pos; - } - - matrix_event_handled(d->handle); - something_pending = matrix_has_queued_event(d->handle); + d->task_queued = 0; + something_pending = matrix_has_queued_event(d); } - - lmatrix_check_timer(d, esp_timer_get_time(), 1); } return something_pending; @@ -489,48 +447,30 @@ static void lmatrix_timer_done(void *param) { DATA *d = (DATA *) param; - d->timer_running = 0; + // We need to see if the key is still pressed, and if so, enable rising edge interrupts - lmatrix_check_timer(d, esp_timer_get_time(), 1); -} + int character = matrix_get_character(d, true); -static void lmatrix_check_timer(DATA *d, uint32_t time_us, bool dotimer) -{ - uint32_t delay = time_us - d->last_event_time; - if (d->timer_running) { - esp_timer_stop(d->timer_handle); - d->timer_running = 0; - } + M_DEBUG("Timer fired with character %d (waiting for release %d)\n", character, d->waiting_for_release); - int timeout = -1; + matrix_queue_character(d, character); + d->waiting_for_release = (character >= 0); - if (d->last_recent_event_was_press) { - if (delay > d->longpress_delay_us) { - callback_call(lua_getstate(), d, matrix_LONGPRESS_INDEX, (d->lastpos << 1) >> 1, d->last_event_time + d->longpress_delay_us); - d->last_recent_event_was_press = 0; - } else { - timeout = (d->longpress_delay_us - delay) / 1000; - } - } - if (d->last_recent_event_was_release) { - if (delay > d->click_delay_us) { - callback_call(lua_getstate(), d, matrix_CLICK_INDEX, (d->lastpos << 1) >> 1, d->last_event_time + d->click_delay_us); - d->last_recent_event_was_release = 0; - } else { - timeout = (d->click_delay_us - delay) / 1000; - } - } + M_DEBUG("Timer: Waiting for release is %d\n", d->waiting_for_release); - if (dotimer && timeout >= 0) { - d->timer_running = 1; - esp_timer_start_once(d->timer_handle, timeout + 1); + for (int i = 0; i < d->row_count; i++) { + gpio_set_intr_type(d->rows[i], d->waiting_for_release ? GPIO_INTR_POSEDGE + : GPIO_INTR_NEGEDGE); } + set_columns(d, 0); } static void lmatrix_task(task_param_t param, task_prio_t prio) { (void) prio; + M_DEBUG("Task invoked\n"); + bool need_to_post = false; lua_State *L = lua_getstate(); @@ -545,6 +485,8 @@ static void lmatrix_task(task_param_t param, task_prio_t prio) // If there is pending stuff, queue another task task_post_medium(tasknumber, param); } + + M_DEBUG("Task done\n"); } @@ -571,4 +513,4 @@ static int matrix_open(lua_State *L) { return 0; } -NODEMCU_MODULE(matrix, "matrix", matrix, matrix_open); +NODEMCU_MODULE(MATRIX, "matrix", matrix, matrix_open); From 92f507562eda0e395af79d550fd3bc1e31a4765c Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Sun, 4 Feb 2024 11:10:58 -0500 Subject: [PATCH 3/5] Seems to work --- components/modules/matrix.c | 45 ++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/components/modules/matrix.c b/components/modules/matrix.c index 9d5cec748..6850d12e5 100644 --- a/components/modules/matrix.c +++ b/components/modules/matrix.c @@ -44,12 +44,18 @@ typedef struct { uint32_t time_us; } matrix_event_t; +typedef enum { + WAITING_FOR_PRESS, + WAITING_FOR_RELEASE, + WAITING_FOR_DEBOUNCE +} state_t; + typedef struct { uint8_t column_count; uint8_t row_count; uint8_t *columns; uint8_t *rows; - bool waiting_for_release; + state_t state; bool open; int character_ref; int callback[CALLBACK_COUNT]; @@ -121,8 +127,7 @@ static void initialize_pins(DATA *d) { set_columns(d, 0); for (int i = 0; i < d->row_count; i++) { - set_gpio_mode_input(d->rows[i], d->waiting_for_release ? GPIO_INTR_POSEDGE - : GPIO_INTR_NEGEDGE); + set_gpio_mode_input(d->rows[i], GPIO_INTR_NEGEDGE); } } @@ -170,7 +175,7 @@ static int matrix_get_character(DATA *d, bool trace) for (int j = 0; j < d->row_count && character < 0; j++) { if (gpio_get_level(d->rows[j]) == 0) { if (trace) { - M_DEBUG("Found keypress at %d %d\n", i, j); + //M_DEBUG("Found keypress at %d %d\n", i, j); } // We found a keypress character = j * d->column_count + i; @@ -190,7 +195,7 @@ static void matrix_queue_character(DATA *d, int character) // If character is >= 0 then we have found the character -- so send it. // M_DEBUG("Skipping queuing\n"); - if (d->waiting_for_release == (character < 0)) { + if ((d->state == WAITING_FOR_PRESS && character >= 0) || (d->state == WAITING_FOR_RELEASE && character < 0)) { if (character >= 0) { character++; d->last_character = character; @@ -217,7 +222,7 @@ static void matrix_interrupt(void *arg) { matrix_queue_character(d, character); - d->waiting_for_release = character >= 0; + d->state = character >= 0 ? WAITING_FOR_RELEASE : WAITING_FOR_PRESS; esp_timer_start_once(d->timer_handle, 5000); } @@ -451,25 +456,35 @@ static void lmatrix_timer_done(void *param) int character = matrix_get_character(d, true); - M_DEBUG("Timer fired with character %d (waiting for release %d)\n", character, d->waiting_for_release); + //M_DEBUG("Timer fired with character %d (waiting for release %d)\n", character, d->state); matrix_queue_character(d, character); - d->waiting_for_release = (character >= 0); - M_DEBUG("Timer: Waiting for release is %d\n", d->waiting_for_release); + if (d->state == WAITING_FOR_RELEASE && character < 0) { + d->state = WAITING_FOR_DEBOUNCE; + } else if (character >= 0) { + d->state = WAITING_FOR_RELEASE; + } else { + d->state = WAITING_FOR_PRESS; + } - for (int i = 0; i < d->row_count; i++) { - gpio_set_intr_type(d->rows[i], d->waiting_for_release ? GPIO_INTR_POSEDGE - : GPIO_INTR_NEGEDGE); + //M_DEBUG("Timer: Waiting for release is %d\n", d->state); + + if (d->state == WAITING_FOR_PRESS) { + for (int i = 0; i < d->row_count; i++) { + gpio_set_intr_type(d->rows[i], GPIO_INTR_NEGEDGE); + } + set_columns(d, 0); + } else { + esp_timer_start_once(d->timer_handle, 40000); } - set_columns(d, 0); } static void lmatrix_task(task_param_t param, task_prio_t prio) { (void) prio; - M_DEBUG("Task invoked\n"); + //M_DEBUG("Task invoked\n"); bool need_to_post = false; lua_State *L = lua_getstate(); @@ -486,7 +501,7 @@ static void lmatrix_task(task_param_t param, task_prio_t prio) task_post_medium(tasknumber, param); } - M_DEBUG("Task done\n"); + //M_DEBUG("Task done\n"); } From c3300a8fbb5f1994b743339ddeaa65b49d5d9156 Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Sun, 4 Feb 2024 12:43:43 -0500 Subject: [PATCH 4/5] Working version --- components/modules/matrix.c | 69 ++++++++++++++----------------------- docs/modules/matrix.md | 10 +++--- 2 files changed, 30 insertions(+), 49 deletions(-) diff --git a/components/modules/matrix.c b/components/modules/matrix.c index 6850d12e5..d0416bde7 100644 --- a/components/modules/matrix.c +++ b/components/modules/matrix.c @@ -7,7 +7,7 @@ * Then we disable all the columns and then drive each column low in turn. Hopefully * one of the rows will go low. This is a keypress. We only report the first keypress found. * we start a timer to handle debounce. - * On timer expiry, see if any key is pressed, if so, just wait agin (maybe should use interrupts) + * On timer expiry, see if any key is pressed, if so, just wait again * If no key is pressed, run timer again. On timer expiry, re-enable interrupts. * * Philip Gladstone, N1DQ @@ -21,14 +21,8 @@ #include #include #include - - -#include - #include "driver/gpio.h" -#define M_DEBUG printf - #define MATRIX_PRESS_INDEX 0 #define MATRIX_RELEASE_INDEX 1 @@ -65,7 +59,6 @@ typedef struct { uint32_t write_offset; // Accessed by ISR uint8_t last_character; matrix_event_t queue[QUEUE_SIZE]; - void *callback_arg; } DATA; static task_handle_t tasknumber; @@ -93,24 +86,24 @@ static void lmatrix_timer_done(void *param); d->read_offset++; \ } -static void set_gpio_mode_input(int pin, gpio_int_type_t intr) { +static esp_err_t set_gpio_mode_input(int pin, gpio_int_type_t intr) { gpio_config_t config = {.pin_bit_mask = 1LL << pin, .mode = GPIO_MODE_INPUT, .pull_up_en = GPIO_PULLUP_ENABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE, .intr_type = intr}; - gpio_config(&config); + return gpio_config(&config); } -static void set_gpio_mode_output(int pin) { +static esp_err_t set_gpio_mode_output(int pin) { gpio_config_t config = {.pin_bit_mask = 1LL << pin, .mode = GPIO_MODE_OUTPUT_OD, .pull_up_en = GPIO_PULLUP_DISABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE }; - gpio_config(&config); + return gpio_config(&config); } static void set_columns(DATA *d, int level) { @@ -119,15 +112,19 @@ static void set_columns(DATA *d, int level) { } } -static void initialize_pins(DATA *d) { +static void initialize_pins(lua_State *L, DATA *d) { for (int i = 0; i < d->column_count; i++) { - set_gpio_mode_output(d->columns[i]); + if (set_gpio_mode_output(d->columns[i]) != ESP_OK) { + luaL_error(L, "Unable to configure pins"); + } } set_columns(d, 0); for (int i = 0; i < d->row_count; i++) { - set_gpio_mode_input(d->rows[i], GPIO_INTR_NEGEDGE); + if (set_gpio_mode_input(d->rows[i], GPIO_INTR_NEGEDGE) != ESP_OK) { + luaL_error(L, "Unable to configure pins"); + } } } @@ -138,7 +135,7 @@ static void disable_row_interrupts(DATA *d) { } // Just takes the channel number. Cleans up the resources used. -int matrix_close(DATA *d) { +static int matrix_close(DATA *d) { if (!d) { return 0; } @@ -157,7 +154,7 @@ int matrix_close(DATA *d) { } // Character returned is 0 .. max if pressed. -1 if not. -static int matrix_get_character(DATA *d, bool trace) +static int matrix_get_character(DATA *d) { set_columns(d, 1); disable_row_interrupts(d); @@ -167,16 +164,11 @@ static int matrix_get_character(DATA *d, bool trace) // We are either waiting for a negative edge (keypress) or a positive edge // (keyrelease) - //M_DEBUG("matrix_get_character called\n"); - for (int i = 0; i < d->column_count && character < 0; i++) { gpio_set_level(d->columns[i], 0); for (int j = 0; j < d->row_count && character < 0; j++) { if (gpio_get_level(d->rows[j]) == 0) { - if (trace) { - //M_DEBUG("Found keypress at %d %d\n", i, j); - } // We found a keypress character = j * d->column_count + i; } @@ -185,15 +177,12 @@ static int matrix_get_character(DATA *d, bool trace) gpio_set_level(d->columns[i], 1); } - //M_DEBUG("returning %d\n", character); - return character; } static void matrix_queue_character(DATA *d, int character) { // If character is >= 0 then we have found the character -- so send it. - // M_DEBUG("Skipping queuing\n"); if ((d->state == WAITING_FOR_PRESS && character >= 0) || (d->state == WAITING_FOR_RELEASE && character < 0)) { if (character >= 0) { @@ -218,7 +207,7 @@ static void matrix_interrupt(void *arg) { // This function runs with high priority DATA *d = (DATA *)arg; - int character = matrix_get_character(d, false); + int character = matrix_get_character(d); matrix_queue_character(d, character); @@ -226,7 +215,7 @@ static void matrix_interrupt(void *arg) { esp_timer_start_once(d->timer_handle, 5000); } -bool matrix_has_queued_event(DATA *d) { +static bool matrix_has_queued_event(DATA *d) { if (!d) { return false; } @@ -321,7 +310,9 @@ static void callback_call(lua_State* L, DATA *d, int cbnum, int key, uint32_t ti if (d) { lua_rawgeti(L, LUA_REGISTRYINDEX, d->character_ref); lua_rawgeti(L, -1, key); - callback_callOne(L, d->callback[cbnum], 1 << cbnum, -1, time); + if (lua_type(L, -1) != LUA_TNIL) { + callback_callOne(L, d->callback[cbnum], 1 << cbnum, -1, time); + } lua_pop(L, 2); } } @@ -338,16 +329,16 @@ static void getpins(lua_State *L, int argno, int count, uint8_t *dest) // Lua: setup({cols}, {rows}, {characters}) static int lmatrix_setup( lua_State* L ) { - // Get the sizes of the first two tables luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TTABLE); luaL_checktype(L, 3, LUA_TTABLE); + // Get the sizes of the first two tables size_t columns = lua_rawlen(L, 1); size_t rows = lua_rawlen(L, 2); - if (columns > 255 || rows > 255) { - return luaL_error(L, "Too many rows or columns"); + if (columns > 255 || rows > 255 || !rows || !columns) { + return luaL_error(L, "Number of rows or columns out of range"); } DATA *d = (DATA *)lua_newuserdata(L, sizeof(DATA) + rows + columns); @@ -368,6 +359,8 @@ static int lmatrix_setup( lua_State* L ) .arg = d }; + d->open = true; + esp_timer_create(&timer_args, &d->timer_handle); for (int i = 0; i < CALLBACK_COUNT; i++) { @@ -378,12 +371,10 @@ static int lmatrix_setup( lua_State* L ) lua_pushvalue(L, 3); d->character_ref = luaL_ref(L, LUA_REGISTRYINDEX); - d->open = true; - for (int i = 0; i < d->row_count; i++) { gpio_isr_handler_add(d->rows[i], matrix_interrupt, d); } - initialize_pins(d); + initialize_pins(L, d); return 1; } @@ -454,9 +445,7 @@ static void lmatrix_timer_done(void *param) // We need to see if the key is still pressed, and if so, enable rising edge interrupts - int character = matrix_get_character(d, true); - - //M_DEBUG("Timer fired with character %d (waiting for release %d)\n", character, d->state); + int character = matrix_get_character(d); matrix_queue_character(d, character); @@ -468,8 +457,6 @@ static void lmatrix_timer_done(void *param) d->state = WAITING_FOR_PRESS; } - //M_DEBUG("Timer: Waiting for release is %d\n", d->state); - if (d->state == WAITING_FOR_PRESS) { for (int i = 0; i < d->row_count; i++) { gpio_set_intr_type(d->rows[i], GPIO_INTR_NEGEDGE); @@ -484,8 +471,6 @@ static void lmatrix_task(task_param_t param, task_prio_t prio) { (void) prio; - //M_DEBUG("Task invoked\n"); - bool need_to_post = false; lua_State *L = lua_getstate(); @@ -500,8 +485,6 @@ static void lmatrix_task(task_param_t param, task_prio_t prio) // If there is pending stuff, queue another task task_post_medium(tasknumber, param); } - - //M_DEBUG("Task done\n"); } diff --git a/docs/modules/matrix.md b/docs/modules/matrix.md index defac1af1..edf803a1a 100644 --- a/docs/modules/matrix.md +++ b/docs/modules/matrix.md @@ -9,10 +9,8 @@ in all. ## Sources for parts -- Amazon: This [search](http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Dindustrial&field-keywords=rotary+encoder+push+button&rh=n%3A16310091%2Ck%3Arotary+encoder+push+button) shows a variety. -- Ebay: Somewhat cheaper in this [search](http://www.ebay.com/sch/i.html?_from=R40&_trksid=p2050601.m570.l1313.TR0.TRC0.H0.Xrotary+encoder+push+button.TRS0&_nkw=rotary+encoder+push+button&_sacat=0) -- Adafruit: [rotary encoder](https://www.adafruit.com/products/377) -- Aliexpress: This [search](http://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20160217173657&SearchText=rotary+encoder+push+button) reveals all sorts of shapes and sizes. +- Adafruit: [Matrix keypad](https://www.adafruit.com/search?q=matrix+keypad) +- Aliexpress: This [search](https://www.aliexpress.us/w/wholesale-matrix-keypad.html) reveals all sorts of shapes and sizes. ## Constants - `matrix.PRESS = 1` The eventtype for a keyboard key press @@ -36,7 +34,7 @@ The keyboard object. #### Example - keyboard = matrix.setup({5,6,7}, {8,9,10,11}, { "1", "2", "3", "4", "5", "6", "7", "8", "9", "#", "0", "*"}) + keyboard = matrix.setup({17, 4, 18}, {16, 21, 19, 5}, { "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#"}) #### Notes If an entry in the key characters table is nil, then that key press will not be reported. @@ -48,7 +46,7 @@ Sets a callback on specific events. `keyboard:on(eventtype[, callback])` #### Parameters -- `eventtype` This defines the type of event being registered. This can be one or more of `matrix.PRESS` and `matrix.RELEASE`. +- `eventtype` This defines the type of event being registered. This can be one or more of `matrix.PRESS` and `matrix.RELEASE`. `matrix.ALL` covers all the event types. - `callback` This is a function that will be invoked when the specified event happens. If the callback is None or omitted, then the registration is cancelled. From eebc8a2dd348bfe6cb4e9f6a80bea3af35da8eea Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Thu, 25 Apr 2024 20:26:05 -0400 Subject: [PATCH 5/5] Fix problem with GC'ing the object while in use --- components/modules/matrix.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/components/modules/matrix.c b/components/modules/matrix.c index d0416bde7..2a90b7e94 100644 --- a/components/modules/matrix.c +++ b/components/modules/matrix.c @@ -52,6 +52,7 @@ typedef struct { state_t state; bool open; int character_ref; + int self_ref; // to prevent GC at bad moments. int callback[CALLBACK_COUNT]; esp_timer_handle_t timer_handle; int8_t task_queued; @@ -347,6 +348,11 @@ static int lmatrix_setup( lua_State* L ) luaL_getmetatable(L, "matrix.keyboard"); lua_setmetatable(L, -2); + // create a self_ref to prevent GC + + lua_pushvalue(L, -1); + d->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); + d->columns = (uint8_t *) (d + 1); d->rows = d->columns + columns; d->column_count = columns; @@ -395,6 +401,10 @@ static int lmatrix_close( lua_State* L ) esp_timer_delete(d->timer_handle); luaL_unref(L, LUA_REGISTRYINDEX, d->character_ref); + if (!HAS_QUEUED_DATA(d)) { + luaL_unref(L, LUA_REGISTRYINDEX, d->self_ref); + } + d->open = false; } return 0; @@ -484,6 +494,10 @@ static void lmatrix_task(task_param_t param, task_prio_t prio) if (need_to_post) { // If there is pending stuff, queue another task task_post_medium(tasknumber, param); + } else if (d) { + if (!d->open) { + luaL_unref(L, LUA_REGISTRYINDEX, d->self_ref); + } } }