ESPHome  2024.11.1
lvgl_select.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 
9 #include "../lvgl.h"
10 
11 namespace esphome {
12 namespace lvgl {
13 
14 class LVGLSelect : public select::Select {
15  public:
16  void set_widget(LvSelectable *widget, lv_anim_enable_t anim = LV_ANIM_OFF) {
17  this->widget_ = widget;
18  this->anim_ = anim;
19  this->set_options_();
20  lv_obj_add_event_cb(
21  this->widget_->obj,
22  [](lv_event_t *e) {
23  auto *it = static_cast<LVGLSelect *>(e->user_data);
24  it->set_options_();
25  },
26  LV_EVENT_REFRESH, this);
27  if (this->initial_state_.has_value()) {
28  this->control(this->initial_state_.value());
29  this->initial_state_.reset();
30  }
31  this->publish();
32  auto lamb = [](lv_event_t *e) {
33  auto *self = static_cast<LVGLSelect *>(e->user_data);
34  self->publish();
35  };
36  lv_obj_add_event_cb(this->widget_->obj, lamb, LV_EVENT_VALUE_CHANGED, this);
37  lv_obj_add_event_cb(this->widget_->obj, lamb, lv_update_event, this);
38  }
39 
40  void publish() { this->publish_state(this->widget_->get_selected_text()); }
41 
42  protected:
43  void control(const std::string &value) override {
44  if (this->widget_ != nullptr) {
45  this->widget_->set_selected_text(value, this->anim_);
46  } else {
47  this->initial_state_ = value;
48  }
49  }
50  void set_options_() { this->traits.set_options(this->widget_->get_options()); }
51 
54  lv_anim_enable_t anim_{LV_ANIM_OFF};
55 };
56 
57 } // namespace lvgl
58 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
LvSelectable * widget_
Definition: lvgl_select.h:52
lv_anim_enable_t anim_
Definition: lvgl_select.h:54
SelectTraits traits
Definition: select.h:34
bool has_value() const
Definition: optional.h:87
void set_selected_text(const std::string &text, lv_anim_enable_t anim)
lv_event_code_t lv_update_event
void set_options(std::vector< std::string > options)
void set_widget(LvSelectable *widget, lv_anim_enable_t anim=LV_ANIM_OFF)
Definition: lvgl_select.h:16
void publish_state(const std::string &state)
Definition: select.cpp:9
optional< std::string > initial_state_
Definition: lvgl_select.h:53
Base-class for all selects.
Definition: select.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< std::string > get_options()
Definition: lvgl_esphome.h:306
void control(const std::string &value) override
Definition: lvgl_select.h:43