ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
lvgl_esphome.cpp
Go to the documentation of this file.
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5#include "lvgl_esphome.h"
6
7#include "core/lv_global.h"
8#include "core/lv_obj_class_private.h"
9
10#include <numeric>
11
12static void *lv_alloc_draw_buf(size_t size, bool internal);
13static void *draw_buf_alloc_cb(size_t size, lv_color_format_t color_format) { return lv_alloc_draw_buf(size, false); };
14
15namespace esphome::lvgl {
16static const char *const TAG = "lvgl";
17
18static const size_t MIN_BUFFER_FRAC = 8; // buffer must be at least 1/8 of the display size
19static const size_t MIN_BUFFER_SIZE = 2048; // Sensible minimum buffer size
20
21static const char *const EVENT_NAMES[] = {
22 "NONE",
23 "PRESSED",
24 "PRESSING",
25 "PRESS_LOST",
26 "SHORT_CLICKED",
27 "LONG_PRESSED",
28 "LONG_PRESSED_REPEAT",
29 "CLICKED",
30 "RELEASED",
31 "SCROLL_BEGIN",
32 "SCROLL_END",
33 "SCROLL",
34 "GESTURE",
35 "KEY",
36 "FOCUSED",
37 "DEFOCUSED",
38 "LEAVE",
39 "HIT_TEST",
40 "COVER_CHECK",
41 "REFR_EXT_DRAW_SIZE",
42 "DRAW_MAIN_BEGIN",
43 "DRAW_MAIN",
44 "DRAW_MAIN_END",
45 "DRAW_POST_BEGIN",
46 "DRAW_POST",
47 "DRAW_POST_END",
48 "DRAW_PART_BEGIN",
49 "DRAW_PART_END",
50 "VALUE_CHANGED",
51 "INSERT",
52 "REFRESH",
53 "READY",
54 "CANCEL",
55 "DELETE",
56 "CHILD_CHANGED",
57 "CHILD_CREATED",
58 "CHILD_DELETED",
59 "SCREEN_UNLOAD_START",
60 "SCREEN_LOAD_START",
61 "SCREEN_LOADED",
62 "SCREEN_UNLOADED",
63 "SIZE_CHANGED",
64 "STYLE_CHANGED",
65 "LAYOUT_CHANGED",
66 "GET_SELF_SIZE",
67};
68
69static const unsigned LOG_LEVEL_MAP[] = {
70 ESPHOME_LOG_LEVEL_DEBUG, ESPHOME_LOG_LEVEL_INFO, ESPHOME_LOG_LEVEL_WARN,
71 ESPHOME_LOG_LEVEL_ERROR, ESPHOME_LOG_LEVEL_ERROR, ESPHOME_LOG_LEVEL_NONE,
72
73};
74
75std::string lv_event_code_name_for(lv_event_t *event) {
76 auto event_code = lv_event_get_code(event);
77 if (event_code < sizeof(EVENT_NAMES) / sizeof(EVENT_NAMES[0])) {
78 return EVENT_NAMES[event_code];
79 }
80 // max 4 bytes: "%u" with uint8_t (max 255, 3 digits) + null
81 char buf[4];
82 snprintf(buf, sizeof(buf), "%u", event_code);
83 return buf;
84}
85
88 ESP_LOGW(TAG, "Display rotation cannot be changed unless rotation was enabled during setup.");
89 return;
90 }
91 this->rotation_ = rotation;
92 if (this->is_ready()) {
93 this->set_resolution_();
94 lv_obj_update_layout(this->get_screen_active());
95 lv_obj_invalidate(this->get_screen_active());
96 }
97}
98
99void LvglComponent::rotate_coordinates(int32_t &x, int32_t &y) const {
100 switch (this->rotation_) {
101 default:
102 break;
103
105 x = this->width_ - x - 1;
106 y = this->height_ - y - 1;
107 break;
108 }
110 auto tmp = x;
111 x = this->height_ - y - 1;
112 y = tmp;
113 break;
114 }
116 auto tmp = y;
117 y = this->width_ - x - 1;
118 x = tmp;
119 break;
120 }
121 }
122}
123
124static void rounder_cb(lv_event_t *event) {
125 auto *comp = static_cast<LvglComponent *>(lv_event_get_user_data(event));
126 auto *area = static_cast<lv_area_t *>(lv_event_get_param(event));
127 // cater for display driver chips with special requirements for bounds of partial
128 // draw areas. Extend the draw area to satisfy:
129 // * Coordinates must be a multiple of draw_rounding
130 auto draw_rounding = comp->draw_rounding;
131 // round down the start coordinates
132 area->x1 = area->x1 / draw_rounding * draw_rounding;
133 area->y1 = area->y1 / draw_rounding * draw_rounding;
134 // round up the end coordinates
135 area->x2 = (area->x2 + draw_rounding) / draw_rounding * draw_rounding - 1;
136 area->y2 = (area->y2 + draw_rounding) / draw_rounding * draw_rounding - 1;
137}
138
139void LvglComponent::render_end_cb(lv_event_t *event) {
140 auto *comp = static_cast<LvglComponent *>(lv_event_get_user_data(event));
141 comp->draw_end_();
142}
143
144void LvglComponent::render_start_cb(lv_event_t *event) {
145 ESP_LOGVV(TAG, "Draw start");
146 auto *comp = static_cast<LvglComponent *>(lv_event_get_user_data(event));
147 comp->draw_start_();
148}
149
150lv_event_code_t lv_api_event; // NOLINT
151lv_event_code_t lv_update_event; // NOLINT
153 ESP_LOGCONFIG(TAG,
154 "LVGL:\n"
155 " Display width/height: %d x %d\n"
156 " Buffer size: %zu%%\n"
157 " Rotation: %d\n"
158 " Draw rounding: %d",
159 this->width_, this->height_, 100 / this->buffer_frac_, this->rotation_, (int) this->draw_rounding);
160 if (this->rotation_type_ != ROTATION_UNUSED) {
161 const char *rot_type = "hardware via display driver";
163#ifdef USE_ESP32_VARIANT_ESP32P4
164 rot_type = this->ppa_client_ != nullptr ? "software (PPA accelerated)" : "software";
165#else
166 rot_type = "software";
167#endif
168 }
169 ESP_LOGCONFIG(TAG, " Rotation type: %s", rot_type);
170 }
171}
172
173void LvglComponent::set_paused(bool paused, bool show_snow) {
174 this->paused_ = paused;
175 this->show_snow_ = show_snow;
176 if (!paused && lv_screen_active() != nullptr) {
177 lv_display_trigger_activity(this->disp_); // resets the inactivity time
178 lv_obj_invalidate(lv_screen_active());
179 }
180 if (paused && this->pause_callback_ != nullptr)
181 this->pause_callback_->trigger();
182 if (!paused && this->resume_callback_ != nullptr)
183 this->resume_callback_->trigger();
184}
185
187 lv_init();
188 // override draw buf alloc to ensure proper alignment for PPA
189 LV_GLOBAL_DEFAULT()->draw_buf_handlers.buf_malloc_cb = draw_buf_alloc_cb;
190 LV_GLOBAL_DEFAULT()->draw_buf_handlers.buf_free_cb = lv_free_core;
191 LV_GLOBAL_DEFAULT()->image_cache_draw_buf_handlers.buf_malloc_cb = draw_buf_alloc_cb;
192 LV_GLOBAL_DEFAULT()->image_cache_draw_buf_handlers.buf_free_cb = lv_free_core;
193 LV_GLOBAL_DEFAULT()->font_draw_buf_handlers.buf_malloc_cb = draw_buf_alloc_cb;
194 LV_GLOBAL_DEFAULT()->font_draw_buf_handlers.buf_free_cb = lv_free_core;
195 lv_tick_set_cb([] { return millis(); });
196 lv_update_event = static_cast<lv_event_code_t>(lv_event_register_id());
197 lv_api_event = static_cast<lv_event_code_t>(lv_event_register_id());
198}
199
200void LvglComponent::add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event) {
201 lv_obj_add_event_cb(obj, callback, event, nullptr);
202}
203
204void LvglComponent::add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1,
205 lv_event_code_t event2) {
206 add_event_cb(obj, callback, event1);
207 add_event_cb(obj, callback, event2);
208}
209
210void LvglComponent::add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1,
211 lv_event_code_t event2, lv_event_code_t event3) {
212 add_event_cb(obj, callback, event1);
213 add_event_cb(obj, callback, event2);
214 add_event_cb(obj, callback, event3);
215}
216
218 this->pages_.push_back(page);
219 page->set_parent(this);
220 lv_display_set_default(this->disp_);
221 page->setup(this->pages_.size() - 1);
222}
223
224void LvglComponent::show_page(size_t index, lv_screen_load_anim_t anim, uint32_t time) {
225 if (index >= this->pages_.size())
226 return;
227 this->current_page_ = index;
228 if (anim == LV_SCREEN_LOAD_ANIM_NONE) {
229 lv_screen_load(this->pages_[this->current_page_]->obj);
230 } else {
231 lv_screen_load_anim(this->pages_[this->current_page_]->obj, anim, time, 0, false);
232 }
233}
234
235void LvglComponent::show_next_page(lv_screen_load_anim_t anim, uint32_t time) {
236 if (this->pages_.empty() || (this->current_page_ == this->pages_.size() - 1 && !this->page_wrap_))
237 return;
238 size_t start = this->current_page_;
239 do {
240 this->current_page_ = (this->current_page_ + 1) % this->pages_.size();
241 if (this->current_page_ == start)
242 return; // all pages have skip=true (guaranteed not to happen by YAML validation)
243 } while (this->pages_[this->current_page_]->skip); // skip empty pages()
244 this->show_page(this->current_page_, anim, time);
245}
246
247void LvglComponent::show_prev_page(lv_screen_load_anim_t anim, uint32_t time) {
248 if (this->pages_.empty() || (this->current_page_ == 0 && !this->page_wrap_))
249 return;
250 size_t start = this->current_page_;
251 do {
252 this->current_page_ = (this->current_page_ + this->pages_.size() - 1) % this->pages_.size();
253 if (this->current_page_ == start)
254 return; // all pages have skip=true (guaranteed not to happen by YAML validation)
255 } while (this->pages_[this->current_page_]->skip); // skip empty pages()
256 this->show_page(this->current_page_, anim, time);
257}
258
259size_t LvglComponent::get_current_page() const { return this->current_page_; }
260bool LvPageType::is_showing() const { return this->parent_->get_current_page() == this->index; }
261
262#ifdef USE_ESP32_VARIANT_ESP32P4
263bool LvglComponent::ppa_rotate_(const lv_color_data *src, lv_color_data *dst, uint16_t width, uint16_t height,
264 uint32_t height_rounded) {
265 ppa_srm_rotation_angle_t angle;
266 uint16_t out_w, out_h;
267
268 // Map ESPHome clockwise display rotation to PPA counter-clockwise angles
269 switch (this->rotation_) {
271 angle = PPA_SRM_ROTATION_ANGLE_270; // 270° CCW = 90° CW
272 out_w = height_rounded;
273 out_h = width;
274 break;
276 angle = PPA_SRM_ROTATION_ANGLE_180;
277 out_w = width;
278 out_h = height;
279 break;
281 angle = PPA_SRM_ROTATION_ANGLE_90; // 90° CCW = 270° CW
282 out_w = height_rounded;
283 out_h = width;
284 break;
285 default:
286 return false; // No rotation needed
287 }
288
289 // Align buffer size to cache line (LV_DRAW_BUF_ALIGN) as required by PPA DMA
290 // the underlying buffer will be large enough as the size is also padded when allocating.
291 size_t out_buf_size = out_w * out_h * sizeof(lv_color_data);
292 out_buf_size = LV_ROUND_UP(out_buf_size, LV_DRAW_BUF_ALIGN);
293
294 ppa_srm_oper_config_t srm_config{};
295 srm_config.in.buffer = src;
296 srm_config.in.pic_w = width;
297 srm_config.in.pic_h = height;
298 srm_config.in.block_w = width;
299 srm_config.in.block_h = height;
300#if LV_COLOR_DEPTH == 16
301 srm_config.in.srm_cm = PPA_SRM_COLOR_MODE_RGB565;
302#elif LV_COLOR_DEPTH == 32
303 srm_config.in.srm_cm = PPA_SRM_COLOR_MODE_ARGB8888;
304#endif
305 srm_config.out.buffer = dst;
306 srm_config.out.buffer_size = out_buf_size;
307 srm_config.out.pic_w = out_w;
308 srm_config.out.pic_h = out_h;
309#if LV_COLOR_DEPTH == 16
310 srm_config.out.srm_cm = PPA_SRM_COLOR_MODE_RGB565;
311#elif LV_COLOR_DEPTH == 32
312 srm_config.out.srm_cm = PPA_SRM_COLOR_MODE_ARGB8888;
313#endif
314 srm_config.rotation_angle = angle;
315 srm_config.scale_x = 1.0f;
316 srm_config.scale_y = 1.0f;
317 srm_config.mode = PPA_TRANS_MODE_BLOCKING;
318
319 esp_err_t ret = ppa_do_scale_rotate_mirror(this->ppa_client_, &srm_config);
320 if (ret != ESP_OK) {
321 ESP_LOGW(TAG, "PPA rotation failed: %s", esp_err_to_name(ret));
322 ESP_LOGW(TAG, "PPA SRM: in=%ux%u src=%p, out=%ux%u dst=%p size=%zu, angle=%d", width, height, src, out_w, out_h,
323 dst, out_buf_size, (int) angle);
324 return false;
325 }
326 return true;
327}
328#endif // USE_ESP32_VARIANT_ESP32P4
329
330void LvglComponent::draw_buffer_(const lv_area_t *area, lv_color_data *ptr) {
331 auto width = lv_area_get_width(area);
332 auto height = lv_area_get_height(area);
333 auto height_rounded = (height + this->draw_rounding - 1) / this->draw_rounding * this->draw_rounding;
334 auto x1 = area->x1;
335 auto y1 = area->y1;
336 if (this->rotation_type_ == ROTATION_SOFTWARE) {
337 lv_color_data *dst = reinterpret_cast<lv_color_data *>(this->rotate_buf_);
338#ifdef USE_ESP32_VARIANT_ESP32P4
339 bool ppa_done = this->ppa_client_ != nullptr && this->ppa_rotate_(ptr, dst, width, height, height_rounded);
340 if (!ppa_done)
341#endif
342 {
343 switch (this->rotation_) {
345 for (lv_coord_t x = height; x-- != 0;) {
346 for (lv_coord_t y = 0; y != width; y++) {
347 dst[y * height_rounded + x] = *ptr++;
348 }
349 }
350 break;
351
353 for (lv_coord_t y = height; y-- != 0;) {
354 for (lv_coord_t x = width; x-- != 0;) {
355 dst[y * width + x] = *ptr++;
356 }
357 }
358 break;
359
361 for (lv_coord_t x = 0; x != height; x++) {
362 for (lv_coord_t y = width; y-- != 0;) {
363 dst[y * height_rounded + x] = *ptr++;
364 }
365 }
366 break;
367
368 default:
369 dst = ptr;
370 break;
371 }
372 }
373 // Coordinate adjustments apply regardless of PPA or SW rotation
374 switch (this->rotation_) {
376 y1 = x1;
377 x1 = this->width_ - area->y1 - height;
378 height = width;
379 width = height_rounded;
380 break;
381
383 x1 = this->width_ - x1 - width;
384 y1 = this->height_ - y1 - height;
385 break;
386
388 x1 = y1;
389 y1 = this->height_ - area->x1 - width;
390 height = width;
391 width = height_rounded;
392 break;
393
394 default:
395 break;
396 }
397 ptr = dst;
398 }
399 for (auto *display : this->displays_) {
400 display->draw_pixels_at(x1, y1, width, height, (const uint8_t *) ptr, display::COLOR_ORDER_RGB, LV_BITNESS,
401 this->big_endian_);
402 }
403}
404
405void LvglComponent::flush_cb_(lv_display_t *disp_drv, const lv_area_t *area, uint8_t *color_p) {
406 if (!this->is_paused()) {
407 auto now = millis();
408 this->draw_buffer_(area, reinterpret_cast<lv_color_data *>(color_p));
409 ESP_LOGV(TAG, "flush_cb, area=%d/%d, %d/%d took %dms", (int) area->x1, (int) area->y1,
410 (int) lv_area_get_width(area), (int) lv_area_get_height(area), (int) (millis() - now));
411 }
412 lv_display_flush_ready(disp_drv);
413}
414
416 parent->add_on_idle_callback([this](uint32_t idle_time) {
417 if (!this->is_idle_ && idle_time > this->timeout_.value()) {
418 this->is_idle_ = true;
419 this->trigger();
420 } else if (this->is_idle_ && idle_time < this->timeout_.value()) {
421 this->is_idle_ = false;
422 }
423 });
424}
425
426#ifdef USE_LVGL_TOUCHSCREEN
427LVTouchListener::LVTouchListener(uint16_t long_press_time, uint16_t long_press_repeat_time, LvglComponent *parent) {
428 this->set_parent(parent);
429 this->drv_ = lv_indev_create();
430 lv_indev_set_type(this->drv_, LV_INDEV_TYPE_POINTER);
431 lv_indev_set_disp(this->drv_, parent->get_disp());
432 lv_indev_set_long_press_time(this->drv_, long_press_time);
433 // long press repeat time TBD
434 lv_indev_set_user_data(this->drv_, this);
435 lv_indev_set_read_cb(this->drv_, [](lv_indev_t *d, lv_indev_data_t *data) {
436 auto *l = static_cast<LVTouchListener *>(lv_indev_get_user_data(d));
437 if (l->touch_pressed_) {
438 data->point.x = l->touch_point_.x;
439 data->point.y = l->touch_point_.y;
440 l->parent_->rotate_coordinates(data->point.x, data->point.y);
441 data->state = LV_INDEV_STATE_PRESSED;
442 } else {
443 data->state = LV_INDEV_STATE_RELEASED;
444 }
445 });
446}
447
449 this->touch_pressed_ = !this->parent_->is_paused() && !tpoints.empty();
450 if (this->touch_pressed_)
451 this->touch_point_ = tpoints[0];
452}
453#endif // USE_LVGL_TOUCHSCREEN
454
455#ifdef USE_LVGL_METER
456
457int16_t lv_get_needle_angle_for_value(lv_obj_t *obj, int value) {
458 auto *scale = lv_obj_get_parent(obj);
459 auto min_value = lv_scale_get_range_min_value(scale);
460 return ((value - min_value) * lv_scale_get_angle_range(scale) / (lv_scale_get_range_max_value(scale) - min_value) +
461 lv_scale_get_rotation((scale))) %
462 360;
463}
464
465void IndicatorLine::set_obj(lv_obj_t *lv_obj) {
466 LvCompound::set_obj(lv_obj);
467 lv_line_set_points(lv_obj, this->points_, 2);
468 lv_obj_add_event_cb(
469 lv_obj_get_parent(obj),
470 [](lv_event_t *e) {
471 auto *indicator = static_cast<IndicatorLine *>(lv_event_get_user_data(e));
472 indicator->update_length_();
473 ESP_LOGV(TAG, "Updated length, value = %d", indicator->angle_);
474 },
475 LV_EVENT_SIZE_CHANGED, this);
476}
477
479 auto angle = lv_get_needle_angle_for_value(this->obj, value);
480 if (angle != this->angle_) {
481 this->angle_ = angle;
482 this->update_length_();
483 }
484}
485
486void IndicatorLine::update_length_() {
487 auto cx = lv_obj_get_width(lv_obj_get_parent(this->obj)) / 2;
488 auto cy = lv_obj_get_height(lv_obj_get_parent(this->obj)) / 2;
489 auto radius = clamp_at_most(cx, cy);
490 auto length = lv_obj_get_style_length(this->obj, LV_PART_MAIN);
491 auto radial_offset = lv_obj_get_style_radial_offset(this->obj, LV_PART_MAIN);
492 if (LV_COORD_IS_PCT(radial_offset)) {
493 radial_offset = radius * LV_COORD_GET_PCT(radial_offset) / 100;
494 }
495 if (LV_COORD_IS_PCT(length)) {
496 length = radius * LV_COORD_GET_PCT(length) / 100;
497 } else if (length < 0) {
498 length += radius;
499 }
500 auto x = lv_trigo_cos(this->angle_) / 32768.0f;
501 auto y = lv_trigo_sin(this->angle_) / 32768.0f;
502 // radius here also represents the offset of the scale center from top left
503 this->points_[0].x = radius + radial_offset * x;
504 this->points_[0].y = radius + radial_offset * y;
505 this->points_[1].x = radius + x * (radial_offset + length);
506 this->points_[1].y = radius + y * (radial_offset + length);
507 lv_obj_refresh_self_size(this->obj);
508 lv_obj_invalidate(this->obj);
509}
510#endif
511
512#ifdef USE_LVGL_KEY_LISTENER
513LVEncoderListener::LVEncoderListener(lv_indev_type_t type, uint16_t long_press_time, uint16_t long_press_repeat_time) {
514 this->drv_ = lv_indev_create();
515 lv_indev_set_type(this->drv_, type);
516 lv_indev_set_long_press_time(this->drv_, long_press_time);
517 lv_indev_set_long_press_repeat_time(this->drv_, long_press_repeat_time);
518 lv_indev_set_user_data(this->drv_, this);
519 lv_indev_set_read_cb(this->drv_, [](lv_indev_t *d, lv_indev_data_t *data) {
520 auto *l = static_cast<LVEncoderListener *>(lv_indev_get_user_data(d));
521 data->state = l->pressed_ ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
522 data->key = l->key_;
523 data->enc_diff = (int16_t) (l->count_ - l->last_count_);
524 l->last_count_ = l->count_;
525 data->continue_reading = false;
526 });
527}
528#endif // USE_LVGL_KEY_LISTENER
529
530#if defined(USE_LVGL_DROPDOWN) || defined(LV_USE_ROLLER)
532 auto selected = this->get_selected_index();
533 if (selected >= this->options_.size())
534 return "";
535 return this->options_[selected];
536}
537
538static std::string join_string(std::vector<std::string> options) {
539 return std::accumulate(
540 options.begin(), options.end(), std::string(),
541 [](const std::string &a, const std::string &b) -> std::string { return a + (!a.empty() ? "\n" : "") + b; });
542}
543
544void LvSelectable::set_selected_text(const std::string &text, lv_anim_enable_t anim) {
545 auto index = std::find(this->options_.begin(), this->options_.end(), text);
546 if (index != this->options_.end()) {
547 this->set_selected_index(index - this->options_.begin(), anim);
548 lv_obj_send_event(this->obj, lv_api_event, nullptr);
549 }
550}
551
552void LvSelectable::set_options(std::vector<std::string> options) {
553 auto index = this->get_selected_index();
554 if (index >= options.size())
555 index = options.size() - 1;
556 this->options_ = std::move(options);
557 this->set_option_string(join_string(this->options_).c_str());
558 lv_obj_send_event(this->obj, LV_EVENT_REFRESH, nullptr);
559 this->set_selected_index(index, LV_ANIM_OFF);
560}
561#endif // USE_LVGL_DROPDOWN || LV_USE_ROLLER
562
563#ifdef USE_LVGL_BUTTONMATRIX
564void LvButtonMatrixType::set_obj(lv_obj_t *lv_obj) {
565 LvCompound::set_obj(lv_obj);
566 lv_obj_add_event_cb(
567 lv_obj,
568 [](lv_event_t *event) {
569 auto *self = static_cast<LvButtonMatrixType *>(lv_event_get_user_data(event));
570 if (self->key_callback_.size() == 0)
571 return;
572 auto key_idx = lv_buttonmatrix_get_selected_button(self->obj);
573 if (key_idx == LV_BUTTONMATRIX_BUTTON_NONE)
574 return;
575 if (self->key_map_.count(key_idx) != 0) {
576 self->send_key_(self->key_map_[key_idx]);
577 return;
578 }
579 const auto *str = lv_buttonmatrix_get_button_text(self->obj, key_idx);
580 auto len = strlen(str);
581 while (len--)
582 self->send_key_(*str++);
583 },
584 LV_EVENT_PRESSED, this);
585}
586#endif // USE_LVGL_BUTTONMATRIX
587
588#ifdef USE_LVGL_KEYBOARD
589static const char *const KB_SPECIAL_KEYS[] = {
590 "abc", "ABC", "1#",
591 // maybe add other special keys here
592};
593
594void LvKeyboardType::set_obj(lv_obj_t *lv_obj) {
595 LvCompound::set_obj(lv_obj);
596 lv_obj_add_event_cb(
597 lv_obj,
598 [](lv_event_t *event) {
599 auto *self = static_cast<LvKeyboardType *>(lv_event_get_user_data(event));
600 if (self->key_callback_.size() == 0)
601 return;
602
603 auto key_idx = lv_buttonmatrix_get_selected_button(self->obj);
604 if (key_idx == LV_BUTTONMATRIX_BUTTON_NONE)
605 return;
606 const char *txt = lv_buttonmatrix_get_button_text(self->obj, key_idx);
607 if (txt == nullptr)
608 return;
609 for (const auto *kb_special_key : KB_SPECIAL_KEYS) {
610 if (strcmp(txt, kb_special_key) == 0)
611 return;
612 }
613 while (*txt != 0)
614 self->send_key_(*txt++);
615 },
616 LV_EVENT_PRESSED, this);
617}
618#endif // USE_LVGL_KEYBOARD
619
621 if (this->draw_end_callback_ != nullptr)
623 if (this->update_when_display_idle_) {
624 for (auto *disp : this->displays_)
625 disp->update();
626 }
627}
628
630 if (this->paused_)
631 return true;
632 if (this->update_when_display_idle_) {
633 for (auto *disp : this->displays_) {
634 if (!disp->is_idle())
635 return true;
636 }
637 }
638 return false;
639}
640
642 int iterations = 6 - lv_display_get_inactive_time(this->disp_) / 60000;
643 if (iterations <= 0)
644 iterations = 1;
645 while (iterations-- != 0) {
646 int32_t col = random_uint32() % this->width_;
647 col = col / this->draw_rounding * this->draw_rounding;
648 int32_t row = random_uint32() % this->height_;
649 row = row / this->draw_rounding * this->draw_rounding;
650 // size will be between 8 and 32, and a multiple of draw_rounding
651 int32_t size = (random_uint32() % 25 + 8) / this->draw_rounding * this->draw_rounding;
652 lv_area_t area{col, row, col + size - 1, row + size - 1};
653 // clip to display bounds just in case
654 if (area.x2 >= this->width_)
655 area.x2 = this->width_ - 1;
656 if (area.y2 >= this->height_)
657 area.y2 = this->height_ - 1;
658
659 // line_len can't exceed 1024, and minimum buffer size is 2048, so this won't overflow the buffer
660 size_t line_len = lv_area_get_width(&area) * lv_area_get_height(&area) / 2;
661 for (size_t i = 0; i != line_len; i++) {
662 ((uint32_t *) (this->draw_buf_))[i] = random_uint32();
663 }
664 this->draw_buffer_(&area, (lv_color_data *) this->draw_buf_);
665 }
666}
667
689LvglComponent::LvglComponent(std::vector<display::Display *> displays, float buffer_frac, bool full_refresh,
690 int draw_rounding, bool resume_on_input, bool update_when_display_idle,
691 RotationType rotation_type)
692 : draw_rounding(draw_rounding),
693 displays_(std::move(displays)),
694 buffer_frac_(buffer_frac),
695 full_refresh_(full_refresh),
696 resume_on_input_(resume_on_input),
697 update_when_display_idle_(update_when_display_idle),
698 rotation_type_(rotation_type) {
699 this->disp_ = lv_display_create(240, 240);
700}
701
703 int32_t width = this->width_;
704 int32_t height = this->height_;
707 std::swap(width, height);
708 }
709 ESP_LOGD(TAG, "Setting resolution to %u x %u (rotation %d)", (unsigned) width, (unsigned) height,
710 (int) this->rotation_);
712 for (auto *display : this->displays_)
713 display->set_rotation(this->rotation_);
714 }
715 lv_display_set_resolution(this->disp_, width, height);
716}
718 auto *display = this->displays_[0];
719 auto rounding = this->draw_rounding;
720 this->width_ = display->get_native_width();
721 this->height_ = display->get_native_height();
722 // cater for displays with dimensions that don't divide by the required rounding
723 auto width = (this->width_ + rounding - 1) / rounding * rounding;
724 auto height = (this->height_ + rounding - 1) / rounding * rounding;
725 auto frac = this->buffer_frac_;
726 if (frac == 0)
727 frac = 1;
728 auto buf_bytes = clamp_at_least(width * height / frac * LV_COLOR_DEPTH / 8, MIN_BUFFER_SIZE);
729 void *buffer = nullptr;
730 // for small buffers, try to allocate in internal memory first to improve performance
731 if (this->buffer_frac_ >= MIN_BUFFER_FRAC / 2)
732 buffer = lv_alloc_draw_buf(buf_bytes, true); // NOLINT
733 if (buffer == nullptr)
734 buffer = lv_alloc_draw_buf(buf_bytes, false); // NOLINT
735 // if specific buffer size not set and can't get 100%, try for a smaller one
736 if (buffer == nullptr && this->buffer_frac_ == 0) {
737 frac = MIN_BUFFER_FRAC;
738 buf_bytes /= MIN_BUFFER_FRAC;
739 buffer = lv_alloc_draw_buf(buf_bytes, false); // NOLINT
740 }
741 this->buffer_frac_ = frac;
742 if (buffer == nullptr) {
743 this->status_set_error(LOG_STR("Memory allocation failure"));
744 this->mark_failed();
745 return;
746 }
747 this->draw_buf_ = static_cast<uint8_t *>(buffer);
748 this->set_resolution_();
749 lv_display_set_color_format(this->disp_, LV_COLOR_FORMAT_RGB565);
750 lv_display_set_flush_cb(this->disp_, static_flush_cb);
751 lv_display_set_user_data(this->disp_, this);
752 lv_display_add_event_cb(this->disp_, rounder_cb, LV_EVENT_INVALIDATE_AREA, this);
753 lv_display_set_buffers(this->disp_, this->draw_buf_, nullptr, buf_bytes,
754 this->full_refresh_ ? LV_DISPLAY_RENDER_MODE_FULL : LV_DISPLAY_RENDER_MODE_PARTIAL);
756 this->rotate_buf_ = static_cast<lv_color_t *>(lv_alloc_draw_buf(buf_bytes, false)); // NOLINT
757 if (this->rotate_buf_ == nullptr) {
758 this->status_set_error(LOG_STR("Memory allocation failure"));
759 this->mark_failed();
760 return;
761 }
762#ifdef USE_ESP32_VARIANT_ESP32P4
763 ppa_client_config_t ppa_config{};
764 ppa_config.oper_type = PPA_OPERATION_SRM;
765 ppa_config.max_pending_trans_num = 1;
766 if (ppa_register_client(&ppa_config, &this->ppa_client_) != ESP_OK) {
767 ESP_LOGW(TAG, "PPA client registration failed, using software rotation");
768 this->ppa_client_ = nullptr;
769 }
770#endif
771 }
772 if (this->draw_start_callback_ != nullptr) {
773 lv_display_add_event_cb(this->disp_, render_start_cb, LV_EVENT_RENDER_START, this);
774 }
775 if (this->draw_end_callback_ != nullptr || this->update_when_display_idle_) {
776 lv_display_add_event_cb(this->disp_, render_end_cb, LV_EVENT_REFR_READY, this);
777 }
778#if LV_USE_LOG
779 lv_log_register_print_cb([](lv_log_level_t level, const char *buf) {
780 auto next = strchr(buf, ')');
781 if (next != nullptr)
782 buf = next + 1;
783 while (isspace(*buf))
784 buf++;
785 if (level >= sizeof(LOG_LEVEL_MAP) / sizeof(LOG_LEVEL_MAP[0]))
786 level = sizeof(LOG_LEVEL_MAP) / sizeof(LOG_LEVEL_MAP[0]) - 1;
787 esp_log_printf_(LOG_LEVEL_MAP[level], TAG, 0, "%.*s", (int) strlen(buf) - 1, buf);
788 });
789#endif
790 this->show_page(0, LV_SCREEN_LOAD_ANIM_NONE, 0);
791 lv_display_trigger_activity(this->disp_);
792}
793
795 // update indicators
796 if (this->is_paused()) {
797 return;
798 }
799 this->idle_callbacks_.call(lv_display_get_inactive_time(this->disp_));
800}
801
803 if (this->is_paused()) {
804 if (this->paused_ && this->show_snow_)
805 this->write_random_();
806 } else {
807#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
808 auto now = millis();
809 lv_timer_handler();
810 auto elapsed = millis() - now;
811 if (elapsed > 15) {
812 ESP_LOGV(TAG, "lv_timer_handler took %dms", (int) (millis() - now));
813 }
814#else
815 lv_timer_handler();
816#endif
817 }
818}
819
820#ifdef USE_LVGL_ANIMIMG
821void lv_animimg_stop(lv_obj_t *obj) {
822 int32_t duration = lv_animimg_get_duration(obj);
823 lv_animimg_set_duration(obj, 0);
824 lv_animimg_start(obj);
825 lv_animimg_set_duration(obj, duration);
826}
827#endif
828void LvglComponent::static_flush_cb(lv_display_t *disp_drv, const lv_area_t *area, uint8_t *color_p) {
829 reinterpret_cast<LvglComponent *>(lv_display_get_user_data(disp_drv))->flush_cb_(disp_drv, area, color_p);
830}
831
832#ifdef USE_LVGL_SCALE
840void lv_scale_draw_event_cb(lv_event_t *e, int16_t range_start, int16_t range_end, lv_color_t color_start,
841 lv_color_t color_end, int width, bool local) {
842 auto *scale = static_cast<lv_obj_t *>(lv_event_get_target(e));
843 lv_draw_task_t *task = lv_event_get_draw_task(e);
844
845 if (lv_draw_task_get_type(task) == LV_DRAW_TASK_TYPE_LINE) {
846 auto *line_dsc = static_cast<lv_draw_line_dsc_t *>(lv_draw_task_get_draw_dsc(task));
847 int tick = line_dsc->base.id2;
848 if (tick >= range_start && tick <= range_end) {
849 int ratio;
850 if (local) {
851 int range = range_end - range_start;
852 tick -= range_start;
853 ratio = range == 0 ? 0 : (tick * 255) / range;
854 } else {
855 // total tick count is guaranteed to be at least 2.
856 ratio = (line_dsc->base.id1 * 255) / (lv_scale_get_total_tick_count(scale) - 1);
857 }
858 line_dsc->color = lv_color_mix(color_end, color_start, ratio);
859 line_dsc->width += width;
860 }
861 }
862}
863#endif // USE_LVGL_SCALE
864
865static void lv_container_constructor(const lv_obj_class_t *class_p, lv_obj_t *obj) {
866 LV_TRACE_OBJ_CREATE("begin");
867 LV_UNUSED(class_p);
868}
869
870// Container class. Name is based on LVGL naming convention but upper case to keep ESPHome clang-tidy happy
871const lv_obj_class_t LV_CONTAINER_CLASS = {
872 .base_class = &lv_obj_class,
873 .constructor_cb = lv_container_constructor,
874 .name = "lv_container",
875};
876
877lv_obj_t *lv_container_create(lv_obj_t *parent) {
878 lv_obj_t *obj = lv_obj_class_create_obj(&LV_CONTAINER_CLASS, parent);
879 lv_obj_class_init_obj(obj);
880 return obj;
881}
882} // namespace esphome::lvgl
883
884lv_result_t lv_mem_test_core() { return LV_RESULT_OK; }
885
886void lv_mem_init() {}
887
889
890#if defined(USE_HOST) || defined(USE_RP2040) || defined(USE_ESP8266)
891void *lv_malloc_core(size_t size) {
892 auto *ptr = malloc(size); // NOLINT
893 if (ptr == nullptr) {
894 ESP_LOGE(esphome::lvgl::TAG, "Failed to allocate %zu bytes", size);
895 }
896 return ptr;
897}
898void lv_free_core(void *ptr) { return free(ptr); } // NOLINT
899void *lv_realloc_core(void *ptr, size_t size) { return realloc(ptr, size); } // NOLINT
900
901void lv_mem_monitor_core(lv_mem_monitor_t *mon_p) { memset(mon_p, 0, sizeof(lv_mem_monitor_t)); }
902static void *lv_alloc_draw_buf(size_t size, bool internal) {
903 return malloc(size); // NOLINT
904}
905
906#elif defined(USE_ESP32)
907static unsigned cap_bits = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT; // NOLINT
908
909static void *lv_alloc_draw_buf(size_t size, bool internal) {
910 void *buffer;
911 size = LV_ROUND_UP(size, LV_DRAW_BUF_ALIGN);
912 buffer = heap_caps_aligned_alloc(LV_DRAW_BUF_ALIGN, size, internal ? MALLOC_CAP_8BIT : cap_bits); // NOLINT
913 if (buffer == nullptr)
914 ESP_LOGW(esphome::lvgl::TAG, "Failed to allocate %zu bytes for %sdraw buffer", size, internal ? "internal " : "");
915 return buffer;
916}
917
918void lv_mem_monitor_core(lv_mem_monitor_t *mon_p) {
919 multi_heap_info_t heap_info;
920 heap_caps_get_info(&heap_info, cap_bits);
921 mon_p->total_size = heap_info.total_allocated_bytes + heap_info.total_free_bytes;
922 mon_p->free_size = heap_info.total_free_bytes;
923 mon_p->max_used = heap_info.total_allocated_bytes;
924 mon_p->free_biggest_size = heap_info.largest_free_block;
925 mon_p->used_cnt = heap_info.allocated_blocks;
926 mon_p->free_cnt = heap_info.free_blocks;
927 mon_p->used_pct = heap_info.allocated_blocks * 100 / (heap_info.allocated_blocks + heap_info.free_blocks);
928 mon_p->frag_pct = 0;
929}
930
931void *lv_malloc_core(size_t size) {
932 void *ptr;
933 ptr = heap_caps_malloc(size, cap_bits);
934 if (ptr == nullptr) {
935 cap_bits = MALLOC_CAP_8BIT;
936 ptr = heap_caps_malloc(size, cap_bits);
937 }
938 if (ptr == nullptr) {
939 ESP_LOGE(esphome::lvgl::TAG, "Failed to allocate %zu bytes", size);
940 return nullptr;
941 }
942 ESP_LOGV(esphome::lvgl::TAG, "allocate %zu - > %p", size, ptr);
943 return ptr;
944}
945
946void lv_free_core(void *ptr) {
947 ESP_LOGV(esphome::lvgl::TAG, "free %p", ptr);
948 if (ptr == nullptr)
949 return;
950 heap_caps_free(ptr);
951}
952
953void *lv_realloc_core(void *ptr, size_t size) {
954 ESP_LOGV(esphome::lvgl::TAG, "realloc %p: %zu", ptr, size);
955 return heap_caps_realloc(ptr, size, cap_bits);
956}
957#endif
uint8_t l
Definition bl0906.h:0
void mark_failed()
Mark this component as failed.
bool is_ready() const
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:2021
Function-pointer-only templatable storage (4 bytes on 32-bit).
Definition automation.h:40
T value(X... x) const
Definition automation.h:79
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:482
TemplatableFn< uint32_t > timeout_
IdleTrigger(LvglComponent *parent, TemplatableFn< uint32_t > timeout)
void set_obj(lv_obj_t *lv_obj) override
LVEncoderListener(lv_indev_type_t type, uint16_t long_press_time, uint16_t long_press_repeat_time)
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
void set_obj(lv_obj_t *lv_obj) override
virtual void set_obj(lv_obj_t *lv_obj)
void set_obj(lv_obj_t *lv_obj) override
void setup(size_t index)
void set_selected_text(const std::string &text, lv_anim_enable_t anim)
void set_options(std::vector< std::string > options)
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)
display::DisplayRotation rotation_
void rotate_coordinates(int32_t &x, int32_t &y) const
std::vector< LvPageType * > pages_
void set_rotation(display::DisplayRotation rotation)
static void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event)
bool ppa_rotate_(const lv_color_data *src, lv_color_data *dst, uint16_t width, uint16_t height, uint32_t height_rounded)
CallbackManager< void(uint32_t)> idle_callbacks_
void show_next_page(lv_screen_load_anim_t anim, uint32_t time)
std::vector< display::Display * > displays_
static void esphome_lvgl_init()
Initialize the LVGL library and register custom events.
void show_prev_page(lv_screen_load_anim_t anim, uint32_t time)
ppa_client_handle_t ppa_client_
LvglComponent(std::vector< display::Display * > displays, float buffer_frac, bool full_refresh, int draw_rounding, bool resume_on_input, bool update_when_display_idle, RotationType rotation_type)
static void render_start_cb(lv_event_t *event)
void add_on_idle_callback(F &&callback)
void add_page(LvPageType *page)
static void static_flush_cb(lv_display_t *disp_drv, const lv_area_t *area, uint8_t *color_p)
static void render_end_cb(lv_event_t *event)
void draw_buffer_(const lv_area_t *area, lv_color_data *ptr)
void show_page(size_t index, lv_screen_load_anim_t anim, uint32_t time)
void flush_cb_(lv_display_t *disp_drv, const lv_area_t *area, uint8_t *color_p)
uint16_t type
uint8_t options
void lv_mem_deinit()
void lv_mem_init()
void lv_free_core(void *ptr)
void * lv_malloc_core(size_t size)
void lv_mem_monitor_core(lv_mem_monitor_t *mon_p)
void * lv_realloc_core(void *ptr, size_t size)
lv_result_t lv_mem_test_core()
Range range
Definition msa3xx.h:0
uint8_t duration
Definition msa3xx.h:0
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:138
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:137
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:136
int16_t lv_get_needle_angle_for_value(lv_obj_t *obj, int value)
void lv_animimg_stop(lv_obj_t *obj)
uint16_t lv_color_data
void(lv_event_t *) event_callback_t
const lv_obj_class_t LV_CONTAINER_CLASS
lv_event_code_t lv_update_event
lv_obj_t * lv_container_create(lv_obj_t *parent)
std::string lv_event_code_name_for(lv_event_t *event)
void lv_scale_draw_event_cb(lv_event_t *e, int16_t range_start, int16_t range_end, lv_color_t color_start, lv_color_t color_end, int width, bool local)
Function to apply colors to ticks based on position.
lv_event_code_t lv_api_event
std::vector< TouchPoint > TouchPoints_t
Definition touchscreen.h:30
T clamp_at_most(T value, U max)
Definition helpers.h:2330
void HOT esp_log_printf_(int level, const char *tag, int line, const char *format,...)
Definition log.cpp:21
std::string size_t len
Definition helpers.h:1045
uint16_t size
Definition helpers.cpp:25
T clamp_at_least(T value, U min)
Definition helpers.h:2325
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:12
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6