ESPHome 2025.5.0
Loading...
Searching...
No Matches
lcd_display.cpp
Go to the documentation of this file.
1#include "lcd_display.h"
2#include "esphome/core/log.h"
4#include "esphome/core/hal.h"
5
6namespace esphome {
7namespace lcd_base {
8
9static const char *const TAG = "lcd";
10
11// First set bit determines command, bits after that are the data.
12static const uint8_t LCD_DISPLAY_COMMAND_CLEAR_DISPLAY = 0x01;
13static const uint8_t LCD_DISPLAY_COMMAND_RETURN_HOME = 0x02;
14static const uint8_t LCD_DISPLAY_COMMAND_ENTRY_MODE_SET = 0x04;
15static const uint8_t LCD_DISPLAY_COMMAND_DISPLAY_CONTROL = 0x08;
16static const uint8_t LCD_DISPLAY_COMMAND_CURSOR_SHIFT = 0x10;
17static const uint8_t LCD_DISPLAY_COMMAND_FUNCTION_SET = 0x20;
18static const uint8_t LCD_DISPLAY_COMMAND_SET_CGRAM_ADDR = 0x40;
19static const uint8_t LCD_DISPLAY_COMMAND_SET_DDRAM_ADDR = 0x80;
20
21static const uint8_t LCD_DISPLAY_ENTRY_SHIFT_INCREMENT = 0x01;
22static const uint8_t LCD_DISPLAY_ENTRY_LEFT = 0x02;
23
24static const uint8_t LCD_DISPLAY_DISPLAY_BLINK_ON = 0x01;
25static const uint8_t LCD_DISPLAY_DISPLAY_CURSOR_ON = 0x02;
26static const uint8_t LCD_DISPLAY_DISPLAY_ON = 0x04;
27
28static const uint8_t LCD_DISPLAY_FUNCTION_8_BIT_MODE = 0x10;
29static const uint8_t LCD_DISPLAY_FUNCTION_2_LINE = 0x08;
30static 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);
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);
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
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}
119void LCDDisplay::command_(uint8_t value) { this->send(value, false); }
120void 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}
136void LCDDisplay::print(uint8_t column, uint8_t row, const std::string &str) { this->print(column, row, str.c_str()); }
137void LCDDisplay::print(const char *str) { this->print(0, 0, str); }
138void LCDDisplay::print(const std::string &str) { this->print(0, 0, str.c_str()); }
139void 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}
148void 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}
161void 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}
167void LCDDisplay::strftime(const char *format, ESPTime time) { this->strftime(0, 0, format, time); }
168void 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
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.
std::map< uint8_t, std::vector< uint8_t > > user_defined_chars_
Definition lcd_display.h:62
void command_(uint8_t value)
virtual void write_n_bits(uint8_t value, uint8_t n)=0
virtual void send(uint8_t value, bool rs)=0
void print(uint8_t column, uint8_t row, const char *str)
Print the given text at the specified column and row.
virtual void call_writer()=0
float get_setup_priority() const override
void void void void void loadchar(uint8_t location, uint8_t charmap[])
Load custom char to given location.
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.
virtual bool is_four_bit_mode()=0
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:27
A more user-friendly version of struct tm from time.h.
Definition time.h:15
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:15
std::string print()