[lvgl] Final stage (#7184)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Clyde Stubbs
2024-08-06 13:56:48 +10:00
committed by GitHub
parent 7074fa06ae
commit 71ea2cec1f
46 changed files with 840 additions and 210 deletions
+32
View File
@@ -0,0 +1,32 @@
import esphome.codegen as cg
from esphome.components import light
from esphome.components.light import LightOutput
import esphome.config_validation as cv
from esphome.const import CONF_GAMMA_CORRECT, CONF_LED, CONF_OUTPUT_ID
from ..defines import CONF_LVGL_ID
from ..lvcode import LvContext
from ..schemas import LVGL_SCHEMA
from ..types import LvType, lvgl_ns
from ..widgets import get_widgets
lv_led_t = LvType("lv_led_t")
LVLight = lvgl_ns.class_("LVLight", LightOutput)
CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
{
cv.Optional(CONF_GAMMA_CORRECT, default=0.0): cv.positive_float,
cv.Required(CONF_LED): cv.use_id(lv_led_t),
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(LVLight),
}
).extend(LVGL_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
await light.register_light(var, config)
paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_LED)
widget = widget[0]
async with LvContext(paren) as ctx:
ctx.add(var.set_obj(widget.obj))
@@ -0,0 +1,48 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/light/light_output.h"
#include "../lvgl_esphome.h"
namespace esphome {
namespace lvgl {
class LVLight : public light::LightOutput {
public:
light::LightTraits get_traits() override {
auto traits = light::LightTraits();
traits.set_supported_color_modes({light::ColorMode::RGB});
return traits;
}
void write_state(light::LightState *state) override {
float red, green, blue;
state->current_values_as_rgb(&red, &green, &blue, false);
auto color = lv_color_make(red * 255, green * 255, blue * 255);
if (this->obj_ != nullptr) {
this->set_value_(color);
} else {
this->initial_value_ = color;
}
}
void set_obj(lv_obj_t *obj) {
this->obj_ = obj;
if (this->initial_value_) {
lv_led_set_color(obj, this->initial_value_.value());
lv_led_on(obj);
this->initial_value_.reset();
}
}
protected:
void set_value_(lv_color_t value) {
lv_led_set_color(this->obj_, value);
lv_led_on(this->obj_);
lv_event_send(this->obj_, lv_custom_event, nullptr);
}
lv_obj_t *obj_{};
optional<lv_color_t> initial_value_{};
};
} // namespace lvgl
} // namespace esphome