ESPHome 2025.5.0
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() {
45 if (this->restore_) {
46 auto index = this->widget_->get_selected_index();
47 this->pref_.save(&index);
48 }
49 }
50
51 protected:
52 void control(const std::string &value) override {
53 this->widget_->set_selected_text(value, this->anim_);
54 this->publish();
55 }
57
59 lv_anim_enable_t anim_;
62};
63
64} // namespace lvgl
65} // 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_object_id_hash()
ESPPreferenceObject pref_
Definition lvgl_select.h:61
void control(const std::string &value) 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:59
std::vector< std::string > get_options()
void set_selected_text(const std::string &text, lv_anim_enable_t anim)
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:34
void publish_state(const std::string &state)
Definition select.cpp:9
void set_options(std::vector< std::string > 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