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,5 +1,5 @@
idf_component_register(
SRCS "src/chip_temperature.cpp" "src/rgb_led.cpp"
SRCS "src/chip_temperature.cpp" "src/rgb_led.cpp" "src/storage.cpp"
INCLUDE_DIRS "include"
PRIV_REQUIRES esp_driver_tsens espressif__led_strip
PRIV_REQUIRES esp_driver_tsens espressif__led_strip joltwallet__littlefs
)

View File

@@ -4,5 +4,4 @@ Hardware abstraction: wraps ESP32-C6 peripherals (GPIO, ADC, I2C) behind a board
- `chip_temperature.h`/`.cpp` -- wraps ESP-IDF's built-in `temperature_sensor` driver (die temperature; there's no external ambient temperature sensor on the ESP32-C6-DevKitC-1).
- `rgb_led.h`/`.cpp` -- wraps the DevKitC-1's onboard WS2812 addressable RGB LED (GPIO8) via the `espressif/led_strip` managed component (`idf_component.yml`).
TODO: mounting the `storage` LittleFS partition (see `partitions.csv`) belongs here once a LittleFS component (e.g. `joltwallet/esp_littlefs` from the IDF Component Registry) is picked and pinned via `idf_component.yml`.
- `storage.h`/`.cpp` -- mounts the `storage` LittleFS partition (see `partitions.csv`) at `/storage` via `joltwallet/littlefs` (verified against the component's actual header, not guessed -- the real mount function is `esp_vfs_littlefs_register()`, not `esp_vfs_littlefs_mount()` despite what some docs summaries say), then exposes plain whole-file `storage_read_file()`/`storage_write_file()` helpers over the standard `fopen`/`fread`/`fwrite` VFS calls. Used by `lua_runtime` to persist the running script (`components/lua_runtime/README.md`).

View File

@@ -1,3 +1,4 @@
dependencies:
espressif/led_strip:
version: "*"
joltwallet/littlefs: "1.22.2"

View File

@@ -0,0 +1,21 @@
#pragma once
#include <cstddef>
namespace kvida::drivers {
// Mounts the "storage" partition (see partitions.csv) as a LittleFS
// filesystem at /storage. Safe to call multiple times (no-op if already
// mounted). Formats the partition automatically if it's blank or corrupt.
void storage_init();
// Reads a whole file into buf (cap includes room for the implicit '\0'
// this appends). Returns false if the filesystem isn't mounted, the file
// doesn't exist, or it doesn't fit in cap.
bool storage_read_file(const char *path, char *buf, size_t cap, size_t *out_len);
// Writes/overwrites a whole file. Returns false if the filesystem isn't
// mounted or the write fails.
bool storage_write_file(const char *path, const char *data, size_t len);
} // namespace kvida::drivers

View File

@@ -0,0 +1,79 @@
#include "storage.h"
#include <cstdio>
#include "esp_log.h"
#include "esp_littlefs.h"
namespace kvida::drivers {
namespace {
constexpr const char *TAG = "storage";
bool g_mounted = false;
} // namespace
void storage_init()
{
if (g_mounted) {
return;
}
esp_vfs_littlefs_conf_t conf = {};
conf.base_path = "/storage";
conf.partition_label = "storage";
conf.format_if_mount_failed = true;
conf.grow_on_mount = false;
esp_err_t err = esp_vfs_littlefs_register(&conf);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_vfs_littlefs_register failed: %s", esp_err_to_name(err));
return;
}
g_mounted = true;
}
bool storage_read_file(const char *path, char *buf, size_t cap, size_t *out_len)
{
if (!g_mounted || cap == 0) {
return false;
}
FILE *f = fopen(path, "r");
if (!f) {
return false;
}
size_t n = fread(buf, 1, cap - 1, f);
bool truncated = !feof(f);
fclose(f);
if (truncated) {
ESP_LOGE(TAG, "%s is larger than the %u-byte read buffer", path, static_cast<unsigned>(cap - 1));
return false;
}
buf[n] = '\0';
if (out_len) {
*out_len = n;
}
return true;
}
bool storage_write_file(const char *path, const char *data, size_t len)
{
if (!g_mounted) {
return false;
}
FILE *f = fopen(path, "w");
if (!f) {
ESP_LOGE(TAG, "fopen(%s, \"w\") failed", path);
return false;
}
size_t written = fwrite(data, 1, len, f);
fclose(f);
return written == len;
}
} // namespace kvida::drivers