12static const char *
const TAG =
"lvgl";
14static const char *
const EVENT_NAMES[] = {
21 "LONG_PRESSED_REPEAT",
52 "SCREEN_UNLOAD_START",
63 if (event_code <
sizeof(EVENT_NAMES) /
sizeof(EVENT_NAMES[0])) {
64 return EVENT_NAMES[event_code];
69static void rounder_cb(lv_disp_drv_t *disp_drv, lv_area_t *area) {
73 auto *comp =
static_cast<LvglComponent *
>(disp_drv->user_data);
74 auto draw_rounding = comp->draw_rounding;
76 area->x1 = area->x1 / draw_rounding * draw_rounding;
77 area->y1 = area->y1 / draw_rounding * draw_rounding;
79 area->x2 = (area->x2 + draw_rounding) / draw_rounding * draw_rounding - 1;
80 area->y2 = (area->y2 + draw_rounding) / draw_rounding * draw_rounding - 1;
86 ESP_LOGCONFIG(TAG,
"LVGL:");
87 ESP_LOGCONFIG(TAG,
" Display width/height: %d x %d", this->
disp_drv_.hor_res, this->disp_drv_.ver_res);
88 ESP_LOGCONFIG(TAG,
" Rotation: %d", this->
rotation);
89 ESP_LOGCONFIG(TAG,
" Draw rounding: %d", (
int) this->
draw_rounding);
94 if (!paused && lv_scr_act() !=
nullptr) {
95 lv_disp_trig_activity(this->
disp_);
96 lv_obj_invalidate(lv_scr_act());
103 lv_update_event =
static_cast<lv_event_code_t
>(lv_event_register_id());
104 lv_api_event =
static_cast<lv_event_code_t
>(lv_event_register_id());
107 lv_obj_add_event_cb(obj, callback, event,
nullptr);
110 lv_event_code_t event2) {
115 lv_event_code_t event2, lv_event_code_t event3) {
121 this->
pages_.push_back(page);
123 lv_disp_set_default(this->
disp_);
127 if (index >= this->
pages_.size())
133 if (this->
pages_.empty() || (this->current_page_ == this->pages_.size() - 1 && !this->page_wrap_))
141 if (this->
pages_.empty() || (this->current_page_ == 0 && !this->page_wrap_))
151 auto width = lv_area_get_width(area);
152 auto height = lv_area_get_height(area);
158 for (lv_coord_t
x = height;
x-- != 0;) {
159 for (lv_coord_t
y = 0;
y != width;
y++) {
160 dst[
y * height +
x] = *ptr++;
164 x1 = this->
disp_drv_.ver_res - area->y1 - height;
166 height = lv_area_get_width(area);
170 for (lv_coord_t
y = height;
y-- != 0;) {
171 for (lv_coord_t
x = width;
x-- != 0;) {
172 dst[
y * width +
x] = *ptr++;
175 x1 = this->
disp_drv_.hor_res - x1 - width;
176 y1 = this->
disp_drv_.ver_res - y1 - height;
180 for (lv_coord_t
x = 0;
x != height;
x++) {
181 for (lv_coord_t
y = width;
y-- != 0;) {
182 dst[
y * height +
x] = *ptr++;
186 y1 = this->
disp_drv_.hor_res - area->x1 - width;
188 height = lv_area_get_width(area);
196 ESP_LOGV(TAG,
"draw buffer x1=%d, y1=%d, width=%d, height=%d", x1, y1, width, height);
206 ESP_LOGVV(TAG,
"flush_cb, area=%d/%d, %d/%d took %dms", area->x1, area->y1, lv_area_get_width(area),
207 lv_area_get_height(area), (
int) (
millis() - now));
209 lv_disp_flush_ready(disp_drv);
229#ifdef USE_LVGL_TOUCHSCREEN
232 lv_indev_drv_init(&this->
drv_);
234 this->
drv_.long_press_repeat_time = long_press_repeat_time;
235 this->
drv_.long_press_time = long_press_time;
236 this->
drv_.type = LV_INDEV_TYPE_POINTER;
237 this->
drv_.user_data =
this;
238 this->
drv_.read_cb = [](lv_indev_drv_t *d, lv_indev_data_t *data) {
240 if (
l->touch_pressed_) {
241 data->point.x =
l->touch_point_.x;
242 data->point.y =
l->touch_point_.y;
243 data->state = LV_INDEV_STATE_PRESSED;
245 data->state = LV_INDEV_STATE_RELEASED;
257#ifdef USE_LVGL_KEY_LISTENER
259 lv_indev_drv_init(&this->
drv_);
261 this->
drv_.user_data =
this;
262 this->
drv_.long_press_time = lpt;
263 this->
drv_.long_press_repeat_time = lprt;
264 this->
drv_.read_cb = [](lv_indev_drv_t *d, lv_indev_data_t *data) {
266 data->state =
l->
pressed_ ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
268 data->enc_diff = (int16_t) (
l->count_ -
l->last_count_);
269 l->last_count_ =
l->count_;
270 data->continue_reading =
false;
275#if defined(USE_LVGL_DROPDOWN) || defined(LV_USE_ROLLER)
278 if (selected >= this->
options_.size())
283static std::string join_string(std::vector<std::string>
options) {
284 return std::accumulate(
286 [](
const std::string &a,
const std::string &b) -> std::string { return a + (!a.empty() ?
"\n" :
"") + b; });
290 auto index = std::find(this->
options_.begin(), this->options_.end(), text);
291 if (index != this->
options_.end()) {
293 lv_event_send(this->
obj, lv_api_event,
nullptr);
303 lv_event_send(this->
obj, LV_EVENT_REFRESH,
nullptr);
308#ifdef USE_LVGL_BUTTONMATRIX
313 [](lv_event_t *event) {
315 if (self->key_callback_.size() == 0)
317 auto key_idx = lv_btnmatrix_get_selected_btn(self->obj);
318 if (key_idx == LV_BTNMATRIX_BTN_NONE)
320 if (self->key_map_.count(key_idx) != 0) {
321 self->send_key_(self->key_map_[key_idx]);
324 const auto *str = lv_btnmatrix_get_btn_text(self->obj, key_idx);
325 auto len = strlen(str);
327 self->send_key_(*str++);
329 LV_EVENT_PRESSED,
this);
333#ifdef USE_LVGL_KEYBOARD
334static const char *
const KB_SPECIAL_KEYS[] = {
343 [](lv_event_t *event) {
345 if (self->key_callback_.size() == 0)
348 auto key_idx = lv_btnmatrix_get_selected_btn(self->obj);
349 if (key_idx == LV_BTNMATRIX_BTN_NONE)
351 const char *txt = lv_btnmatrix_get_btn_text(self->obj, key_idx);
354 for (
const auto *kb_special_key : KB_SPECIAL_KEYS) {
355 if (strcmp(txt, kb_special_key) == 0)
359 self->send_key_(*txt++);
361 LV_EVENT_PRESSED,
this);
366 int iterations = 6 - lv_disp_get_inactive_time(this->
disp_) / 60000;
369 while (iterations-- != 0) {
374 auto size = (
random_uint32() % 32) / this->draw_rounding * this->draw_rounding - 1;
378 area.x2 = col + size;
379 area.y2 = row + size;
380 if (area.x2 >= this->disp_drv_.hor_res)
382 if (area.y2 >= this->disp_drv_.ver_res)
385 size_t line_len = lv_area_get_width(&area) * lv_area_get_height(&area) / 2;
386 for (
size_t i = 0; i != line_len; i++) {
414 int draw_rounding,
bool resume_on_input)
415 : draw_rounding(draw_rounding),
416 displays_(std::move(displays)),
417 buffer_frac_(buffer_frac),
418 full_refresh_(full_refresh),
419 resume_on_input_(resume_on_input) {
420 lv_disp_draw_buf_init(&this->
draw_buf_,
nullptr,
nullptr, 0);
431 ESP_LOGCONFIG(TAG,
"LVGL Setup starts");
433 auto width = display->get_width();
434 auto height = display->get_height();
435 size_t buffer_pixels = width * height / this->
buffer_frac_;
436 auto buf_bytes = buffer_pixels * LV_COLOR_DEPTH / 8;
437 void *buffer =
nullptr;
439 buffer = malloc(buf_bytes);
440 if (buffer ==
nullptr)
442 if (buffer ==
nullptr) {
447 lv_disp_draw_buf_init(&this->
draw_buf_, buffer,
nullptr, buffer_pixels);
452 this->
rotation = display->get_rotation();
462 lv_log_register_print_cb([](
const char *buf) {
463 auto next = strchr(buf,
')');
466 while (isspace(*buf))
468 esp_log_printf_(LVGL_LOG_LEVEL, TAG, 0,
"%.*s", (
int) strlen(buf) - 1, buf);
474 this->
show_page(0, LV_SCR_LOAD_ANIM_NONE, 0);
475 lv_disp_trig_activity(this->
disp_);
476 ESP_LOGCONFIG(TAG,
"LVGL Setup complete");
491 lv_timer_handler_run_in_period(5);
494#ifdef USE_LVGL_ANIMIMG
496 auto *animg = (lv_animimg_t *) obj;
497 int32_t
duration = animg->anim.time;
498 lv_animimg_set_duration(obj, 0);
499 lv_animimg_start(obj);
500 lv_animimg_set_duration(obj,
duration);
511#if defined(USE_HOST) || defined(USE_RP2040) || defined(USE_ESP8266)
513 auto *ptr = malloc(size);
514 if (ptr ==
nullptr) {
515 ESP_LOGE(esphome::lvgl::TAG,
"Failed to allocate %zu bytes", size);
522static unsigned cap_bits = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
526 ptr = heap_caps_malloc(size, cap_bits);
527 if (ptr ==
nullptr) {
528 cap_bits = MALLOC_CAP_8BIT;
529 ptr = heap_caps_malloc(size, cap_bits);
531 if (ptr ==
nullptr) {
532 ESP_LOGE(esphome::lvgl::TAG,
"Failed to allocate %zu bytes", size);
535 ESP_LOGV(esphome::lvgl::TAG,
"allocate %zu - > %p", size, ptr);
540 ESP_LOGV(esphome::lvgl::TAG,
"free %p", ptr);
547 ESP_LOGV(esphome::lvgl::TAG,
"realloc %p: %zu", ptr, size);
548 return heap_caps_realloc(ptr, size, cap_bits);
virtual void mark_failed()
Mark this component as failed.
void status_set_error(const char *message="unspecified")
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()