ESPHome 2026.5.1
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 : 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 this->publish();
54 }
55 void set_options_() {
56 // Widget uses std::vector<std::string>, SelectTraits uses FixedVector<const char*>
57 // Convert by extracting c_str() pointers
58 const auto &opts = this->widget_->get_options();
60 opt_ptrs.init(opts.size());
61 for (const auto &opt : opts) {
62 opt_ptrs.push_back(opt.c_str());
63 }
64 this->traits.set_options(opt_ptrs);
65 }
66
68 lv_anim_enable_t anim_;
71};
72
73} // namespace esphome::lvgl
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:529
void push_back(const T &value)
Add element without bounds checking Caller must ensure sufficient capacity was allocated via init() S...
Definition helpers.h:646
void init(size_t n)
Definition helpers.h:619
ESPPreferenceObject pref_
Definition lvgl_select.h:70
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:68
const std::vector< std::string > & 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.cpp:11
void set_options(const std::initializer_list< const char * > &options)
lv_event_code_t lv_update_event