Add Lua runtime: chip temp -> LED color -> HA, verified live
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>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS "main.cpp"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES drivers sensors profiles transport lua_runtime esp_timer
|
||||
)
|
||||
idf_component_register(
|
||||
SRCS "main.cpp"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES drivers transport lua_runtime esp_timer
|
||||
)
|
||||
|
||||
@@ -1,27 +1,14 @@
|
||||
#include "chip_temperature_sensor.h"
|
||||
#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";
|
||||
|
||||
kvida::ChipTemperatureSensor g_chip_temp_sensor;
|
||||
esp_timer_handle_t g_chip_temp_timer = nullptr;
|
||||
|
||||
void publish_chip_temperature_tick(void * /*arg*/)
|
||||
{
|
||||
if (!g_chip_temp_sensor.fetch()) {
|
||||
return;
|
||||
}
|
||||
kvida::SensorValue value{};
|
||||
if (g_chip_temp_sensor.get(kvida::SensorChannel::ChipTemperature, value)) {
|
||||
float celsius = static_cast<float>(value.val1) + static_cast<float>(value.val2) * 1e-6f;
|
||||
kvida::publish_chip_temperature(celsius);
|
||||
}
|
||||
}
|
||||
esp_timer_handle_t g_lua_tick_timer = nullptr;
|
||||
|
||||
void on_led_color(uint8_t red, uint8_t green, uint8_t blue)
|
||||
{
|
||||
@@ -34,22 +21,23 @@ extern "C" void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "kvida-os starting");
|
||||
|
||||
// Quick end-to-end data-flow proof (real Sensor impl -> MQTT/HA, and
|
||||
// HA -> MQTT -> actuator) using only what's on the ESP32-C6-DevKitC-1
|
||||
// itself: no sensor profiles or hardware wiring exist yet.
|
||||
g_chip_temp_sensor.begin();
|
||||
// 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 = &publish_chip_temperature_tick,
|
||||
.callback = [](void *) { kvida::lua_runtime_tick(); },
|
||||
.arg = nullptr,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "chip_temp_publish",
|
||||
.name = "lua_tick",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &g_chip_temp_timer));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(g_chip_temp_timer, 30LL * 1000000));
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user