ESPHome  2024.12.2
lvgl_esphome.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
3 
4 #ifdef USE_BINARY_SENSOR
6 #endif // USE_BINARY_SENSOR
7 #ifdef USE_LVGL_IMAGE
9 #endif // USE_LVGL_IMAGE
10 #ifdef USE_LVGL_ROTARY_ENCODER
12 #endif // USE_LVGL_ROTARY_ENCODER
13 
14 // required for clang-tidy
15 #ifndef LV_CONF_H
16 #define LV_CONF_SKIP 1 // NOLINT
17 #endif // LV_CONF_H
18 
21 #include "esphome/core/component.h"
22 #include "esphome/core/log.h"
23 #include <lvgl.h>
24 #include <map>
25 #include <utility>
26 #include <vector>
27 
28 #ifdef USE_LVGL_FONT
30 #endif // USE_LVGL_FONT
31 #ifdef USE_LVGL_TOUCHSCREEN
33 #endif // USE_LVGL_TOUCHSCREEN
34 
35 #if defined(USE_LVGL_BUTTONMATRIX) || defined(USE_LVGL_KEYBOARD)
37 #endif // USE_LVGL_BUTTONMATRIX
38 
39 namespace esphome {
40 namespace lvgl {
41 
42 extern lv_event_code_t lv_api_event; // NOLINT
43 extern lv_event_code_t lv_update_event; // NOLINT
44 extern std::string lv_event_code_name_for(uint8_t event_code);
45 #if LV_COLOR_DEPTH == 16
47 #elif LV_COLOR_DEPTH == 32
49 #else // LV_COLOR_DEPTH
51 #endif // LV_COLOR_DEPTH
52 
53 #ifdef USE_LVGL_IMAGE
54 // Shortcut / overload, so that the source of an image can easily be updated
55 // from within a lambda.
56 inline void lv_img_set_src(lv_obj_t *obj, esphome::image::Image *image) {
57  lv_img_set_src(obj, image->get_lv_img_dsc());
58 }
59 inline void lv_disp_set_bg_image(lv_disp_t *disp, esphome::image::Image *image) {
60  lv_disp_set_bg_image(disp, image->get_lv_img_dsc());
61 }
62 #endif // USE_LVGL_IMAGE
63 #ifdef USE_LVGL_ANIMIMG
64 inline void lv_animimg_set_src(lv_obj_t *img, std::vector<image::Image *> images) {
65  auto *dsc = static_cast<std::vector<lv_img_dsc_t *> *>(lv_obj_get_user_data(img));
66  if (dsc == nullptr) {
67  // object will be lazily allocated but never freed.
68  dsc = new std::vector<lv_img_dsc_t *>(images.size()); // NOLINT
69  lv_obj_set_user_data(img, dsc);
70  }
71  dsc->clear();
72  for (auto &image : images) {
73  dsc->push_back(image->get_lv_img_dsc());
74  }
75  lv_animimg_set_src(img, (const void **) dsc->data(), dsc->size());
76 }
77 
78 #endif // USE_LVGL_ANIMIMG
79 
80 // Parent class for things that wrap an LVGL object
81 class LvCompound {
82  public:
83  virtual void set_obj(lv_obj_t *lv_obj) { this->obj = lv_obj; }
84  lv_obj_t *obj{};
85 };
86 
87 class LvPageType {
88  public:
89  LvPageType(bool skip) : skip(skip) {}
90 
91  void setup(size_t index) {
92  this->index = index;
93  this->obj = lv_obj_create(nullptr);
94  }
95  lv_obj_t *obj{};
96  size_t index{};
97  bool skip;
98 };
99 
100 using LvLambdaType = std::function<void(lv_obj_t *)>;
101 using set_value_lambda_t = std::function<void(float)>;
102 using event_callback_t = void(_lv_event_t *);
103 using text_lambda_t = std::function<const char *()>;
104 
105 template<typename... Ts> class ObjUpdateAction : public Action<Ts...> {
106  public:
107  explicit ObjUpdateAction(std::function<void(Ts...)> &&lamb) : lamb_(std::move(lamb)) {}
108 
109  void play(Ts... x) override { this->lamb_(x...); }
110 
111  protected:
112  std::function<void(Ts...)> lamb_;
113 };
114 #ifdef USE_LVGL_FONT
115 class FontEngine {
116  public:
117  FontEngine(font::Font *esp_font);
118  const lv_font_t *get_lv_font();
119 
120  const font::GlyphData *get_glyph_data(uint32_t unicode_letter);
121  uint16_t baseline{};
122  uint16_t height{};
123  uint8_t bpp{};
124 
125  protected:
126  font::Font *font_{};
127  uint32_t last_letter_{};
128  const font::GlyphData *last_data_{};
129  lv_font_t lv_font_{};
130 };
131 #endif // USE_LVGL_FONT
132 #ifdef USE_LVGL_ANIMIMG
133 void lv_animimg_stop(lv_obj_t *obj);
134 #endif // USE_LVGL_ANIMIMG
135 
137  constexpr static const char *const TAG = "lvgl";
138 
139  public:
140  LvglComponent(std::vector<display::Display *> displays, float buffer_frac, bool full_refresh, int draw_rounding,
141  bool resume_on_input);
142  static void static_flush_cb(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p);
143 
144  float get_setup_priority() const override { return setup_priority::PROCESSOR; }
145  void setup() override;
146  void update() override;
147  void loop() override;
148  void add_on_idle_callback(std::function<void(uint32_t)> &&callback) {
149  this->idle_callbacks_.add(std::move(callback));
150  }
151  void add_on_pause_callback(std::function<void(bool)> &&callback) { this->pause_callbacks_.add(std::move(callback)); }
152  void dump_config() override;
153  bool is_idle(uint32_t idle_ms) { return lv_disp_get_inactive_time(this->disp_) > idle_ms; }
154  lv_disp_t *get_disp() { return this->disp_; }
155  lv_obj_t *get_scr_act() { return lv_disp_get_scr_act(this->disp_); }
156  // Pause or resume the display.
157  // @param paused If true, pause the display. If false, resume the display.
158  // @param show_snow If true, show the snow effect when paused.
159  void set_paused(bool paused, bool show_snow);
160  bool is_paused() const { return this->paused_; }
161  // If the display is paused and we have resume_on_input_ set to true, resume the display.
162  void maybe_wakeup() {
163  if (this->paused_ && this->resume_on_input_) {
164  this->set_paused(false, false);
165  }
166  }
167 
171  static void esphome_lvgl_init();
172  static void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event);
173  static void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1, lv_event_code_t event2);
174  static void add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_event_code_t event1, lv_event_code_t event2,
175  lv_event_code_t event3);
176  void add_page(LvPageType *page);
177  void show_page(size_t index, lv_scr_load_anim_t anim, uint32_t time);
178  void show_next_page(lv_scr_load_anim_t anim, uint32_t time);
179  void show_prev_page(lv_scr_load_anim_t anim, uint32_t time);
180  void set_page_wrap(bool wrap) { this->page_wrap_ = wrap; }
181  void set_focus_mark(lv_group_t *group) { this->focus_marks_[group] = lv_group_get_focused(group); }
182  void restore_focus_mark(lv_group_t *group) {
183  auto *mark = this->focus_marks_[group];
184  if (mark != nullptr) {
185  lv_group_focus_obj(mark);
186  }
187  }
188  // rounding factor to align bounds of update area when drawing
189  size_t draw_rounding{2};
190 
192 
193  protected:
194  void write_random_();
195  void draw_buffer_(const lv_area_t *area, lv_color_t *ptr);
196  void flush_cb_(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p);
197 
198  std::vector<display::Display *> displays_{};
199  size_t buffer_frac_{1};
200  bool full_refresh_{};
201  bool resume_on_input_{};
202 
203  lv_disp_draw_buf_t draw_buf_{};
204  lv_disp_drv_t disp_drv_{};
205  lv_disp_t *disp_{};
206  bool paused_{};
207  std::vector<LvPageType *> pages_{};
208  size_t current_page_{0};
209  bool show_snow_{};
210  bool page_wrap_{true};
211  std::map<lv_group_t *, lv_obj_t *> focus_marks_{};
212 
214  CallbackManager<void(bool)> pause_callbacks_{};
215  lv_color_t *rotate_buf_{};
216 };
217 
218 class IdleTrigger : public Trigger<> {
219  public:
220  explicit IdleTrigger(LvglComponent *parent, TemplatableValue<uint32_t> timeout);
221 
222  protected:
224  bool is_idle_{};
225 };
226 
227 class PauseTrigger : public Trigger<> {
228  public:
229  explicit PauseTrigger(LvglComponent *parent, TemplatableValue<bool> paused);
230 
231  protected:
233 };
234 
235 template<typename... Ts> class LvglAction : public Action<Ts...>, public Parented<LvglComponent> {
236  public:
237  explicit LvglAction(std::function<void(LvglComponent *)> &&lamb) : action_(std::move(lamb)) {}
238  void play(Ts... x) override { this->action_(this->parent_); }
239 
240  protected:
241  std::function<void(LvglComponent *)> action_{};
242 };
243 
244 template<typename... Ts> class LvglCondition : public Condition<Ts...>, public Parented<LvglComponent> {
245  public:
246  LvglCondition(std::function<bool(LvglComponent *)> &&condition_lambda)
247  : condition_lambda_(std::move(condition_lambda)) {}
248  bool check(Ts... x) override { return this->condition_lambda_(this->parent_); }
249 
250  protected:
251  std::function<bool(LvglComponent *)> condition_lambda_{};
252 };
253 
254 #ifdef USE_LVGL_TOUCHSCREEN
255 class LVTouchListener : public touchscreen::TouchListener, public Parented<LvglComponent> {
256  public:
257  LVTouchListener(uint16_t long_press_time, uint16_t long_press_repeat_time, LvglComponent *parent);
258  void update(const touchscreen::TouchPoints_t &tpoints) override;
259  void release() override {
260  touch_pressed_ = false;
261  this->parent_->maybe_wakeup();
262  }
263  lv_indev_drv_t *get_drv() { return &this->drv_; }
264 
265  protected:
266  lv_indev_drv_t drv_{};
267  touchscreen::TouchPoint touch_point_{};
268  bool touch_pressed_{};
269 };
270 #endif // USE_LVGL_TOUCHSCREEN
271 
272 #ifdef USE_LVGL_KEY_LISTENER
273 class LVEncoderListener : public Parented<LvglComponent> {
274  public:
275  LVEncoderListener(lv_indev_type_t type, uint16_t lpt, uint16_t lprt);
276 
277 #ifdef USE_BINARY_SENSOR
278  void add_button(binary_sensor::BinarySensor *button, lv_key_t key) {
279  button->add_on_state_callback([this, key](bool state) { this->event(key, state); });
280  }
281 #endif
282 
283 #ifdef USE_LVGL_ROTARY_ENCODER
285  sensor->register_listener([this](int32_t count) { this->set_count(count); });
286  }
287 #endif // USE_LVGL_ROTARY_ENCODER
288 
289  void event(int key, bool pressed) {
290  if (!this->parent_->is_paused()) {
291  this->pressed_ = pressed;
292  this->key_ = key;
293  } else if (!pressed) {
294  // maybe wakeup on release if paused
295  this->parent_->maybe_wakeup();
296  }
297  }
298 
299  void set_count(int32_t count) {
300  if (!this->parent_->is_paused()) {
301  this->count_ = count;
302  } else {
303  this->parent_->maybe_wakeup();
304  }
305  }
306 
307  lv_indev_drv_t *get_drv() { return &this->drv_; }
308 
309  protected:
310  lv_indev_drv_t drv_{};
311  bool pressed_{};
312  int32_t count_{};
313  int32_t last_count_{};
314  int key_{};
315 };
316 #endif // USE_LVGL_KEY_LISTENER
317 
318 #if defined(USE_LVGL_DROPDOWN) || defined(LV_USE_ROLLER)
319 class LvSelectable : public LvCompound {
320  public:
321  virtual size_t get_selected_index() = 0;
322  virtual void set_selected_index(size_t index, lv_anim_enable_t anim) = 0;
323  void set_selected_text(const std::string &text, lv_anim_enable_t anim);
324  std::string get_selected_text();
325  std::vector<std::string> get_options() { return this->options_; }
326  void set_options(std::vector<std::string> options);
327 
328  protected:
329  virtual void set_option_string(const char *options) = 0;
330  std::vector<std::string> options_{};
331 };
332 
333 #ifdef USE_LVGL_DROPDOWN
334 class LvDropdownType : public LvSelectable {
335  public:
336  size_t get_selected_index() override { return lv_dropdown_get_selected(this->obj); }
337  void set_selected_index(size_t index, lv_anim_enable_t anim) override { lv_dropdown_set_selected(this->obj, index); }
338 
339  protected:
340  void set_option_string(const char *options) override { lv_dropdown_set_options(this->obj, options); }
341 };
342 #endif // USE_LVGL_DROPDOWN
343 
344 #ifdef USE_LVGL_ROLLER
345 class LvRollerType : public LvSelectable {
346  public:
347  size_t get_selected_index() override { return lv_roller_get_selected(this->obj); }
348  void set_selected_index(size_t index, lv_anim_enable_t anim) override {
349  lv_roller_set_selected(this->obj, index, anim);
350  }
351  void set_mode(lv_roller_mode_t mode) { this->mode_ = mode; }
352 
353  protected:
354  void set_option_string(const char *options) override { lv_roller_set_options(this->obj, options, this->mode_); }
355  lv_roller_mode_t mode_{LV_ROLLER_MODE_NORMAL};
356 };
357 #endif
358 #endif // defined(USE_LVGL_DROPDOWN) || defined(LV_USE_ROLLER)
359 
360 #ifdef USE_LVGL_BUTTONMATRIX
362  public:
363  void set_obj(lv_obj_t *lv_obj) override;
364  uint16_t get_selected() { return lv_btnmatrix_get_selected_btn(this->obj); }
365  void set_key(size_t idx, uint8_t key) { this->key_map_[idx] = key; }
366 
367  protected:
368  std::map<size_t, uint8_t> key_map_{};
369 };
370 #endif // USE_LVGL_BUTTONMATRIX
371 
372 #ifdef USE_LVGL_KEYBOARD
374  public:
375  void set_obj(lv_obj_t *lv_obj) override;
376 };
377 #endif // USE_LVGL_KEYBOARD
378 } // namespace lvgl
379 } // namespace esphome
void setup()
std::string lv_event_code_name_for(uint8_t event_code)
void add_on_idle_callback(std::function< void(uint32_t)> &&callback)
Definition: lvgl_esphome.h:148
void set_sensor(rotary_encoder::RotaryEncoderSensor *sensor)
Definition: lvgl_esphome.h:284
void loop()
void lv_img_set_src(lv_obj_t *obj, esphome::image::Image *image)
Definition: lvgl_esphome.h:56
LvglCondition(std::function< bool(LvglComponent *)> &&condition_lambda)
Definition: lvgl_esphome.h:246
bool check(Ts... x) override
Definition: lvgl_esphome.h:248
std::vector< TouchPoint > TouchPoints_t
Definition: touchscreen.h:30
bool is_idle(uint32_t idle_ms)
Definition: lvgl_esphome.h:153
void register_listener(std::function< void(uint32_t)> listener)
void set_selected_index(size_t index, lv_anim_enable_t anim) override
Definition: lvgl_esphome.h:348
interface for components that provide keypresses
Definition: key_provider.h:10
MediaPlayerStateTrigger< MediaPlayerState::MEDIA_PLAYER_STATE_IDLE > IdleTrigger
Definition: automation.h:56
float get_setup_priority() const override
Definition: lvgl_esphome.h:144
void set_option_string(const char *options) override
Definition: lvgl_esphome.h:340
void lv_disp_set_bg_image(lv_disp_t *disp, esphome::image::Image *image)
Definition: lvgl_esphome.h:59
lv_indev_drv_t * get_drv()
Definition: lvgl_esphome.h:263
uint16_t x
Definition: tt21100.cpp:17
void play(Ts... x) override
Definition: lvgl_esphome.h:238
STL namespace.
Component for rendering LVGL.
Definition: lvgl_esphome.h:136
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_mode(lv_roller_mode_t mode)
Definition: lvgl_esphome.h:351
void add_button(binary_sensor::BinarySensor *button, lv_key_t key)
Definition: lvgl_esphome.h:278
void(_lv_event_t *) event_callback_t
Definition: lvgl_esphome.h:102
TemplatableValue< bool > paused_
Definition: lvgl_esphome.h:232
lv_event_code_t lv_update_event
std::function< void(float)> set_value_lambda_t
Definition: lvgl_esphome.h:101
lv_event_code_t lv_api_event
void restore_focus_mark(lv_group_t *group)
Definition: lvgl_esphome.h:182
void setup(size_t index)
Definition: lvgl_esphome.h:91
virtual void set_obj(lv_obj_t *lv_obj)
Definition: lvgl_esphome.h:83
Base class for all automation conditions.
Definition: automation.h:74
void add_on_pause_callback(std::function< void(bool)> &&callback)
Definition: lvgl_esphome.h:151
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
void set_page_wrap(bool wrap)
Definition: lvgl_esphome.h:180
std::function< void(Ts...)> lamb_
Definition: lvgl_esphome.h:112
MediaPlayerStateTrigger< MediaPlayerState::MEDIA_PLAYER_STATE_PAUSED > PauseTrigger
Definition: automation.h:58
LvglAction(std::function< void(LvglComponent *)> &&lamb)
Definition: lvgl_esphome.h:237
uint8_t type
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:20
void lv_animimg_set_src(lv_obj_t *img, std::vector< image::Image *> images)
Definition: lvgl_esphome.h:64
uint8_t options
size_t get_selected_index() override
Definition: lvgl_esphome.h:347
TemplatableValue< uint32_t > timeout_
Definition: lvgl_esphome.h:223
lv_img_dsc_t * get_lv_img_dsc()
Definition: image.cpp:83
std::function< void(lv_obj_t *)> LvLambdaType
Definition: lvgl_esphome.h:100
void set_key(size_t idx, uint8_t key)
Definition: lvgl_esphome.h:365
void event(int key, bool pressed)
Definition: lvgl_esphome.h:289
std::function< const char *()> text_lambda_t
Definition: lvgl_esphome.h:103
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
void set_focus_mark(lv_group_t *group)
Definition: lvgl_esphome.h:181
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
std::vector< std::string > get_options()
Definition: lvgl_esphome.h:325
void set_count(int32_t count)
Definition: lvgl_esphome.h:299
void lv_animimg_stop(lv_obj_t *obj)
size_t get_selected_index() override
Definition: lvgl_esphome.h:336
ObjUpdateAction(std::function< void(Ts...)> &&lamb)
Definition: lvgl_esphome.h:107
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
Helper class to easily give an object a parent of type T.
Definition: helpers.h:530
void play(Ts... x) override
Definition: lvgl_esphome.h:109
bool state
Definition: fan.h:34
void set_selected_index(size_t index, lv_anim_enable_t anim) override
Definition: lvgl_esphome.h:337
void set_option_string(const char *options) override
Definition: lvgl_esphome.h:354