12static const char *
const TAG =
"lvgl";
14static const size_t MIN_BUFFER_FRAC = 8;
16static const char *
const EVENT_NAMES[] = {
23 "LONG_PRESSED_REPEAT",
54 "SCREEN_UNLOAD_START",
65 if (event_code <
sizeof(EVENT_NAMES) /
sizeof(EVENT_NAMES[0])) {
66 return EVENT_NAMES[event_code];
71static void rounder_cb(lv_disp_drv_t *disp_drv, lv_area_t *area) {
75 auto *comp =
static_cast<LvglComponent *
>(disp_drv->user_data);
76 auto draw_rounding = comp->draw_rounding;
78 area->x1 = area->x1 / draw_rounding * draw_rounding;
79 area->y1 = area->y1 / draw_rounding * draw_rounding;
81 area->x2 = (area->x2 + draw_rounding) / draw_rounding * draw_rounding - 1;
82 area->y2 = (area->y2 + draw_rounding) / draw_rounding * draw_rounding - 1;
86 ESP_LOGVV(TAG,
"Draw end: %" PRIu32
" pixels in %" PRIu32
" ms", px, time);
87 auto *comp =
static_cast<LvglComponent *
>(disp_drv->user_data);
92 ESP_LOGVV(TAG,
"Draw start");
93 auto *comp =
static_cast<LvglComponent *
>(disp_drv->user_data);
102 " Display width/height: %d x %d\n"
103 " Buffer size: %zu%%\n"
105 " Draw rounding: %d",
106 this->
disp_drv_.hor_res, this->disp_drv_.ver_res, 100 / this->buffer_frac_, this->rotation,
107 (
int) this->draw_rounding);
112 if (!paused && lv_scr_act() !=
nullptr) {
113 lv_disp_trig_activity(this->
disp_);
114 lv_obj_invalidate(lv_scr_act());
124 lv_update_event =
static_cast<lv_event_code_t
>(lv_event_register_id());
125 lv_api_event =
static_cast<lv_event_code_t
>(lv_event_register_id());
128 lv_obj_add_event_cb(obj, callback, event,
nullptr);
131 lv_event_code_t event2) {
136 lv_event_code_t event2, lv_event_code_t event3) {
142 this->
pages_.push_back(page);
144 lv_disp_set_default(this->
disp_);
148 if (index >= this->
pages_.size())
154 if (this->
pages_.empty() || (this->current_page_ == this->pages_.size() - 1 && !this->page_wrap_))
162 if (this->
pages_.empty() || (this->current_page_ == 0 && !this->page_wrap_))
172 auto width = lv_area_get_width(area);
173 auto height = lv_area_get_height(area);
180 for (lv_coord_t
x = height;
x-- != 0;) {
181 for (lv_coord_t
y = 0;
y != width;
y++) {
182 dst[
y * height_rounded +
x] = *ptr++;
186 x1 = this->
disp_drv_.ver_res - area->y1 - height;
188 width = height_rounded;
192 for (lv_coord_t
y = height;
y-- != 0;) {
193 for (lv_coord_t
x = width;
x-- != 0;) {
194 dst[
y * width +
x] = *ptr++;
197 x1 = this->
disp_drv_.hor_res - x1 - width;
198 y1 = this->
disp_drv_.ver_res - y1 - height;
202 for (lv_coord_t
x = 0;
x != height;
x++) {
203 for (lv_coord_t
y = width;
y-- != 0;) {
204 dst[
y * height_rounded +
x] = *ptr++;
208 y1 = this->
disp_drv_.hor_res - area->x1 - width;
210 width = height_rounded;
218 ESP_LOGV(TAG,
"draw buffer x1=%d, y1=%d, width=%d, height=%d", x1, y1, width, height);
228 ESP_LOGVV(TAG,
"flush_cb, area=%d/%d, %d/%d took %dms", area->x1, area->y1, lv_area_get_width(area),
229 lv_area_get_height(area), (
int) (
millis() - now));
231 lv_disp_flush_ready(disp_drv);
244#ifdef USE_LVGL_TOUCHSCREEN
247 lv_indev_drv_init(&this->
drv_);
249 this->
drv_.long_press_repeat_time = long_press_repeat_time;
250 this->
drv_.long_press_time = long_press_time;
251 this->
drv_.type = LV_INDEV_TYPE_POINTER;
252 this->
drv_.user_data =
this;
253 this->
drv_.read_cb = [](lv_indev_drv_t *d, lv_indev_data_t *data) {
255 if (
l->touch_pressed_) {
256 data->point.x =
l->touch_point_.x;
257 data->point.y =
l->touch_point_.y;
258 data->state = LV_INDEV_STATE_PRESSED;
260 data->state = LV_INDEV_STATE_RELEASED;
272#ifdef USE_LVGL_KEY_LISTENER
274 lv_indev_drv_init(&this->
drv_);
276 this->
drv_.user_data =
this;
277 this->
drv_.long_press_time = lpt;
278 this->
drv_.long_press_repeat_time = lprt;
279 this->
drv_.read_cb = [](lv_indev_drv_t *d, lv_indev_data_t *data) {
281 data->state =
l->
pressed_ ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
283 data->enc_diff = (int16_t) (
l->count_ -
l->last_count_);
284 l->last_count_ =
l->count_;
285 data->continue_reading =
false;
290#if defined(USE_LVGL_DROPDOWN) || defined(LV_USE_ROLLER)
293 if (selected >= this->
options_.size())
298static std::string join_string(std::vector<std::string>
options) {
299 return std::accumulate(
301 [](
const std::string &a,
const std::string &b) -> std::string { return a + (!a.empty() ?
"\n" :
"") + b; });
305 auto index = std::find(this->
options_.begin(), this->options_.end(), text);
306 if (index != this->
options_.end()) {
308 lv_event_send(this->
obj, lv_api_event,
nullptr);
318 lv_event_send(this->
obj, LV_EVENT_REFRESH,
nullptr);
323#ifdef USE_LVGL_BUTTONMATRIX
328 [](lv_event_t *event) {
330 if (self->key_callback_.size() == 0)
332 auto key_idx = lv_btnmatrix_get_selected_btn(self->obj);
333 if (key_idx == LV_BTNMATRIX_BTN_NONE)
335 if (self->key_map_.count(key_idx) != 0) {
336 self->send_key_(self->key_map_[key_idx]);
339 const auto *str = lv_btnmatrix_get_btn_text(self->obj, key_idx);
340 auto len = strlen(str);
342 self->send_key_(*str++);
344 LV_EVENT_PRESSED,
this);
348#ifdef USE_LVGL_KEYBOARD
349static const char *
const KB_SPECIAL_KEYS[] = {
358 [](lv_event_t *event) {
360 if (self->key_callback_.size() == 0)
363 auto key_idx = lv_btnmatrix_get_selected_btn(self->obj);
364 if (key_idx == LV_BTNMATRIX_BTN_NONE)
366 const char *txt = lv_btnmatrix_get_btn_text(self->obj, key_idx);
369 for (
const auto *kb_special_key : KB_SPECIAL_KEYS) {
370 if (strcmp(txt, kb_special_key) == 0)
374 self->send_key_(*txt++);
376 LV_EVENT_PRESSED,
this);
381 int iterations = 6 - lv_disp_get_inactive_time(this->
disp_) / 60000;
384 while (iterations-- != 0) {
389 auto size = (
random_uint32() % 32) / this->draw_rounding * this->draw_rounding - 1;
393 area.x2 = col + size;
394 area.y2 = row + size;
395 if (area.x2 >= this->disp_drv_.hor_res)
397 if (area.y2 >= this->disp_drv_.ver_res)
400 size_t line_len = lv_area_get_width(&area) * lv_area_get_height(&area) / 2;
401 for (
size_t i = 0; i != line_len; i++) {
429 int draw_rounding,
bool resume_on_input)
430 : draw_rounding(draw_rounding),
431 displays_(std::move(displays)),
432 buffer_frac_(buffer_frac),
433 full_refresh_(full_refresh),
434 resume_on_input_(resume_on_input) {
435 lv_disp_draw_buf_init(&this->
draw_buf_,
nullptr,
nullptr, 0);
449 auto width = (display->get_width() + rounding - 1) / rounding * rounding;
450 auto height = (display->get_height() + rounding - 1) / rounding * rounding;
454 size_t buffer_pixels = width * height / frac;
455 auto buf_bytes = buffer_pixels * LV_COLOR_DEPTH / 8;
456 void *buffer =
nullptr;
458 buffer = malloc(buf_bytes);
459 if (buffer ==
nullptr)
463 frac = MIN_BUFFER_FRAC;
464 buffer_pixels /= MIN_BUFFER_FRAC;
465 buf_bytes /= MIN_BUFFER_FRAC;
468 if (buffer ==
nullptr) {
474 lv_disp_draw_buf_init(&this->
draw_buf_, buffer,
nullptr, buffer_pixels);
475 this->
disp_drv_.hor_res = display->get_width();
476 this->
disp_drv_.ver_res = display->get_height();
478 this->
rotation = display->get_rotation();
494 lv_log_register_print_cb([](
const char *buf) {
495 auto next = strchr(buf,
')');
498 while (isspace(*buf))
500 esp_log_printf_(LVGL_LOG_LEVEL, TAG, 0,
"%.*s", (
int) strlen(buf) - 1, buf);
506 this->
show_page(0, LV_SCR_LOAD_ANIM_NONE, 0);
507 lv_disp_trig_activity(this->
disp_);
522 lv_timer_handler_run_in_period(5);
526#ifdef USE_LVGL_ANIMIMG
528 auto *animg = (lv_animimg_t *) obj;
529 int32_t
duration = animg->anim.time;
530 lv_animimg_set_duration(obj, 0);
531 lv_animimg_start(obj);
532 lv_animimg_set_duration(obj,
duration);
543#if defined(USE_HOST) || defined(USE_RP2040) || defined(USE_ESP8266)
545 auto *ptr = malloc(size);
546 if (ptr ==
nullptr) {
547 ESP_LOGE(esphome::lvgl::TAG,
"Failed to allocate %zu bytes", size);
554static unsigned cap_bits = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
558 ptr = heap_caps_malloc(size, cap_bits);
559 if (ptr ==
nullptr) {
560 cap_bits = MALLOC_CAP_8BIT;
561 ptr = heap_caps_malloc(size, cap_bits);
563 if (ptr ==
nullptr) {
564 ESP_LOGE(esphome::lvgl::TAG,
"Failed to allocate %zu bytes", size);
567 ESP_LOGV(esphome::lvgl::TAG,
"allocate %zu - > %p", size, ptr);
572 ESP_LOGV(esphome::lvgl::TAG,
"free %p", ptr);
579 ESP_LOGV(esphome::lvgl::TAG,
"realloc %p: %zu", ptr, size);
580 return heap_caps_realloc(ptr, size, cap_bits);
virtual void mark_failed()
Mark this component as failed.
void status_set_error(const char *message=nullptr)
void set_parent(T *parent)
Set the parent of this object.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
IdleTrigger(LvglComponent *parent, TemplatableValue< uint32_t > timeout)
TemplatableValue< uint32_t > timeout_
LVEncoderListener(lv_indev_type_t type, uint16_t lpt, uint16_t lprt)
LVTouchListener(uint16_t long_press_time, uint16_t long_press_repeat_time, LvglComponent *parent)
touchscreen::TouchPoint touch_point_
void update(const touchscreen::TouchPoints_t &tpoints) override
virtual void set_obj(lv_obj_t *lv_obj)
void set_obj(lv_obj_t *lv_obj) override
void set_selected_text(const std::string &text, lv_anim_enable_t anim)
void set_options(std::vector< std::string > options)
std::string get_selected_text()
std::vector< std::string > options_
virtual void set_selected_index(size_t index, lv_anim_enable_t anim)=0
virtual size_t get_selected_index()=0
virtual void set_option_string(const char *options)=0
Component for rendering LVGL.
void set_paused(bool paused, bool show_snow)
size_t get_current_page() const
static void static_flush_cb(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
Trigger * pause_callback_
Trigger * resume_callback_
std::vector< LvPageType * > pages_
static void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event)
void show_next_page(lv_scr_load_anim_t anim, uint32_t time)
void dump_config() override
CallbackManager< void(uint32_t)> idle_callbacks_
void add_on_idle_callback(std::function< void(uint32_t)> &&callback)
lv_disp_draw_buf_t draw_buf_
display::DisplayRotation rotation
Trigger * draw_start_callback_
void show_page(size_t index, lv_scr_load_anim_t anim, uint32_t time)
Trigger * draw_end_callback_
void show_prev_page(lv_scr_load_anim_t anim, uint32_t time)
static void render_start_cb(lv_disp_drv_t *disp_drv)
std::vector< display::Display * > displays_
static void esphome_lvgl_init()
Initialize the LVGL library and register custom events.
void flush_cb_(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
LvglComponent(std::vector< display::Display * > displays, float buffer_frac, bool full_refresh, int draw_rounding, bool resume_on_input)
void add_page(LvPageType *page)
void draw_buffer_(const lv_area_t *area, lv_color_t *ptr)
static void monitor_cb(lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px)
void * lv_custom_mem_alloc(size_t size)
void lv_custom_mem_free(void *ptr)
void * lv_custom_mem_realloc(void *ptr, size_t size)
@ DISPLAY_ROTATION_0_DEGREES
@ DISPLAY_ROTATION_270_DEGREES
@ DISPLAY_ROTATION_180_DEGREES
@ DISPLAY_ROTATION_90_DEGREES
void lv_animimg_stop(lv_obj_t *obj)
std::string lv_event_code_name_for(uint8_t event_code)
lv_event_code_t lv_update_event
void(_lv_event_t *) event_callback_t
lv_event_code_t lv_api_event
std::vector< TouchPoint > TouchPoints_t
Providing packet encoding functions for exchanging data with a remote host.
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
std::string str_sprintf(const char *fmt,...)
uint32_t IRAM_ATTR HOT millis()