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