Files
kvida-os/main/main.cpp
Ronny Eia 967fecbf69 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>
2026-07-12 10:27:00 +02:00

68 lines
2.2 KiB
C++

#include <cstddef>
#include "commissioning.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "lua_runtime.h"
#include "mqtt.h"
#include "rgb_led.h"
namespace {
constexpr const char *TAG = "kvida_os";
esp_timer_handle_t g_lua_tick_timer = nullptr;
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");
// 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 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(); },
.arg = nullptr,
.dispatch_method = ESP_TIMER_TASK,
.name = "lua_tick",
.skip_unhandled_events = true,
};
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &g_lua_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(g_lua_tick_timer, 30LL * 1000000));
// First boot / no stored Wi-Fi credentials -> fall straight into
// commissioning (nothing to protect yet, so no physical-interaction
// gate needed here). Re-entering commissioning on an already-configured
// device should require a physical trigger (e.g. the user button) per
// AGENTS.md's security principle -- not wired up yet since `drivers`
// doesn't have a real button implementation.
if (!kvida::try_stored_credentials()) {
kvida::start_commissioning();
}
}