ESPHome  2023.5.5
menu_item.cpp
Go to the documentation of this file.
1 #include "menu_item.h"
2 
3 #include <cstdio>
4 
5 namespace esphome {
6 namespace display_menu_base {
7 
8 void MenuItem::on_enter() { this->on_enter_callbacks_.call(); }
9 
10 void MenuItem::on_leave() { this->on_leave_callbacks_.call(); }
11 
12 void MenuItem::on_value_() { this->on_value_callbacks_.call(); }
13 
14 #ifdef USE_SELECT
15 std::string MenuItemSelect::get_value_text() const {
16  std::string result;
17 
18  if (this->value_getter_.has_value()) {
19  result = this->value_getter_.value()(this);
20  } else {
21  if (this->select_var_ != nullptr) {
22  result = this->select_var_->state;
23  }
24  }
25 
26  return result;
27 }
28 
30  bool changed = false;
31 
32  if (this->select_var_ != nullptr) {
33  this->select_var_->make_call().select_next(true).perform();
34  changed = true;
35  }
36 
37  return changed;
38 }
39 
41  bool changed = false;
42 
43  if (this->select_var_ != nullptr) {
44  this->select_var_->make_call().select_previous(true).perform();
45  changed = true;
46  }
47 
48  return changed;
49 }
50 #endif // USE_SELECT
51 
52 #ifdef USE_NUMBER
53 std::string MenuItemNumber::get_value_text() const {
54  std::string result;
55 
56  if (this->value_getter_.has_value()) {
57  result = this->value_getter_.value()(this);
58  } else {
59  char data[32];
60  snprintf(data, sizeof(data), this->format_.c_str(), get_number_value_());
61  result = data;
62  }
63 
64  return result;
65 }
66 
68  bool changed = false;
69 
70  if (this->number_var_ != nullptr) {
71  float last = this->number_var_->state;
72  this->number_var_->make_call().number_increment(false).perform();
73 
74  if (this->number_var_->state != last) {
75  this->on_value_();
76  changed = true;
77  }
78  }
79 
80  return changed;
81 }
82 
84  bool changed = false;
85 
86  if (this->number_var_ != nullptr) {
87  float last = this->number_var_->state;
88  this->number_var_->make_call().number_decrement(false).perform();
89 
90  if (this->number_var_->state != last) {
91  this->on_value_();
92  changed = true;
93  }
94  }
95 
96  return changed;
97 }
98 
100  float result = 0.0;
101 
102  if (this->number_var_ != nullptr) {
103  if (!this->number_var_->has_state() || this->number_var_->state < this->number_var_->traits.get_min_value()) {
104  result = this->number_var_->traits.get_min_value();
105  } else if (this->number_var_->state > this->number_var_->traits.get_max_value()) {
106  result = this->number_var_->traits.get_max_value();
107  } else {
108  result = this->number_var_->state;
109  }
110  }
111 
112  return result;
113 }
114 #endif // USE_NUMBER
115 
116 #ifdef USE_SWITCH
117 std::string MenuItemSwitch::get_value_text() const {
118  std::string result;
119 
120  if (this->value_getter_.has_value()) {
121  result = this->value_getter_.value()(this);
122  } else {
123  result = this->get_switch_state_() ? this->switch_on_text_ : this->switch_off_text_;
124  }
125 
126  return result;
127 }
128 
129 bool MenuItemSwitch::select_next() { return this->toggle_switch_(); }
130 
131 bool MenuItemSwitch::select_prev() { return this->toggle_switch_(); }
132 
133 bool MenuItemSwitch::get_switch_state_() const { return (this->switch_var_ != nullptr && this->switch_var_->state); }
134 
136  bool changed = false;
137 
138  if (this->switch_var_ != nullptr) {
139  this->switch_var_->toggle();
140  this->on_value_();
141  changed = true;
142  }
143 
144  return changed;
145 }
146 #endif // USE_SWITCH
147 
148 std::string MenuItemCustom::get_value_text() const {
149  return (this->value_getter_.has_value()) ? this->value_getter_.value()(this) : "";
150 }
151 
153  this->on_value_();
154  return true;
155 }
156 
158  this->on_value_();
159  return true;
160 }
161 
163  this->on_next_();
164  this->on_value_();
165  return true;
166 }
167 
169  this->on_prev_();
170  this->on_value_();
171  return true;
172 }
173 
174 void MenuItemCustom::on_next_() { this->on_next_callbacks_.call(); }
175 
176 void MenuItemCustom::on_prev_() { this->on_prev_callbacks_.call(); }
177 
178 } // namespace display_menu_base
179 } // namespace esphome
CallbackManager< void()> on_leave_callbacks_
Definition: menu_item.h:66
std::string get_value_text() const override
Definition: menu_item.cpp:15
std::string get_value_text() const override
Definition: menu_item.cpp:117
CallbackManager< void()> on_enter_callbacks_
Definition: menu_item.h:65
CallbackManager< void()> on_value_callbacks_
Definition: menu_item.h:67
std::string get_value_text() const override
Definition: menu_item.cpp:148
Definition: a4988.cpp:4
std::string get_value_text() const override
Definition: menu_item.cpp:53