From 72da7654101c07b6ae65fb4b151a967e10c71caa Mon Sep 17 00:00:00 2001 From: sukuwc Date: Wed, 18 Jun 2025 16:58:09 +0200 Subject: [PATCH] SUKU timer implementation imported from previous branch --- grid_common/grid_lua.c | 1 + grid_common/grid_ui.c | 25 ++++++++++++++ grid_common/grid_ui.h | 2 ++ grid_common/lua_src/lua_source_collection.h | 3 ++ grid_common/lua_src/timer.lua | 34 +++++++++++++++++++ .../grid_esp32_port/grid_esp32_port.c | 3 +- grid_esp/sdkconfig | 8 +++++ grid_make/main.c | 3 +- 8 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 grid_common/lua_src/timer.lua diff --git a/grid_common/grid_lua.c b/grid_common/grid_lua.c index 82d2a78ff..e8eb411c9 100644 --- a/grid_common/grid_lua.c +++ b/grid_common/grid_lua.c @@ -253,6 +253,7 @@ void grid_lua_start_vm(struct grid_lua_model* lua) { grid_lua_dostring(lua, GRID_LUA_FNC_G_ELEMENTNAME_source); grid_lua_dostring(lua, GRID_LUA_FNC_G_MAPSAT_source); grid_lua_dostring(lua, GRID_LUA_FNC_G_SIGN_source); + grid_lua_dostring(lua, GRID_LUA_FNC_G_TIMER_source); grid_lua_dostring(lua, GRID_LUA_FNC_G_SEGCALC_source); // grid_lua_dostring(lua, GRID_LUA_FNC_G_TOML_source); grid_lua_dostring(lua, "midi_fifo = {}"); diff --git a/grid_common/grid_ui.c b/grid_common/grid_ui.c index 1125cd854..43d7cb028 100644 --- a/grid_common/grid_ui.c +++ b/grid_common/grid_ui.c @@ -1525,3 +1525,28 @@ void grid_port_process_ui_UNSAFE(struct grid_ui_model* ui) { grid_ui_busy_semaphore_release(ui); } + +void grid_port_process_timer_UNSAFE(struct grid_ui_model* ui) { + grid_ui_busy_semaphore_lock(ui); + + grid_lua_clear_stdo(&grid_lua_state); + + grid_lua_dostring(&grid_lua_state, "check_timers()"); + + char* stdo = grid_lua_get_output_string(&grid_lua_state); + + if (strlen(stdo) > 0) { + + struct grid_msg_packet response; + grid_msg_packet_init(&grid_msg_state, &response, GRID_PARAMETER_GLOBAL_POSITION, GRID_PARAMETER_GLOBAL_POSITION); + + response.body_length += sprintf(response.body, "%s", stdo); + + grid_msg_packet_close(&grid_msg_state, &response); + grid_transport_send_msg_packet_to_all(&grid_transport_state, &response); + + grid_lua_clear_stdo(&grid_lua_state); + } + + grid_ui_busy_semaphore_release(ui); +} diff --git a/grid_common/grid_ui.h b/grid_common/grid_ui.h index 01edf7d78..783207060 100644 --- a/grid_common/grid_ui.h +++ b/grid_common/grid_ui.h @@ -221,4 +221,6 @@ void grid_port_process_ui_local_UNSAFE(struct grid_ui_model* ui); void grid_port_process_ui_UNSAFE(struct grid_ui_model* ui); +void grid_port_process_timer_UNSAFE(struct grid_ui_model* ui); + #endif /* GRID_UI_H */ diff --git a/grid_common/lua_src/lua_source_collection.h b/grid_common/lua_src/lua_source_collection.h index 5613078b9..e46469c84 100644 --- a/grid_common/lua_src/lua_source_collection.h +++ b/grid_common/lua_src/lua_source_collection.h @@ -1,6 +1,9 @@ #ifndef LUA_SOURCE_COLLECTION_H_INCLUDED #define LUA_SOURCE_COLLECTION_H_INCLUDED +#include "timer.h" +#define GRID_LUA_FNC_G_TIMER_source grid_lua_src_timer_lua + #include "lookup.h" #define GRID_LUA_FNC_G_LOOKUP_source grid_lua_src_lookup_lua #define GRID_LUA_FNC_G_LOOKUP_short "glut" diff --git a/grid_common/lua_src/timer.lua b/grid_common/lua_src/timer.lua new file mode 100644 index 000000000..cbab15ea3 --- /dev/null +++ b/grid_common/lua_src/timer.lua @@ -0,0 +1,34 @@ +-- Updated infinite loop for the `setTimeout` example +local timers = {} +local timersNextId = 0 + +function set_timeout(callback, delay) + local timerId = timersNextId + timersNextId = timersNextId + 1 + local targetTime = os.clock() + delay + timers[timerId] = {id = timerId, time = targetTime, interval = delay, callback = callback} + return timerId +end + +function clear_timeout(timerId) + if timers[timerId] then + timers[timerId] = nil + end +end + +function check_timers() + local now = os.clock() + for timerId, timer in pairs(timers) do + if now >= timer.time then + --print("Servicing timer"..timerId.." now") + local retrigger = timer.callback(timer) + if retrigger == true then + --print("Retriggering timer"..timerId.." now") + timers[timerId].time = timers[timerId].time + timers[timerId].interval + else + --print("Deleting timer"..timerId.." now") + timers[timerId] = nil + end + end + end +end diff --git a/grid_esp/components/grid_esp32_port/grid_esp32_port.c b/grid_esp/components/grid_esp32_port/grid_esp32_port.c index 57ecb644c..3f2ece803 100644 --- a/grid_esp/components/grid_esp32_port/grid_esp32_port.c +++ b/grid_esp/components/grid_esp32_port/grid_esp32_port.c @@ -250,8 +250,9 @@ void grid_utask_process_ui(struct grid_utask_timer* timer) { } if (grid_ui_event_count_istriggered(&grid_ui_state) > 0) { - grid_port_process_ui_UNSAFE(&grid_ui_state); + } else { + grid_port_process_timer_UNSAFE(&grid_ui_state); } } diff --git a/grid_esp/sdkconfig b/grid_esp/sdkconfig index 2c5fdbfec..7502304ea 100644 --- a/grid_esp/sdkconfig +++ b/grid_esp/sdkconfig @@ -2058,6 +2058,14 @@ CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN=y # TinyUSB Stack # CONFIG_TINYUSB_DEBUG_LEVEL=1 +CONFIG_TINYUSB_RHPORT_FS=y + +# +# TinyUSB DCD +# +# CONFIG_TINYUSB_MODE_SLAVE is not set +CONFIG_TINYUSB_MODE_DMA=y +# end of TinyUSB DCD # # TinyUSB task configuration diff --git a/grid_make/main.c b/grid_make/main.c index 014d2f873..58acb54b2 100644 --- a/grid_make/main.c +++ b/grid_make/main.c @@ -183,8 +183,9 @@ void grid_utask_process_ui(struct grid_utask_timer* timer) { } if (grid_ui_event_count_istriggered(&grid_ui_state) > 0) { - grid_port_process_ui_UNSAFE(&grid_ui_state); + } else { + grid_port_process_timer_UNSAFE(&grid_ui_state); } }