Add persistent script storage + hot-reload, verified live

lua_runtime now loads the running script from a new LittleFS-backed
drivers::storage (joltwallet/littlefs, the project's fifth external
managed component -- mounts the "storage" partition already reserved
in partitions.csv), falling back to the compiled-in default script
only on first boot. transport exposes GET/POST /api/script -- raw Lua
text, not JSON, since json_helpers.h can't round-trip scripts safely
-- relayed to lua_runtime via the same callback-registration pattern
already used for LedColorHandler, keeping transport free of a
dependency on lua_runtime. lua_runtime_reload() persists and hot-swaps
to a fresh Lua state immediately, no reboot.

Verified on real ESP32-C6 hardware: script loads via kvida-sdk, an
edited script hot-reloads within one tick, and the edit survives a
power cycle (proving LittleFS persistence, not just an in-RAM change).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Ronny Eia
2026-07-12 10:27:00 +02:00
parent eae6d3af5a
commit 967fecbf69
12 changed files with 329 additions and 29 deletions

View File

@@ -1,3 +1,5 @@
#include <cstddef>
#include "commissioning.h"
#include "esp_log.h"
#include "esp_timer.h"
@@ -15,19 +17,33 @@ void on_led_color(uint8_t red, uint8_t green, uint8_t blue)
kvida::drivers::rgb_led_set_color(red, green, blue);
}
void on_script_change(const char *script, size_t len)
{
kvida::lua_runtime_reload(script, len);
}
size_t on_script_provider(char *buf, size_t cap)
{
return kvida::lua_runtime_current_script(buf, cap);
}
} // namespace
extern "C" void app_main(void)
{
ESP_LOGI(TAG, "kvida-os starting");
// The default Lua script (components/lua_runtime/src/default_script.h)
// Runs whatever script is in storage (uploaded via kvida-sdk's
// editor), falling back to the compiled-in default
// (components/lua_runtime/src/default_script.h) on first boot --
// reads chip temperature, drives the LED, and publishes to Home
// Assistant -- see that component's README for what's real here vs.
// still a "quick proof" using only what's on the ESP32-C6-DevKitC-1.
// Assistant. See lua_runtime's README for what's real here vs. still
// a "quick proof" using only what's on the ESP32-C6-DevKitC-1.
kvida::drivers::rgb_led_init();
kvida::lua_runtime_init();
kvida::set_led_color_handler(&on_led_color);
kvida::set_script_change_handler(&on_script_change);
kvida::set_script_provider(&on_script_provider);
const esp_timer_create_args_t timer_args = {
.callback = [](void *) { kvida::lua_runtime_tick(); },