ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
lvgl_select.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
10
11namespace esphome::lvgl {
12
13class LVGLSelect final : public select::Select, public Component {
14 public:
15 LVGLSelect(LvSelectable *widget, lv_anim_enable_t anim, bool restore)
16 : widget_(widget), anim_(anim), restore_(restore) {}
17
18 void setup() override {
19 this->set_options_();
20 if (this->restore_) {
21 size_t index;
23 if (this->pref_.load(&index))
24 this->widget_->set_selected_index(index, LV_ANIM_OFF);
25 }
26 this->publish();
27 lv_obj_add_event_cb(
28 this->widget_->obj,
29 [](lv_event_t *e) {
30 auto *it = static_cast<LVGLSelect *>(lv_event_get_user_data(e));
31 it->set_options_();
32 },
33 LV_EVENT_REFRESH, this);
34 auto lamb = [](lv_event_t *e) {
35 auto *self = static_cast<LVGLSelect *>(lv_event_get_user_data(e));
36 self->publish();
37 };
38 lv_obj_add_event_cb(this->widget_->obj, lamb, LV_EVENT_VALUE_CHANGED, this);
39 lv_obj_add_event_cb(this->widget_->obj, lamb, lv_update_event, this);
40 }
41
42 void publish() {
43 auto index = this->widget_->get_selected_index();
44 this->publish_state(index);
45 if (this->restore_) {
46 this->pref_.save(&index);
47 }
48 }
49
50 protected:
51 void control(size_t index) override {
52 this->widget_->set_selected_index(index, this->anim_);
53 // The update event fires the widget's on_value/on_update triggers
54 lv_obj_send_event(this->widget_->obj, lv_update_event, nullptr);
55 }
57
59 lv_anim_enable_t anim_;
62};
63
64} // namespace esphome::lvgl
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
ESPPreferenceObject pref_
Definition lvgl_select.h:61
void control(size_t index) override
Definition lvgl_select.h:51
void setup() override
Definition lvgl_select.h:18
LVGLSelect(LvSelectable *widget, lv_anim_enable_t anim, bool restore)
Definition lvgl_select.h:15
lv_anim_enable_t anim_
Definition lvgl_select.h:59
const FixedVector< const char * > & get_options()
virtual void set_selected_index(size_t index, lv_anim_enable_t anim)=0
virtual size_t get_selected_index()=0
Base-class for all selects.
Definition select.h:29
SelectTraits traits
Definition select.h:31
void publish_state(const std::string &state)
Definition select.h:36
void set_options(const std::initializer_list< const char * > &options)
lv_event_code_t lv_update_event