ESPHome  2024.4.1
lcd_menu.cpp
Go to the documentation of this file.
1 #include "lcd_menu.h"
2 #include "esphome/core/log.h"
3 #include <algorithm>
4 
5 namespace esphome {
6 namespace lcd_menu {
7 
8 static const char *const TAG = "lcd_menu";
9 
11  if (this->display_->is_failed()) {
12  this->mark_failed();
13  return;
14  }
15 
17 }
18 
20 
22  ESP_LOGCONFIG(TAG, "LCD Menu");
23  ESP_LOGCONFIG(TAG, " Columns: %u, Rows: %u", this->columns_, this->rows_);
24  ESP_LOGCONFIG(TAG, " Mark characters: %02x, %02x, %02x, %02x", this->mark_selected_, this->mark_editing_,
25  this->mark_submenu_, this->mark_back_);
26  if (this->is_failed()) {
27  ESP_LOGE(TAG, "The connected display failed, the menu is disabled!");
28  }
29 }
30 
31 void LCDCharacterMenuComponent::draw_item(const display_menu_base::MenuItem *item, uint8_t row, bool selected) {
32  char data[this->columns_ + 1]; // Bounded to 65 through the config
33 
34  memset(data, ' ', this->columns_);
35 
36  if (selected) {
37  data[0] = (this->editing_ || (this->mode_ == display_menu_base::MENU_MODE_JOYSTICK && item->get_immediate_edit()))
38  ? this->mark_editing_
39  : this->mark_selected_;
40  }
41 
42  switch (item->get_type()) {
44  data[this->columns_ - 1] = this->mark_submenu_;
45  break;
47  data[this->columns_ - 1] = this->mark_back_;
48  break;
49  default:
50  break;
51  }
52 
53  auto text = item->get_text();
54  size_t n = std::min(text.size(), (size_t) this->columns_ - 2);
55  memcpy(data + 1, item->get_text().c_str(), n);
56 
57  if (item->has_value()) {
58  std::string value = item->get_value_text();
59 
60  // Maximum: start mark, at least two chars of label, space, '[', value, ']',
61  // end mark. Config guarantees columns >= 12
62  size_t val_width = std::min((size_t) this->columns_ - 7, value.length());
63  memcpy(data + this->columns_ - val_width - 4, " [", 2);
64  memcpy(data + this->columns_ - val_width - 2, value.c_str(), val_width);
65  data[this->columns_ - 2] = ']';
66  }
67 
68  data[this->columns_] = '\0';
69 
70  this->display_->print(0, row, data);
71 }
72 
73 } // namespace lcd_menu
74 } // namespace esphome
float get_setup_priority() const override
Definition: lcd_menu.cpp:19
virtual bool has_value() const
Definition: menu_item.h:53
MenuItemType get_type() const
Definition: menu_item.h:45
virtual bool get_immediate_edit() const
Definition: menu_item.h:52
virtual void setup()
Where the component&#39;s initialization should happen.
Definition: component.cpp:48
virtual std::string get_value_text() const
Definition: menu_item.h:54
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:20
std::string get_text() const
Definition: menu_item.h:51
void print(uint8_t column, uint8_t row, const char *str)
Print the given text at the specified column and row.
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void draw_item(const display_menu_base::MenuItem *item, uint8_t row, bool selected) override
Definition: lcd_menu.cpp:31