ESPHome  2024.10.2
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 
9  switch (type) {
11  return LOG_STR("MENU_ITEM_LABEL");
13  return LOG_STR("MENU_ITEM_MENU");
15  return LOG_STR("MENU_ITEM_BACK");
17  return LOG_STR("MENU_ITEM_SELECT");
19  return LOG_STR("MENU_ITEM_NUMBER");
21  return LOG_STR("MENU_ITEM_SWITCH");
23  return LOG_STR("MENU_ITEM_COMMAND");
25  return LOG_STR("MENU_ITEM_CUSTOM");
26  default:
27  return LOG_STR("UNKNOWN");
28  }
29 }
30 
31 void MenuItem::on_enter() { this->on_enter_callbacks_.call(); }
32 
33 void MenuItem::on_leave() { this->on_leave_callbacks_.call(); }
34 
35 void MenuItem::on_value_() { this->on_value_callbacks_.call(); }
36 
37 #ifdef USE_SELECT
38 std::string MenuItemSelect::get_value_text() const {
39  std::string result;
40 
41  if (this->value_getter_.has_value()) {
42  result = this->value_getter_.value()(this);
43  } else {
44  if (this->select_var_ != nullptr) {
45  result = this->select_var_->state;
46  }
47  }
48 
49  return result;
50 }
51 
53  bool changed = false;
54 
55  if (this->select_var_ != nullptr) {
56  this->select_var_->make_call().select_next(true).perform();
57  changed = true;
58  }
59 
60  return changed;
61 }
62 
64  bool changed = false;
65 
66  if (this->select_var_ != nullptr) {
67  this->select_var_->make_call().select_previous(true).perform();
68  changed = true;
69  }
70 
71  return changed;
72 }
73 #endif // USE_SELECT
74 
75 #ifdef USE_NUMBER
76 std::string MenuItemNumber::get_value_text() const {
77  std::string result;
78 
79  if (this->value_getter_.has_value()) {
80  result = this->value_getter_.value()(this);
81  } else {
82  char data[32];
83  snprintf(data, sizeof(data), this->format_.c_str(), get_number_value_());
84  result = data;
85  }
86 
87  return result;
88 }
89 
91  bool changed = false;
92 
93  if (this->number_var_ != nullptr) {
94  float last = this->number_var_->state;
95  this->number_var_->make_call().number_increment(false).perform();
96 
97  if (this->number_var_->state != last) {
98  this->on_value_();
99  changed = true;
100  }
101  }
102 
103  return changed;
104 }
105 
107  bool changed = false;
108 
109  if (this->number_var_ != nullptr) {
110  float last = this->number_var_->state;
111  this->number_var_->make_call().number_decrement(false).perform();
112 
113  if (this->number_var_->state != last) {
114  this->on_value_();
115  changed = true;
116  }
117  }
118 
119  return changed;
120 }
121 
123  float result = 0.0;
124 
125  if (this->number_var_ != nullptr) {
126  if (!this->number_var_->has_state() || this->number_var_->state < this->number_var_->traits.get_min_value()) {
127  result = this->number_var_->traits.get_min_value();
128  } else if (this->number_var_->state > this->number_var_->traits.get_max_value()) {
129  result = this->number_var_->traits.get_max_value();
130  } else {
131  result = this->number_var_->state;
132  }
133  }
134 
135  return result;
136 }
137 #endif // USE_NUMBER
138 
139 #ifdef USE_SWITCH
140 std::string MenuItemSwitch::get_value_text() const {
141  std::string result;
142 
143  if (this->value_getter_.has_value()) {
144  result = this->value_getter_.value()(this);
145  } else {
146  result = this->get_switch_state_() ? this->switch_on_text_ : this->switch_off_text_;
147  }
148 
149  return result;
150 }
151 
152 bool MenuItemSwitch::select_next() { return this->toggle_switch_(); }
153 
154 bool MenuItemSwitch::select_prev() { return this->toggle_switch_(); }
155 
156 bool MenuItemSwitch::get_switch_state_() const { return (this->switch_var_ != nullptr && this->switch_var_->state); }
157 
159  bool changed = false;
160 
161  if (this->switch_var_ != nullptr) {
162  this->switch_var_->toggle();
163  this->on_value_();
164  changed = true;
165  }
166 
167  return changed;
168 }
169 #endif // USE_SWITCH
170 
171 std::string MenuItemCustom::get_value_text() const {
172  return (this->value_getter_.has_value()) ? this->value_getter_.value()(this) : "";
173 }
174 
176  this->on_value_();
177  return true;
178 }
179 
181  this->on_value_();
182  return true;
183 }
184 
186  this->on_next_();
187  this->on_value_();
188  return true;
189 }
190 
192  this->on_prev_();
193  this->on_value_();
194  return true;
195 }
196 
197 void MenuItemCustom::on_next_() { this->on_next_callbacks_.call(); }
198 
199 void MenuItemCustom::on_prev_() { this->on_prev_callbacks_.call(); }
200 
201 } // namespace display_menu_base
202 } // namespace esphome
CallbackManager< void()> on_leave_callbacks_
Definition: menu_item.h:70
std::string get_value_text() const override
Definition: menu_item.cpp:38
std::string get_value_text() const override
Definition: menu_item.cpp:140
uint8_t type
CallbackManager< void()> on_enter_callbacks_
Definition: menu_item.h:69
CallbackManager< void()> on_value_callbacks_
Definition: menu_item.h:71
std::string get_value_text() const override
Definition: menu_item.cpp:171
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const LogString * menu_item_type_to_string(MenuItemType type)
Returns a string representation of a menu item type suitable for logging.
Definition: menu_item.cpp:8
std::string get_value_text() const override
Definition: menu_item.cpp:76