ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
lvgl_select.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
9#include "../lvgl.h"
10
11namespace esphome {
12namespace lvgl {
13
14class LVGLSelect : public select::Select, public Component {
15 public:
16 LVGLSelect(LvSelectable *widget, lv_anim_enable_t anim, bool restore)
17 : widget_(widget), anim_(anim), restore_(restore) {}
18
19 void setup() override {
20 this->set_options_();
21 if (this->restore_) {
22 size_t index;
24 if (this->pref_.load(&index))
25 this->widget_->set_selected_index(index, LV_ANIM_OFF);
26 }
27 this->publish();
28 lv_obj_add_event_cb(
29 this->widget_->obj,
30 [](lv_event_t *e) {
31 auto *it = static_cast<LVGLSelect *>(e->user_data);
32 it->set_options_();
33 },
34 LV_EVENT_REFRESH, this);
35 auto lamb = [](lv_event_t *e) {
36 auto *self = static_cast<LVGLSelect *>(e->user_data);
37 self->publish();
38 };
39 lv_obj_add_event_cb(this->widget_->obj, lamb, LV_EVENT_VALUE_CHANGED, this);
40 lv_obj_add_event_cb(this->widget_->obj, lamb, lv_update_event, this);
41 }
42
43 void publish() {
44 auto index = this->widget_->get_selected_index();
45 this->publish_state(index);
46 if (this->restore_) {
47 this->pref_.save(&index);
48 }
49 }
50
51 protected:
52 void control(size_t index) override {
53 this->widget_->set_selected_index(index, this->anim_);
54 this->publish();
55 }
56 void set_options_() {
57 // Widget uses std::vector<std::string>, SelectTraits uses FixedVector<const char*>
58 // Convert by extracting c_str() pointers
59 const auto &opts = this->widget_->get_options();
61 opt_ptrs.init(opts.size());
62 for (const auto &opt : opts) {
63 opt_ptrs.push_back(opt.c_str());
64 }
65 this->traits.set_options(opt_ptrs);
66 }
67
69 lv_anim_enable_t anim_;
72};
73
74} // namespace lvgl
75} // namespace esphome
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:184
void push_back(const T &value)
Add element without bounds checking Caller must ensure sufficient capacity was allocated via init() S...
Definition helpers.h:298
void init(size_t n)
Definition helpers.h:271
ESPPreferenceObject pref_
Definition lvgl_select.h:71
void control(size_t index) override
Definition lvgl_select.h:52
void setup() override
Definition lvgl_select.h:19
LVGLSelect(LvSelectable *widget, lv_anim_enable_t anim, bool restore)
Definition lvgl_select.h:16
lv_anim_enable_t anim_
Definition lvgl_select.h:69
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:31
SelectTraits traits
Definition select.h:33
void publish_state(const std::string &state)
Definition select.cpp:12
void set_options(const std::initializer_list< const char * > &options)
lv_event_code_t lv_update_event
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
ESPPreferences * global_preferences