ESPHome  2024.4.1
lcd_display.cpp
Go to the documentation of this file.
1 #include "lcd_display.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/hal.h"
5 
6 namespace esphome {
7 namespace lcd_base {
8 
9 static const char *const TAG = "lcd";
10 
11 // First set bit determines command, bits after that are the data.
12 static const uint8_t LCD_DISPLAY_COMMAND_CLEAR_DISPLAY = 0x01;
13 static const uint8_t LCD_DISPLAY_COMMAND_RETURN_HOME = 0x02;
14 static const uint8_t LCD_DISPLAY_COMMAND_ENTRY_MODE_SET = 0x04;
15 static const uint8_t LCD_DISPLAY_COMMAND_DISPLAY_CONTROL = 0x08;
16 static const uint8_t LCD_DISPLAY_COMMAND_CURSOR_SHIFT = 0x10;
17 static const uint8_t LCD_DISPLAY_COMMAND_FUNCTION_SET = 0x20;
18 static const uint8_t LCD_DISPLAY_COMMAND_SET_CGRAM_ADDR = 0x40;
19 static const uint8_t LCD_DISPLAY_COMMAND_SET_DDRAM_ADDR = 0x80;
20 
21 static const uint8_t LCD_DISPLAY_ENTRY_SHIFT_INCREMENT = 0x01;
22 static const uint8_t LCD_DISPLAY_ENTRY_LEFT = 0x02;
23 
24 static const uint8_t LCD_DISPLAY_DISPLAY_BLINK_ON = 0x01;
25 static const uint8_t LCD_DISPLAY_DISPLAY_CURSOR_ON = 0x02;
26 static const uint8_t LCD_DISPLAY_DISPLAY_ON = 0x04;
27 
28 static const uint8_t LCD_DISPLAY_FUNCTION_8_BIT_MODE = 0x10;
29 static const uint8_t LCD_DISPLAY_FUNCTION_2_LINE = 0x08;
30 static const uint8_t LCD_DISPLAY_FUNCTION_5X10_DOTS = 0x04;
31 
33  this->buffer_ = new uint8_t[this->rows_ * this->columns_]; // NOLINT
34  for (uint8_t i = 0; i < this->rows_ * this->columns_; i++)
35  this->buffer_[i] = ' ';
36 
37  uint8_t display_function = 0;
38 
39  if (!this->is_four_bit_mode())
40  display_function |= LCD_DISPLAY_FUNCTION_8_BIT_MODE;
41 
42  if (this->rows_ > 1)
43  display_function |= LCD_DISPLAY_FUNCTION_2_LINE;
44 
45  // TODO dotsize
46 
47  // Commands can only be sent 40ms after boot-up, so let's wait if we're close
48  const uint8_t now = millis();
49  if (now < 40)
50  delay(40u - now);
51 
52  if (this->is_four_bit_mode()) {
53  this->write_n_bits(0x03, 4);
54  delay(5); // 4.1ms
55  this->write_n_bits(0x03, 4);
56  delay(5);
57  this->write_n_bits(0x03, 4);
58  delayMicroseconds(150);
59  this->write_n_bits(0x02, 4);
60  } else {
61  this->command_(LCD_DISPLAY_COMMAND_FUNCTION_SET | display_function);
62  delay(5); // 4.1ms
63  this->command_(LCD_DISPLAY_COMMAND_FUNCTION_SET | display_function);
64  delayMicroseconds(150);
65  this->command_(LCD_DISPLAY_COMMAND_FUNCTION_SET | display_function);
66  }
67 
68  // store user defined characters
69  for (auto &user_defined_char : this->user_defined_chars_) {
70  this->command_(LCD_DISPLAY_COMMAND_SET_CGRAM_ADDR | (user_defined_char.first << 3));
71  for (auto data : user_defined_char.second)
72  this->send(data, true);
73  }
74 
75  this->command_(LCD_DISPLAY_COMMAND_FUNCTION_SET | display_function);
76  uint8_t display_control = LCD_DISPLAY_DISPLAY_ON;
77  this->command_(LCD_DISPLAY_COMMAND_DISPLAY_CONTROL | display_control);
78 
79  // clear display, also sets DDRAM address to 0 (home)
80  this->command_(LCD_DISPLAY_COMMAND_CLEAR_DISPLAY);
81  delay(2); // 1.52ms
82 
83  uint8_t entry_mode = LCD_DISPLAY_ENTRY_LEFT;
84  this->command_(LCD_DISPLAY_COMMAND_ENTRY_MODE_SET | entry_mode); // 37µs
85 
86  this->command_(LCD_DISPLAY_COMMAND_RETURN_HOME);
87  delay(2); // 1.52ms
88 }
89 
91 void HOT LCDDisplay::display() {
92  this->command_(LCD_DISPLAY_COMMAND_SET_DDRAM_ADDR | 0);
93 
94  for (uint8_t i = 0; i < this->columns_; i++)
95  this->send(this->buffer_[i], true);
96 
97  if (this->rows_ >= 3) {
98  for (uint8_t i = 0; i < this->columns_; i++)
99  this->send(this->buffer_[this->columns_ * 2 + i], true);
100  }
101 
102  if (this->rows_ >= 1) {
103  this->command_(LCD_DISPLAY_COMMAND_SET_DDRAM_ADDR | 0x40);
104 
105  for (uint8_t i = 0; i < this->columns_; i++)
106  this->send(this->buffer_[this->columns_ + i], true);
107 
108  if (this->rows_ >= 4) {
109  for (uint8_t i = 0; i < this->columns_; i++)
110  this->send(this->buffer_[this->columns_ * 3 + i], true);
111  }
112  }
113 }
115  this->clear();
116  this->call_writer();
117  this->display();
118 }
119 void LCDDisplay::command_(uint8_t value) { this->send(value, false); }
120 void LCDDisplay::print(uint8_t column, uint8_t row, const char *str) {
121  uint8_t pos = column + row * this->columns_;
122  for (; *str != '\0'; str++) {
123  if (*str == '\n') {
124  pos = ((pos / this->columns_) + 1) * this->columns_;
125  continue;
126  }
127  if (pos >= this->rows_ * this->columns_) {
128  ESP_LOGW(TAG, "LCDDisplay writing out of range!");
129  break;
130  }
131 
132  this->buffer_[pos] = *reinterpret_cast<const uint8_t *>(str);
133  pos++;
134  }
135 }
136 void LCDDisplay::print(uint8_t column, uint8_t row, const std::string &str) { this->print(column, row, str.c_str()); }
137 void LCDDisplay::print(const char *str) { this->print(0, 0, str); }
138 void LCDDisplay::print(const std::string &str) { this->print(0, 0, str.c_str()); }
139 void LCDDisplay::printf(uint8_t column, uint8_t row, const char *format, ...) {
140  va_list arg;
141  va_start(arg, format);
142  char buffer[256];
143  int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
144  va_end(arg);
145  if (ret > 0)
146  this->print(column, row, buffer);
147 }
148 void LCDDisplay::printf(const char *format, ...) {
149  va_list arg;
150  va_start(arg, format);
151  char buffer[256];
152  int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
153  va_end(arg);
154  if (ret > 0)
155  this->print(0, 0, buffer);
156 }
158  for (uint8_t i = 0; i < this->rows_ * this->columns_; i++)
159  this->buffer_[i] = ' ';
160 }
161 void LCDDisplay::strftime(uint8_t column, uint8_t row, const char *format, ESPTime time) {
162  char buffer[64];
163  size_t ret = time.strftime(buffer, sizeof(buffer), format);
164  if (ret > 0)
165  this->print(column, row, buffer);
166 }
167 void LCDDisplay::strftime(const char *format, ESPTime time) { this->strftime(0, 0, format, time); }
168 void LCDDisplay::loadchar(uint8_t location, uint8_t charmap[]) {
169  location &= 0x7; // we only have 8 locations 0-7
170  this->command_(LCD_DISPLAY_COMMAND_SET_CGRAM_ADDR | (location << 3));
171  for (int i = 0; i < 8; i++) {
172  this->send(charmap[i], true);
173  }
174 }
175 
176 } // namespace lcd_base
177 } // namespace esphome
size_t strftime(char *buffer, size_t buffer_len, const char *format)
Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument...
Definition: time.cpp:18
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
virtual void send(uint8_t value, bool rs)=0
void void void void void loadchar(uint8_t location, uint8_t charmap[])
Load custom char to given location.
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
virtual void write_n_bits(uint8_t value, uint8_t n)=0
virtual void call_writer()=0
std::map< uint8_t, std::vector< uint8_t > > user_defined_chars_
Definition: lcd_display.h:62
void printf(uint8_t column, uint8_t row, const char *format,...) __attribute__((format(printf
Evaluate the printf-format and print the text at the specified column and row.
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:20
void print(uint8_t column, uint8_t row, const char *str)
Print the given text at the specified column and row.
float get_setup_priority() const override
Definition: lcd_display.cpp:90
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 IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
virtual bool is_four_bit_mode()=0
void void void strftime(uint8_t column, uint8_t row, const char *format, ESPTime time) __attribute__((format(strftime
Evaluate the strftime-format and print the text at the specified column and row.
void command_(uint8_t value)
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26