Sandboxed Lua state (espressif/lua v5.5.0, base/table/string/math only, no io/os/package/debug) exposes a kvida API table (chip_temperature, set_led, publish) to a compiled-in default script that reads chip temperature, maps it to an LED color, and publishes the reading to Home Assistant. Generalizes transport's publish_chip_temperature() into publish_value(name, value), the first real use of AGENTS.md's semantic publish API. Requires -DLUA_32BITS globally since this toolchain's long long support isn't visible to Lua's default build. Verified on real ESP32-C6 hardware: no crashes/errors across multiple serial monitor windows, LED changes color, chip_temperature sensor appears in Home Assistant via MQTT Discovery. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
52 lines
1.7 KiB
C++
52 lines
1.7 KiB
C++
#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);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
extern "C" void app_main(void)
|
|
{
|
|
ESP_LOGI(TAG, "kvida-os starting");
|
|
|
|
// The default Lua script (components/lua_runtime/src/default_script.h)
|
|
// 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.
|
|
kvida::drivers::rgb_led_init();
|
|
kvida::lua_runtime_init();
|
|
kvida::set_led_color_handler(&on_led_color);
|
|
|
|
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();
|
|
}
|
|
}
|