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;
90 " Display width/height: %d x %d\n"
91 " Buffer size: %zu%%\n"
94 this->
disp_drv_.hor_res, this->disp_drv_.ver_res, 100 / this->buffer_frac_, this->rotation,
95 (
int) this->draw_rounding);
100 if (!paused && lv_scr_act() !=
nullptr) {
101 lv_disp_trig_activity(this->
disp_);
102 lv_obj_invalidate(lv_scr_act());
109 lv_update_event =
static_cast<lv_event_code_t
>(lv_event_register_id());
110 lv_api_event =
static_cast<lv_event_code_t
>(lv_event_register_id());
113 lv_obj_add_event_cb(obj, callback, event,
nullptr);
116 lv_event_code_t event2) {
121 lv_event_code_t event2, lv_event_code_t event3) {
127 this->
pages_.push_back(page);
129 lv_disp_set_default(this->
disp_);
133 if (index >= this->
pages_.size())
139 if (this->
pages_.empty() || (this->current_page_ == this->pages_.size() - 1 && !this->page_wrap_))
147 if (this->
pages_.empty() || (this->current_page_ == 0 && !this->page_wrap_))
157 auto width = lv_area_get_width(area);
158 auto height = lv_area_get_height(area);
164 for (lv_coord_t
x = height;
x-- != 0;) {
165 for (lv_coord_t
y = 0;
y != width;
y++) {
166 dst[
y * height +
x] = *ptr++;
170 x1 = this->
disp_drv_.ver_res - area->y1 - height;
172 height = lv_area_get_width(area);
176 for (lv_coord_t
y = height;
y-- != 0;) {
177 for (lv_coord_t
x = width;
x-- != 0;) {
178 dst[
y * width +
x] = *ptr++;
181 x1 = this->
disp_drv_.hor_res - x1 - width;
182 y1 = this->
disp_drv_.ver_res - y1 - height;
186 for (lv_coord_t
x = 0;
x != height;
x++) {
187 for (lv_coord_t
y = width;
y-- != 0;) {
188 dst[
y * height +
x] = *ptr++;
192 y1 = this->
disp_drv_.hor_res - area->x1 - width;
194 height = lv_area_get_width(area);
202 ESP_LOGV(TAG,
"draw buffer x1=%d, y1=%d, width=%d, height=%d", x1, y1, width, height);
212 ESP_LOGVV(TAG,
"flush_cb, area=%d/%d, %d/%d took %dms", area->x1, area->y1, lv_area_get_width(area),
213 lv_area_get_height(area), (
int) (
millis() - now));
215 lv_disp_flush_ready(disp_drv);
235#ifdef USE_LVGL_TOUCHSCREEN
238 lv_indev_drv_init(&this->
drv_);
240 this->
drv_.long_press_repeat_time = long_press_repeat_time;
241 this->
drv_.long_press_time = long_press_time;
242 this->
drv_.type = LV_INDEV_TYPE_POINTER;
243 this->
drv_.user_data =
this;
244 this->
drv_.read_cb = [](lv_indev_drv_t *d, lv_indev_data_t *data) {
246 if (
l->touch_pressed_) {
247 data->point.x =
l->touch_point_.x;
248 data->point.y =
l->touch_point_.y;
249 data->state = LV_INDEV_STATE_PRESSED;
251 data->state = LV_INDEV_STATE_RELEASED;
263#ifdef USE_LVGL_KEY_LISTENER
265 lv_indev_drv_init(&this->
drv_);
267 this->
drv_.user_data =
this;
268 this->
drv_.long_press_time = lpt;
269 this->
drv_.long_press_repeat_time = lprt;
270 this->
drv_.read_cb = [](lv_indev_drv_t *d, lv_indev_data_t *data) {
272 data->state =
l->
pressed_ ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
274 data->enc_diff = (int16_t) (
l->count_ -
l->last_count_);
275 l->last_count_ =
l->count_;
276 data->continue_reading =
false;
281#if defined(USE_LVGL_DROPDOWN) || defined(LV_USE_ROLLER)
284 if (selected >= this->
options_.size())
289static std::string join_string(std::vector<std::string>
options) {
290 return std::accumulate(
292 [](
const std::string &a,
const std::string &b) -> std::string { return a + (!a.empty() ?
"\n" :
"") + b; });
296 auto index = std::find(this->
options_.begin(), this->options_.end(), text);
297 if (index != this->
options_.end()) {
299 lv_event_send(this->
obj, lv_api_event,
nullptr);
309 lv_event_send(this->
obj, LV_EVENT_REFRESH,
nullptr);
314#ifdef USE_LVGL_BUTTONMATRIX
319 [](lv_event_t *event) {
321 if (self->key_callback_.size() == 0)
323 auto key_idx = lv_btnmatrix_get_selected_btn(self->obj);
324 if (key_idx == LV_BTNMATRIX_BTN_NONE)
326 if (self->key_map_.count(key_idx) != 0) {
327 self->send_key_(self->key_map_[key_idx]);
330 const auto *str = lv_btnmatrix_get_btn_text(self->obj, key_idx);
331 auto len = strlen(str);
333 self->send_key_(*str++);
335 LV_EVENT_PRESSED,
this);
339#ifdef USE_LVGL_KEYBOARD
340static const char *
const KB_SPECIAL_KEYS[] = {
349 [](lv_event_t *event) {
351 if (self->key_callback_.size() == 0)
354 auto key_idx = lv_btnmatrix_get_selected_btn(self->obj);
355 if (key_idx == LV_BTNMATRIX_BTN_NONE)
357 const char *txt = lv_btnmatrix_get_btn_text(self->obj, key_idx);
360 for (
const auto *kb_special_key : KB_SPECIAL_KEYS) {
361 if (strcmp(txt, kb_special_key) == 0)
365 self->send_key_(*txt++);
367 LV_EVENT_PRESSED,
this);
372 int iterations = 6 - lv_disp_get_inactive_time(this->
disp_) / 60000;
375 while (iterations-- != 0) {
380 auto size = (
random_uint32() % 32) / this->draw_rounding * this->draw_rounding - 1;
384 area.x2 = col + size;
385 area.y2 = row + size;
386 if (area.x2 >= this->disp_drv_.hor_res)
388 if (area.y2 >= this->disp_drv_.ver_res)
391 size_t line_len = lv_area_get_width(&area) * lv_area_get_height(&area) / 2;
392 for (
size_t i = 0; i != line_len; i++) {
420 int draw_rounding,
bool resume_on_input)
421 : draw_rounding(draw_rounding),
422 displays_(std::move(displays)),
423 buffer_frac_(buffer_frac),
424 full_refresh_(full_refresh),
425 resume_on_input_(resume_on_input) {
426 lv_disp_draw_buf_init(&this->
draw_buf_,
nullptr,
nullptr, 0);
438 auto width = display->get_width();
439 auto height = display->get_height();
443 size_t buffer_pixels = width * height / frac;
444 auto buf_bytes = buffer_pixels * LV_COLOR_DEPTH / 8;
445 void *buffer =
nullptr;
447 buffer = malloc(buf_bytes);
448 if (buffer ==
nullptr)
452 frac = MIN_BUFFER_FRAC;
453 buffer_pixels /= MIN_BUFFER_FRAC;
454 buf_bytes /= MIN_BUFFER_FRAC;
457 if (buffer ==
nullptr) {
463 lv_disp_draw_buf_init(&this->
draw_buf_, buffer,
nullptr, buffer_pixels);
468 this->
rotation = display->get_rotation();
478 lv_log_register_print_cb([](
const char *buf) {
479 auto next = strchr(buf,
')');
482 while (isspace(*buf))
484 esp_log_printf_(LVGL_LOG_LEVEL, TAG, 0,
"%.*s", (
int) strlen(buf) - 1, buf);
490 this->
show_page(0, LV_SCR_LOAD_ANIM_NONE, 0);
491 lv_disp_trig_activity(this->
disp_);
506 lv_timer_handler_run_in_period(5);
509#ifdef USE_LVGL_ANIMIMG
511 auto *animg = (lv_animimg_t *) obj;
512 int32_t
duration = animg->anim.time;
513 lv_animimg_set_duration(obj, 0);
514 lv_animimg_start(obj);
515 lv_animimg_set_duration(obj,
duration);
526#if defined(USE_HOST) || defined(USE_RP2040) || defined(USE_ESP8266)
528 auto *ptr = malloc(size);
529 if (ptr ==
nullptr) {
530 ESP_LOGE(esphome::lvgl::TAG,
"Failed to allocate %zu bytes", size);
537static unsigned cap_bits = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
541 ptr = heap_caps_malloc(size, cap_bits);
542 if (ptr ==
nullptr) {
543 cap_bits = MALLOC_CAP_8BIT;
544 ptr = heap_caps_malloc(size, cap_bits);
546 if (ptr ==
nullptr) {
547 ESP_LOGE(esphome::lvgl::TAG,
"Failed to allocate %zu bytes", size);
550 ESP_LOGV(esphome::lvgl::TAG,
"allocate %zu - > %p", size, ptr);
555 ESP_LOGV(esphome::lvgl::TAG,
"free %p", ptr);
562 ESP_LOGV(esphome::lvgl::TAG,
"realloc %p: %zu", ptr, size);
563 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.
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)
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
void show_page(size_t index, lv_scr_load_anim_t anim, uint32_t time)
void show_prev_page(lv_scr_load_anim_t anim, uint32_t time)
CallbackManager< void(bool)> pause_callbacks_
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 add_on_pause_callback(std::function< void(bool)> &&callback)
void draw_buffer_(const lv_area_t *area, lv_color_t *ptr)
PauseTrigger(LvglComponent *parent, TemplatableValue< bool > paused)
TemplatableValue< bool > paused_
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()