#include #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(); } }