Merge branch 'dev' into dev

This commit is contained in:
CptSkippy 2024-08-26 08:30:54 -07:00 committed by GitHub
commit fb8fff2e6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 349 additions and 160 deletions

View file

@ -1,34 +1,34 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, spi
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
CONF_REACTIVE_POWER,
CONF_VOLTAGE,
CONF_CURRENT,
CONF_FORWARD_ACTIVE_ENERGY,
CONF_FREQUENCY,
CONF_ID,
CONF_LINE_FREQUENCY,
CONF_POWER,
CONF_POWER_FACTOR,
CONF_FREQUENCY,
CONF_FORWARD_ACTIVE_ENERGY,
CONF_REACTIVE_POWER,
CONF_REVERSE_ACTIVE_ENERGY,
CONF_VOLTAGE,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_POWER_FACTOR,
DEVICE_CLASS_VOLTAGE,
ICON_LIGHTBULB,
ICON_CURRENT_AC,
ICON_LIGHTBULB,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
UNIT_AMPERE,
UNIT_HERTZ,
UNIT_VOLT,
UNIT_AMPERE,
UNIT_WATT,
UNIT_VOLT_AMPS_REACTIVE,
UNIT_WATT,
UNIT_WATT_HOURS,
)
CONF_LINE_FREQUENCY = "line_frequency"
CONF_METER_CONSTANT = "meter_constant"
CONF_PL_CONST = "pl_const"
CONF_GAIN_PGA = "gain_pga"

View file

@ -7,6 +7,7 @@ from esphome.const import (
CONF_FORWARD_ACTIVE_ENERGY,
CONF_FREQUENCY,
CONF_ID,
CONF_LINE_FREQUENCY,
CONF_PHASE_A,
CONF_PHASE_ANGLE,
CONF_PHASE_B,
@ -39,7 +40,6 @@ from esphome.const import (
from . import atm90e32_ns
CONF_LINE_FREQUENCY = "line_frequency"
CONF_CHIP_TEMPERATURE = "chip_temperature"
CONF_GAIN_PGA = "gain_pga"
CONF_CURRENT_PHASES = "current_phases"

View file

@ -8,6 +8,8 @@
#endif
#include <driver/ledc.h>
#include <cinttypes>
#define CLOCK_FREQUENCY 80e6f
#ifdef USE_ARDUINO
@ -115,20 +117,22 @@ void LEDCOutput::write_state(float state) {
const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
const float duty_rounded = roundf(state * max_duty);
auto duty = static_cast<uint32_t>(duty_rounded);
ESP_LOGV(TAG, "Setting duty: %" PRIu32 " on channel %u", duty, this->channel_);
#ifdef USE_ARDUINO
ESP_LOGV(TAG, "Setting duty: %u on channel %u", duty, this->channel_);
ledcWrite(this->channel_, duty);
#endif
#ifdef USE_ESP_IDF
// ensure that 100% on is not 99.975% on
if ((duty == max_duty) && (max_duty != 1)) {
duty = max_duty + 1;
}
auto speed_mode = get_speed_mode(channel_);
auto chan_num = static_cast<ledc_channel_t>(channel_ % 8);
int hpoint = ledc_angle_to_htop(this->phase_angle_, this->bit_depth_);
if (duty == max_duty) {
ledc_stop(speed_mode, chan_num, 1);
} else if (duty == 0) {
ledc_stop(speed_mode, chan_num, 0);
} else {
ledc_set_duty_with_hpoint(speed_mode, chan_num, duty, hpoint);
ledc_update_duty(speed_mode, chan_num);
}
#endif
}

View file

@ -266,7 +266,10 @@ async def to_code(config):
await add_top_layer(config)
await msgboxes_to_code(config)
await disp_update(f"{lv_component}->get_disp()", config)
# At this point only the setup code should be generated
assert LvContext.added_lambda_count == 1
Widget.set_completed()
async with LvContext(lv_component):
await generate_triggers(lv_component)
for conf in config.get(CONF_ON_IDLE, ()):
templ = await cg.templatable(conf[CONF_TIMEOUT], [], cg.uint32)

View file

@ -5,6 +5,7 @@ from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_TIMEOUT
from esphome.cpp_generator import RawExpression
from esphome.cpp_types import nullptr
from .defines import (
@ -26,6 +27,7 @@ from .lvcode import (
add_line_marks,
lv,
lv_add,
lv_expr,
lv_obj,
lvgl_comp,
)
@ -38,7 +40,13 @@ from .types import (
lv_disp_t,
lv_obj_t,
)
from .widgets import Widget, get_widgets, lv_scr_act, set_obj_properties
from .widgets import (
Widget,
get_widgets,
lv_scr_act,
set_obj_properties,
wait_for_widgets,
)
async def action_to_code(
@ -48,9 +56,11 @@ async def action_to_code(
template_arg,
args,
):
await wait_for_widgets()
async with LambdaContext(parameters=args, where=action_id) as context:
with LvConditional(lv_expr.is_pre_initialise()):
context.add(RawExpression("return"))
for widget in widgets:
with LvConditional(widget.obj != nullptr):
await action(widget)
var = cg.new_Pvariable(action_id, template_arg, await context.get_lambda())
return var
@ -147,7 +157,7 @@ async def lvgl_update_to_code(config, action_id, template_arg, args):
widgets = await get_widgets(config)
w = widgets[0]
disp = f"{w.obj}->get_disp()"
async with LambdaContext(parameters=args, where=action_id) as context:
async with LambdaContext(LVGL_COMP_ARG, where=action_id) as context:
await disp_update(disp, config)
var = cg.new_Pvariable(action_id, template_arg, await context.get_lambda())
await cg.register_parented(var, w.var)

View file

@ -10,7 +10,7 @@ from ..defines import CONF_LVGL_ID, CONF_WIDGET
from ..lvcode import EVENT_ARG, LambdaContext, LvContext
from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, lv_pseudo_button_t
from ..widgets import Widget, get_widgets
from ..widgets import Widget, get_widgets, wait_for_widgets
CONFIG_SCHEMA = (
binary_sensor_schema(BinarySensor)
@ -29,6 +29,7 @@ async def to_code(config):
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
assert isinstance(widget, Widget)
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as pressed_ctx:
pressed_ctx.add(sensor.publish_state(widget.is_pressed()))
async with LvContext(paren) as ctx:

View file

@ -505,4 +505,10 @@ DEFAULT_ESPHOME_FONT = "esphome_lv_default_font"
def join_enums(enums, prefix=""):
return literal("|".join(f"(int){prefix}{e.upper()}" for e in enums))
enums = list(enums)
enums.sort()
# If a prefix is provided, prepend each constant with the prefix, and assume that all the constants are within the
# same namespace, otherwise cast to int to avoid triggering warnings about mixing enum types.
if prefix:
return literal("|".join(f"{prefix}{e.upper()}" for e in enums))
return literal("|".join(f"(int){e.upper()}" for e in enums))

View file

@ -8,7 +8,7 @@ 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
from ..widgets import get_widgets, wait_for_widgets
lv_led_t = LvType("lv_led_t")
LVLight = lvgl_ns.class_("LVLight", LightOutput)
@ -28,5 +28,6 @@ async def to_code(config):
paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_LED)
widget = widget[0]
await wait_for_widgets()
async with LvContext(paren) as ctx:
ctx.add(var.set_obj(widget.obj))

View file

@ -176,6 +176,8 @@ class LvContext(LambdaContext):
Code generation into the LVGL initialisation code (called in `setup()`)
"""
added_lambda_count = 0
def __init__(self, lv_component, args=None):
self.args = args or LVGL_COMP_ARG
super().__init__(parameters=self.args)
@ -183,6 +185,7 @@ class LvContext(LambdaContext):
async def add_init_lambda(self):
cg.add(self.lv_component.add_init_lambda(await self.get_lambda()))
LvContext.added_lambda_count += 1
async def __aexit__(self, exc_type, exc_val, exc_tb):
await super().__aexit__(exc_type, exc_val, exc_tb)

View file

@ -294,6 +294,13 @@ void LvglComponent::loop() {
}
lv_timer_handler_run_in_period(5);
}
bool lv_is_pre_initialise() {
if (!lv_is_initialized()) {
ESP_LOGE(TAG, "LVGL call before component is initialised");
return true;
}
return false;
}
#ifdef USE_LVGL_IMAGE
lv_img_dsc_t *lv_img_from(image::Image *src, lv_img_dsc_t *img_dsc) {

View file

@ -40,6 +40,7 @@ namespace lvgl {
extern lv_event_code_t lv_api_event; // NOLINT
extern lv_event_code_t lv_update_event; // NOLINT
extern bool lv_is_pre_initialise();
#ifdef USE_LVGL_COLOR
inline lv_color_t lv_color_from(Color color) { return lv_color_make(color.red, color.green, color.blue); }
#endif // USE_LVGL_COLOR

View file

@ -16,7 +16,7 @@ from ..lvcode import (
)
from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LvNumber, lvgl_ns
from ..widgets import get_widgets
from ..widgets import get_widgets, wait_for_widgets
LVGLNumber = lvgl_ns.class_("LVGLNumber", number.Number)
@ -44,11 +44,13 @@ async def to_code(config):
step=widget.get_step(),
)
await wait_for_widgets()
async with LambdaContext([(cg.float_, "v")]) as control:
await widget.set_property(
"value", MockObj("v") * MockObj(widget.get_scale()), config[CONF_ANIMATED]
)
lv.event_send(widget.obj, API_EVENT, cg.nullptr)
control.add(var.publish_state(widget.get_value()))
async with LambdaContext(EVENT_ARG) as event:
event.add(var.publish_state(widget.get_value()))
event_code = (

View file

@ -15,7 +15,7 @@ from ..lvcode import (
)
from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LvSelect, lvgl_ns
from ..widgets import get_widgets
from ..widgets import get_widgets, wait_for_widgets
LVGLSelect = lvgl_ns.class_("LVGLSelect", select.Select)
@ -37,11 +37,13 @@ async def to_code(config):
options = widget.config.get(CONF_OPTIONS, [])
selector = await select.new_select(config, options=options)
paren = await cg.get_variable(config[CONF_LVGL_ID])
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as pub_ctx:
pub_ctx.add(selector.publish_index(widget.get_value()))
async with LambdaContext([(cg.uint16, "v")]) as control:
await widget.set_property("selected", "v", animated=config[CONF_ANIMATED])
lv.event_send(widget.obj, API_EVENT, cg.nullptr)
control.add(selector.publish_index(widget.get_value()))
async with LvContext(paren) as ctx:
lv_add(selector.set_control_lambda(await control.get_lambda()))
ctx.add(

View file

@ -14,7 +14,7 @@ from ..lvcode import (
)
from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LvNumber
from ..widgets import Widget, get_widgets
from ..widgets import Widget, get_widgets, wait_for_widgets
CONFIG_SCHEMA = (
sensor_schema(Sensor)
@ -33,6 +33,7 @@ async def to_code(config):
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
assert isinstance(widget, Widget)
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as lamb:
lv_add(sensor.publish_state(widget.get_value()))
async with LvContext(paren, LVGL_COMP_ARG):

View file

@ -3,7 +3,7 @@ from esphome.components.switch import Switch, new_switch, switch_schema
import esphome.config_validation as cv
from esphome.cpp_generator import MockObj
from ..defines import CONF_LVGL_ID, CONF_WIDGET
from ..defines import CONF_LVGL_ID, CONF_WIDGET, literal
from ..lvcode import (
API_EVENT,
EVENT_ARG,
@ -16,7 +16,7 @@ from ..lvcode import (
)
from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LV_STATE, lv_pseudo_button_t, lvgl_ns
from ..widgets import get_widgets
from ..widgets import get_widgets, wait_for_widgets
LVGLSwitch = lvgl_ns.class_("LVGLSwitch", Switch)
CONFIG_SCHEMA = (
@ -35,6 +35,7 @@ async def to_code(config):
paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as checked_ctx:
checked_ctx.add(switch.publish_state(widget.get_value()))
async with LambdaContext([(cg.bool_, "v")]) as control:
@ -43,6 +44,7 @@ async def to_code(config):
cond.else_()
widget.clear_state(LV_STATE.CHECKED)
lv.event_send(widget.obj, API_EVENT, cg.nullptr)
control.add(switch.publish_state(literal("v")))
async with LvContext(paren) as ctx:
lv_add(switch.set_control_lambda(await control.get_lambda()))
ctx.add(

View file

@ -15,7 +15,7 @@ from ..lvcode import (
)
from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LvText, lvgl_ns
from ..widgets import get_widgets
from ..widgets import get_widgets, wait_for_widgets
LVGLText = lvgl_ns.class_("LVGLText", text.Text)
@ -32,9 +32,11 @@ async def to_code(config):
paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
async with LambdaContext([(cg.std_string, "text_value")]) as control:
await widget.set_property("text", "text_value.c_str())")
lv.event_send(widget.obj, API_EVENT, None)
control.add(textvar.publish_state(widget.get_value()))
async with LambdaContext(EVENT_ARG) as lamb:
lv_add(textvar.publish_state(widget.get_value()))
async with LvContext(paren):

View file

@ -10,7 +10,7 @@ from ..defines import CONF_LVGL_ID, CONF_WIDGET
from ..lvcode import API_EVENT, EVENT_ARG, UPDATE_EVENT, LambdaContext, LvContext
from ..schemas import LVGL_SCHEMA
from ..types import LV_EVENT, LvText
from ..widgets import get_widgets
from ..widgets import get_widgets, wait_for_widgets
CONFIG_SCHEMA = (
text_sensor_schema(TextSensor)
@ -28,6 +28,7 @@ async def to_code(config):
paren = await cg.get_variable(config[CONF_LVGL_ID])
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as pressed_ctx:
pressed_ctx.add(sensor.publish_state(widget.get_value()))
async with LvContext(paren) as ctx:

View file

@ -118,7 +118,14 @@ class Widget:
def clear_flag(self, flag):
return lv_obj.clear_flag(self.obj, literal(flag))
async def set_property(self, prop, value, animated: bool = None):
async def set_property(self, prop, value, animated: bool = None, lv_name=None):
"""
Set a property of the widget.
:param prop: The property name
:param value: The value
:param animated: If the change should be animated
:param lv_name: The base type of the widget e.g. "obj"
"""
if isinstance(value, dict):
value = value.get(prop)
if isinstance(ALL_STYLES.get(prop), LValidator):
@ -131,11 +138,12 @@ class Widget:
value = value.total_milliseconds
if isinstance(value, str):
value = literal(value)
lv_name = lv_name or self.type.lv_name
if animated is None or self.type.animated is not True:
lv.call(f"{self.type.lv_name}_set_{prop}", self.obj, value)
lv.call(f"{lv_name}_set_{prop}", self.obj, value)
else:
lv.call(
f"{self.type.lv_name}_set_{prop}",
f"{lv_name}_set_{prop}",
self.obj,
value,
literal("LV_ANIM_ON" if animated else "LV_ANIM_OFF"),
@ -223,6 +231,19 @@ async def get_widget_(wid: Widget):
return await FakeAwaitable(get_widget_generator(wid))
def widgets_wait_generator():
while True:
if Widget.widgets_completed:
return
yield
async def wait_for_widgets():
if Widget.widgets_completed:
return
await FakeAwaitable(widgets_wait_generator())
async def get_widgets(config: Union[dict, list], id: str = CONF_ID) -> list[Widget]:
if not config:
return []
@ -306,8 +327,15 @@ async def set_obj_properties(w: Widget, config):
lv_obj.set_flex_align(w.obj, main, cross, track)
parts = collect_parts(config)
for part, states in parts.items():
part = "LV_PART_" + part.upper()
for state, props in states.items():
lv_state = join_enums((f"LV_STATE_{state}", f"LV_PART_{part}"))
state = "LV_STATE_" + state.upper()
if state == "LV_STATE_DEFAULT":
lv_state = literal(part)
elif part == "LV_PART_MAIN":
lv_state = literal(state)
else:
lv_state = join_enums((state, part))
for style_id in props.get(CONF_STYLES, ()):
lv_obj.add_style(w.obj, MockObj(style_id), lv_state)
for prop, value in {
@ -371,7 +399,7 @@ async def set_obj_properties(w: Widget, config):
w.add_state(state)
cond.else_()
w.clear_state(state)
await w.set_property(CONF_SCROLLBAR_MODE, config)
await w.set_property(CONF_SCROLLBAR_MODE, config, lv_name="obj")
async def add_widgets(parent: Widget, config: dict):

View file

@ -3,7 +3,7 @@ import functools
import esphome.codegen as cg
import esphome.config_validation as cv
from ..defines import CONF_MAIN, literal
from ..defines import CONF_MAIN
from ..lvcode import lv
from ..types import LvType
from . import Widget, WidgetType
@ -38,11 +38,13 @@ LINE_SCHEMA = {
class LineType(WidgetType):
def __init__(self):
super().__init__(CONF_LINE, LvType("lv_line_t"), (CONF_MAIN,), LINE_SCHEMA)
super().__init__(
CONF_LINE, LvType("lv_line_t"), (CONF_MAIN,), LINE_SCHEMA, modify_schema={}
)
async def to_code(self, w: Widget, config):
"""For a line object, create and add the points"""
data = literal(config[CONF_POINTS])
if data := config.get(CONF_POINTS):
points = cg.static_const_array(config[CONF_POINT_LIST_ID], data)
lv.line_set_points(w.obj, points, len(data))

View file

@ -13,7 +13,7 @@ from ..defines import (
TYPE_FLEX,
literal,
)
from ..helpers import add_lv_use
from ..helpers import add_lv_use, lvgl_components_required
from ..lv_validation import lv_bool, lv_pct, lv_text
from ..lvcode import (
EVENT_ARG,
@ -72,6 +72,7 @@ async def msgbox_to_code(conf):
*buttonmatrix_spec.get_uses(),
*button_spec.get_uses(),
)
lvgl_components_required.add("BUTTONMATRIX")
messagebox_id = conf[CONF_ID]
outer = lv_Pvariable(lv_obj_t, messagebox_id.id)
buttonmatrix = new_Pvariable(

View file

@ -431,6 +431,7 @@ CONF_LIGHT_ID = "light_id"
CONF_LIGHTNING_ENERGY = "lightning_energy"
CONF_LIGHTNING_THRESHOLD = "lightning_threshold"
CONF_LIMIT_MODE = "limit_mode"
CONF_LINE_FREQUENCY = "line_frequency"
CONF_LINE_THICKNESS = "line_thickness"
CONF_LINE_TYPE = "line_type"
CONF_LOADED_INTEGRATIONS = "loaded_integrations"

View file

@ -1,19 +1,64 @@
#include "bytebuffer.h"
#include <cassert>
#include <cstring>
namespace esphome {
ByteBuffer ByteBuffer::create(size_t capacity) {
std::vector<uint8_t> data(capacity);
return {data};
}
ByteBuffer ByteBuffer::wrap(uint8_t *ptr, size_t len) {
ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len, Endian endianness) {
// there is a double copy happening here, could be optimized but at cost of clarity.
std::vector<uint8_t> data(ptr, ptr + len);
return {data};
ByteBuffer buffer = {data};
buffer.endianness_ = endianness;
return buffer;
}
ByteBuffer ByteBuffer::wrap(std::vector<uint8_t> data) { return {std::move(data)}; }
ByteBuffer ByteBuffer::wrap(std::vector<uint8_t> const &data, Endian endianness) {
ByteBuffer buffer = {data};
buffer.endianness_ = endianness;
return buffer;
}
ByteBuffer ByteBuffer::wrap(uint8_t value) {
ByteBuffer buffer = ByteBuffer(1);
buffer.put_uint8(value);
buffer.flip();
return buffer;
}
ByteBuffer ByteBuffer::wrap(uint16_t value, Endian endianness) {
ByteBuffer buffer = ByteBuffer(2, endianness);
buffer.put_uint16(value);
buffer.flip();
return buffer;
}
ByteBuffer ByteBuffer::wrap(uint32_t value, Endian endianness) {
ByteBuffer buffer = ByteBuffer(4, endianness);
buffer.put_uint32(value);
buffer.flip();
return buffer;
}
ByteBuffer ByteBuffer::wrap(uint64_t value, Endian endianness) {
ByteBuffer buffer = ByteBuffer(8, endianness);
buffer.put_uint64(value);
buffer.flip();
return buffer;
}
ByteBuffer ByteBuffer::wrap(float value, Endian endianness) {
ByteBuffer buffer = ByteBuffer(sizeof(float), endianness);
buffer.put_float(value);
buffer.flip();
return buffer;
}
ByteBuffer ByteBuffer::wrap(double value, Endian endianness) {
ByteBuffer buffer = ByteBuffer(sizeof(double), endianness);
buffer.put_double(value);
buffer.flip();
return buffer;
}
void ByteBuffer::set_limit(size_t limit) {
assert(limit <= this->get_capacity());
@ -27,108 +72,102 @@ void ByteBuffer::clear() {
this->limit_ = this->get_capacity();
this->position_ = 0;
}
uint16_t ByteBuffer::get_uint16() {
assert(this->get_remaining() >= 2);
uint16_t value;
if (endianness_ == LITTLE) {
value = this->data_[this->position_++];
value |= this->data_[this->position_++] << 8;
} else {
value = this->data_[this->position_++] << 8;
value |= this->data_[this->position_++];
}
return value;
void ByteBuffer::flip() {
this->limit_ = this->position_;
this->position_ = 0;
}
uint32_t ByteBuffer::get_uint32() {
assert(this->get_remaining() >= 4);
uint32_t value;
if (endianness_ == LITTLE) {
value = this->data_[this->position_++];
value |= this->data_[this->position_++] << 8;
value |= this->data_[this->position_++] << 16;
value |= this->data_[this->position_++] << 24;
} else {
value = this->data_[this->position_++] << 24;
value |= this->data_[this->position_++] << 16;
value |= this->data_[this->position_++] << 8;
value |= this->data_[this->position_++];
}
return value;
}
uint32_t ByteBuffer::get_uint24() {
assert(this->get_remaining() >= 3);
uint32_t value;
if (endianness_ == LITTLE) {
value = this->data_[this->position_++];
value |= this->data_[this->position_++] << 8;
value |= this->data_[this->position_++] << 16;
} else {
value = this->data_[this->position_++] << 16;
value |= this->data_[this->position_++] << 8;
value |= this->data_[this->position_++];
}
return value;
}
uint32_t ByteBuffer::get_int24() {
auto value = this->get_uint24();
uint32_t mask = (~(uint32_t) 0) << 23;
if ((value & mask) != 0)
value |= mask;
return value;
}
/// Getters
uint8_t ByteBuffer::get_uint8() {
assert(this->get_remaining() >= 1);
return this->data_[this->position_++];
}
float ByteBuffer::get_float() {
auto value = this->get_uint32();
return *(float *) &value;
uint64_t ByteBuffer::get_uint(size_t length) {
assert(this->get_remaining() >= length);
uint64_t value = 0;
if (this->endianness_ == LITTLE) {
this->position_ += length;
auto index = this->position_;
while (length-- != 0) {
value <<= 8;
value |= this->data_[--index];
}
} else {
while (length-- != 0) {
value <<= 8;
value |= this->data_[this->position_++];
}
}
return value;
}
uint32_t ByteBuffer::get_int24() {
auto value = this->get_uint24();
uint32_t mask = (~static_cast<uint32_t>(0)) << 23;
if ((value & mask) != 0)
value |= mask;
return value;
}
float ByteBuffer::get_float() {
assert(this->get_remaining() >= sizeof(float));
auto ui_value = this->get_uint32();
float value;
memcpy(&value, &ui_value, sizeof(float));
return value;
}
double ByteBuffer::get_double() {
assert(this->get_remaining() >= sizeof(double));
auto ui_value = this->get_uint64();
double value;
memcpy(&value, &ui_value, sizeof(double));
return value;
}
std::vector<uint8_t> ByteBuffer::get_vector(size_t length) {
assert(this->get_remaining() >= length);
auto start = this->data_.begin() + this->position_;
this->position_ += length;
return {start, start + length};
}
/// Putters
void ByteBuffer::put_uint8(uint8_t value) {
assert(this->get_remaining() >= 1);
this->data_[this->position_++] = value;
}
void ByteBuffer::put_uint16(uint16_t value) {
assert(this->get_remaining() >= 2);
void ByteBuffer::put_uint(uint64_t value, size_t length) {
assert(this->get_remaining() >= length);
if (this->endianness_ == LITTLE) {
this->data_[this->position_++] = (uint8_t) value;
this->data_[this->position_++] = (uint8_t) (value >> 8);
while (length-- != 0) {
this->data_[this->position_++] = static_cast<uint8_t>(value);
value >>= 8;
}
} else {
this->data_[this->position_++] = (uint8_t) (value >> 8);
this->data_[this->position_++] = (uint8_t) value;
this->position_ += length;
auto index = this->position_;
while (length-- != 0) {
this->data_[--index] = static_cast<uint8_t>(value);
value >>= 8;
}
}
}
void ByteBuffer::put_uint24(uint32_t value) {
assert(this->get_remaining() >= 3);
if (this->endianness_ == LITTLE) {
this->data_[this->position_++] = (uint8_t) value;
this->data_[this->position_++] = (uint8_t) (value >> 8);
this->data_[this->position_++] = (uint8_t) (value >> 16);
} else {
this->data_[this->position_++] = (uint8_t) (value >> 16);
this->data_[this->position_++] = (uint8_t) (value >> 8);
this->data_[this->position_++] = (uint8_t) value;
}
void ByteBuffer::put_float(float value) {
static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported");
assert(this->get_remaining() >= sizeof(float));
uint32_t ui_value;
memcpy(&ui_value, &value, sizeof(float)); // this work-around required to silence compiler warnings
this->put_uint32(ui_value);
}
void ByteBuffer::put_uint32(uint32_t value) {
assert(this->get_remaining() >= 4);
if (this->endianness_ == LITTLE) {
this->data_[this->position_++] = (uint8_t) value;
this->data_[this->position_++] = (uint8_t) (value >> 8);
this->data_[this->position_++] = (uint8_t) (value >> 16);
this->data_[this->position_++] = (uint8_t) (value >> 24);
} else {
this->data_[this->position_++] = (uint8_t) (value >> 24);
this->data_[this->position_++] = (uint8_t) (value >> 16);
this->data_[this->position_++] = (uint8_t) (value >> 8);
this->data_[this->position_++] = (uint8_t) value;
}
void ByteBuffer::put_double(double value) {
static_assert(sizeof(double) == sizeof(uint64_t), "Double sizes other than 64 bit not supported");
assert(this->get_remaining() >= sizeof(double));
uint64_t ui_value;
memcpy(&ui_value, &value, sizeof(double));
this->put_uint64(ui_value);
}
void ByteBuffer::put_float(float value) { this->put_uint32(*(uint32_t *) &value); }
void ByteBuffer::flip() {
this->limit_ = this->position_;
this->position_ = 0;
void ByteBuffer::put_vector(const std::vector<uint8_t> &value) {
assert(this->get_remaining() >= value.size());
std::copy(value.begin(), value.end(), this->data_.begin() + this->position_);
this->position_ += value.size();
}
} // namespace esphome

View file

@ -15,55 +15,103 @@ enum Endian { LITTLE, BIG };
*
* There are three variables maintained pointing into the buffer:
*
* 0 <= position <= limit <= capacity
*
* capacity: the maximum amount of data that can be stored
* capacity: the maximum amount of data that can be stored - set on construction and cannot be changed
* limit: the limit of the data currently available to get or put
* position: the current insert or extract position
*
* 0 <= position <= limit <= capacity
*
* In addition a mark can be set to the current position with mark(). A subsequent call to reset() will restore
* the position to the mark.
*
* The buffer can be marked to be little-endian (default) or big-endian. All subsequent operations will use that order.
*
* The flip() operation will reset the position to 0 and limit to the current position. This is useful for reading
* data from a buffer after it has been written.
*
*/
class ByteBuffer {
public:
// Default constructor (compatibility with TEMPLATABLE_VALUE)
ByteBuffer() : ByteBuffer(std::vector<uint8_t>()) {}
/**
* Create a new Bytebuffer with the given capacity
*/
static ByteBuffer create(size_t capacity);
ByteBuffer(size_t capacity, Endian endianness = LITTLE)
: data_(std::vector<uint8_t>(capacity)), endianness_(endianness), limit_(capacity){};
/**
* Wrap an existing vector in a Bytebufffer
* Wrap an existing vector in a ByteBufffer
*/
static ByteBuffer wrap(std::vector<uint8_t> data);
static ByteBuffer wrap(std::vector<uint8_t> const &data, Endian endianness = LITTLE);
/**
* Wrap an existing array in a Bytebufffer
* Wrap an existing array in a ByteBuffer. Note that this will create a copy of the data.
*/
static ByteBuffer wrap(uint8_t *ptr, size_t len);
static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness = LITTLE);
// Convenience functions to create a ByteBuffer from a value
static ByteBuffer wrap(uint8_t value);
static ByteBuffer wrap(uint16_t value, Endian endianness = LITTLE);
static ByteBuffer wrap(uint32_t value, Endian endianness = LITTLE);
static ByteBuffer wrap(uint64_t value, Endian endianness = LITTLE);
static ByteBuffer wrap(int8_t value) { return wrap(static_cast<uint8_t>(value)); }
static ByteBuffer wrap(int16_t value, Endian endianness = LITTLE) {
return wrap(static_cast<uint16_t>(value), endianness);
}
static ByteBuffer wrap(int32_t value, Endian endianness = LITTLE) {
return wrap(static_cast<uint32_t>(value), endianness);
}
static ByteBuffer wrap(int64_t value, Endian endianness = LITTLE) {
return wrap(static_cast<uint64_t>(value), endianness);
}
static ByteBuffer wrap(float value, Endian endianness = LITTLE);
static ByteBuffer wrap(double value, Endian endianness = LITTLE);
static ByteBuffer wrap(bool value) { return wrap(static_cast<uint8_t>(value)); }
// Get an integral value from the buffer, increment position by length
uint64_t get_uint(size_t length);
// Get one byte from the buffer, increment position by 1
uint8_t get_uint8();
// Get a 16 bit unsigned value, increment by 2
uint16_t get_uint16();
uint16_t get_uint16() { return static_cast<uint16_t>(this->get_uint(sizeof(uint16_t))); };
// Get a 24 bit unsigned value, increment by 3
uint32_t get_uint24();
uint32_t get_uint24() { return static_cast<uint32_t>(this->get_uint(3)); };
// Get a 32 bit unsigned value, increment by 4
uint32_t get_uint32();
// signed versions of the get functions
uint8_t get_int8() { return (int8_t) this->get_uint8(); };
int16_t get_int16() { return (int16_t) this->get_uint16(); }
uint32_t get_uint32() { return static_cast<uint32_t>(this->get_uint(sizeof(uint32_t))); };
// Get a 64 bit unsigned value, increment by 8
uint64_t get_uint64() { return this->get_uint(sizeof(uint64_t)); };
// Signed versions of the get functions
uint8_t get_int8() { return static_cast<int8_t>(this->get_uint8()); };
int16_t get_int16() { return static_cast<int16_t>(this->get_uint(sizeof(int16_t))); }
uint32_t get_int24();
int32_t get_int32() { return (int32_t) this->get_uint32(); }
int32_t get_int32() { return static_cast<int32_t>(this->get_uint(sizeof(int32_t))); }
int64_t get_int64() { return static_cast<int64_t>(this->get_uint(sizeof(int64_t))); }
// Get a float value, increment by 4
float get_float();
// Get a double value, increment by 8
double get_double();
// Get a bool value, increment by 1
bool get_bool() { return this->get_uint8(); }
// Get vector of bytes, increment by length
std::vector<uint8_t> get_vector(size_t length);
// put values into the buffer, increment the position accordingly
// Put values into the buffer, increment the position accordingly
// put any integral value, length represents the number of bytes
void put_uint(uint64_t value, size_t length);
void put_uint8(uint8_t value);
void put_uint16(uint16_t value);
void put_uint24(uint32_t value);
void put_uint32(uint32_t value);
void put_uint16(uint16_t value) { this->put_uint(value, sizeof(uint16_t)); }
void put_uint24(uint32_t value) { this->put_uint(value, 3); }
void put_uint32(uint32_t value) { this->put_uint(value, sizeof(uint32_t)); }
void put_uint64(uint64_t value) { this->put_uint(value, sizeof(uint64_t)); }
// Signed versions of the put functions
void put_int8(int8_t value) { this->put_uint8(static_cast<uint8_t>(value)); }
void put_int16(int32_t value) { this->put_uint(static_cast<uint16_t>(value), sizeof(uint16_t)); }
void put_int24(int32_t value) { this->put_uint(static_cast<uint32_t>(value), 3); }
void put_int32(int32_t value) { this->put_uint(static_cast<uint32_t>(value), sizeof(uint32_t)); }
void put_int64(int64_t value) { this->put_uint(static_cast<uint64_t>(value), sizeof(uint64_t)); }
// Extra put functions
void put_float(float value);
void put_double(double value);
void put_bool(bool value) { this->put_uint8(value); }
void put_vector(const std::vector<uint8_t> &value);
inline size_t get_capacity() const { return this->data_.size(); }
inline size_t get_position() const { return this->position_; }
@ -80,12 +128,12 @@ class ByteBuffer {
// set limit to current position, postition to zero. Used when swapping from write to read operations.
void flip();
// retrieve a pointer to the underlying data.
uint8_t *array() { return this->data_.data(); };
std::vector<uint8_t> get_data() { return this->data_; };
void rewind() { this->position_ = 0; }
void reset() { this->position_ = this->mark_; }
protected:
ByteBuffer(std::vector<uint8_t> data) : data_(std::move(data)) { this->limit_ = this->get_capacity(); }
ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {}
std::vector<uint8_t> data_;
Endian endianness_{LITTLE};
size_t position_{0};

View file

@ -106,6 +106,8 @@ def storage_should_clean(old: StorageJSON, new: StorageJSON) -> bool:
return True
if old.build_path != new.build_path:
return True
if old.loaded_integrations != new.loaded_integrations:
return True
return False
@ -117,7 +119,9 @@ def update_storage_json():
return
if storage_should_clean(old, new):
_LOGGER.info("Core config or version changed, cleaning build files...")
_LOGGER.info(
"Core config, version or integrations changed, cleaning build files..."
)
clean_build()
new.save(path)

View file

@ -1,6 +1,8 @@
lvgl:
log_level: TRACE
bg_color: light_blue
disp_bg_color: 0xffff00
disp_bg_image: cat_image
theme:
obj:
border_width: 1
@ -78,6 +80,9 @@ lvgl:
on_click:
then:
- lvgl.animimg.stop: anim_img
- lvgl.update:
disp_bg_color: 0xffff00
disp_bg_image: cat_image
- label:
text: "Hello shiny day"
text_color: 0xFFFFFF
@ -304,6 +309,17 @@ lvgl:
src: cat_image
align: top_left
y: 50
- tileview:
id: tileview_id
scrollbar_mode: active
tiles:
- id: page_1
row: 0
column: 0
dir: HOR
widgets:
- obj:
bg_color: 0x000000
- id: page2
widgets:
@ -379,6 +395,7 @@ lvgl:
format: "bar value %f"
args: [x]
- line:
id: lv_line_id
align: center
points:
- 5, 5
@ -387,7 +404,10 @@ lvgl:
- 180, 60
- 240, 10
on_click:
lvgl.page.next:
- lvgl.widget.update:
id: lv_line_id
line_color: 0xFFFF
- lvgl.page.next:
- switch:
align: right_mid
- checkbox: