ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
st7789v.cpp
Go to the documentation of this file.
1#include "st7789v.h"
2#include "esphome/core/log.h"
3#include <algorithm>
4
5namespace esphome::st7789v {
6
7static const char *const TAG = "st7789v";
8#ifdef USE_ESP32
9static constexpr size_t TEMP_BUFFER_SIZE = 1024;
10#else
11static constexpr size_t TEMP_BUFFER_SIZE = 512;
12#endif
13
15#ifdef USE_POWER_SUPPLY
16 this->power_.request();
17 // the PowerSupply component takes care of post turn-on delay
18#endif
19 this->spi_setup();
20 this->dc_pin_->setup(); // OUTPUT
21
22 this->init_reset_();
23
24 this->write_command_(ST7789_SLPOUT); // Sleep out
25 delay(120); // NOLINT
26
27 this->write_command_(ST7789_NORON); // Normal display mode on
28
29 // *** display and color format setting ***
30 this->write_command_(ST7789_MADCTL);
31 this->write_data_(ST7789_MADCTL_COLOR_ORDER);
32
33 // JLX240 display datasheet
34 this->write_command_(0xB6);
35 this->write_data_(0x0A);
36 this->write_data_(0x82);
37
38 this->write_command_(ST7789_COLMOD);
39 this->write_data_(0x55);
40 delay(10);
41
42 // *** ST7789V Frame rate setting ***
43 this->write_command_(ST7789_PORCTRL);
44 this->write_data_(0x0c);
45 this->write_data_(0x0c);
46 this->write_data_(0x00);
47 this->write_data_(0x33);
48 this->write_data_(0x33);
49
50 this->write_command_(ST7789_GCTRL); // Voltages: VGH / VGL
51 this->write_data_(0x35);
52
53 // *** ST7789V Power setting ***
54 this->write_command_(ST7789_VCOMS);
55 this->write_data_(0x28); // JLX240 display datasheet
56
57 this->write_command_(ST7789_LCMCTRL);
58 this->write_data_(0x0C);
59
60 this->write_command_(ST7789_VDVVRHEN);
61 this->write_data_(0x01);
62 this->write_data_(0xFF);
63
64 this->write_command_(ST7789_VRHS); // voltage VRHS
65 this->write_data_(0x10);
66
67 this->write_command_(ST7789_VDVS);
68 this->write_data_(0x20);
69
70 this->write_command_(ST7789_FRCTRL2);
71 this->write_data_(0x0f);
72
73 this->write_command_(ST7789_PWCTRL1);
74 this->write_data_(0xa4);
75 this->write_data_(0xa1);
76
77 // *** ST7789V gamma setting ***
78 this->write_command_(ST7789_PVGAMCTRL);
79 this->write_data_(0xd0);
80 this->write_data_(0x00);
81 this->write_data_(0x02);
82 this->write_data_(0x07);
83 this->write_data_(0x0a);
84 this->write_data_(0x28);
85 this->write_data_(0x32);
86 this->write_data_(0x44);
87 this->write_data_(0x42);
88 this->write_data_(0x06);
89 this->write_data_(0x0e);
90 this->write_data_(0x12);
91 this->write_data_(0x14);
92 this->write_data_(0x17);
93
94 this->write_command_(ST7789_NVGAMCTRL);
95 this->write_data_(0xd0);
96 this->write_data_(0x00);
97 this->write_data_(0x02);
98 this->write_data_(0x07);
99 this->write_data_(0x0a);
100 this->write_data_(0x28);
101 this->write_data_(0x31);
102 this->write_data_(0x54);
103 this->write_data_(0x47);
104 this->write_data_(0x0e);
105 this->write_data_(0x1c);
106 this->write_data_(0x17);
107 this->write_data_(0x1b);
108 this->write_data_(0x1e);
109
110 this->write_command_(ST7789_INVON);
111
112 // Clear display - ensures we do not see garbage at power-on
113 this->draw_filled_rect_(0, 0, this->get_width_internal(), this->get_height_internal(), 0x0000);
114
115 delay(120); // NOLINT
116
117 this->write_command_(ST7789_DISPON); // Display on
118 delay(120); // NOLINT
119
120 backlight_(true);
121
122 this->init_internal_(this->get_buffer_length_());
123 memset(this->buffer_, 0x00, this->get_buffer_length_());
124}
125
127 LOG_DISPLAY("", "SPI ST7789V", this);
128 ESP_LOGCONFIG(TAG,
129 " Model: %s\n"
130 " Height: %u\n"
131 " Width: %u\n"
132 " Height Offset: %u\n"
133 " Width Offset: %u\n"
134 " 8-bit color mode: %s\n"
135 " Data rate: %dMHz",
136 this->model_str_, this->height_, this->width_, this->offset_height_, this->offset_width_,
137 YESNO(this->eightbitcolor_), (unsigned) (this->data_rate_ / 1000000));
138 LOG_PIN(" CS Pin: ", this->cs_);
139 LOG_PIN(" DC Pin: ", this->dc_pin_);
140 LOG_PIN(" Reset Pin: ", this->reset_pin_);
141 LOG_PIN(" B/L Pin: ", this->backlight_pin_);
142 LOG_UPDATE_INTERVAL(this);
143#ifdef USE_POWER_SUPPLY
144 ESP_LOGCONFIG(TAG, " Power Supply Configured: yes");
145#endif
146}
147
149
151 this->do_update_();
152 this->write_display_data();
153}
154
155void ST7789V::set_model_str(const char *model_str) { this->model_str_ = model_str; }
156
158 uint16_t x1 = this->offset_width_;
159 uint16_t x2 = x1 + get_width_internal() - 1;
160 uint16_t y1 = this->offset_height_;
161 uint16_t y2 = y1 + get_height_internal() - 1;
162
163 this->enable();
164
165 // set column(x) address
166 this->dc_pin_->digital_write(false);
167 this->write_byte(ST7789_CASET);
168 this->dc_pin_->digital_write(true);
169 this->write_addr_(x1, x2);
170 // set page(y) address
171 this->dc_pin_->digital_write(false);
172 this->write_byte(ST7789_RASET);
173 this->dc_pin_->digital_write(true);
174 this->write_addr_(y1, y2);
175 // write display memory
176 this->dc_pin_->digital_write(false);
177 this->write_byte(ST7789_RAMWR);
178 this->dc_pin_->digital_write(true);
179
180 if (this->eightbitcolor_) {
181 uint8_t temp_buffer[TEMP_BUFFER_SIZE];
182 size_t temp_index = 0;
183 size_t width = static_cast<size_t>(this->get_width_internal());
184 for (size_t line = 0; line < this->get_buffer_length_(); line += width) {
185 for (size_t index = 0; index < width; ++index) {
189 temp_buffer[temp_index++] = (uint8_t) (color >> 8);
190 temp_buffer[temp_index++] = (uint8_t) color;
191 if (temp_index == TEMP_BUFFER_SIZE) {
192 this->write_array(temp_buffer, TEMP_BUFFER_SIZE);
193 temp_index = 0;
194 }
195 }
196 }
197 if (temp_index != 0)
198 this->write_array(temp_buffer, temp_index);
199 } else {
200 this->write_array(this->buffer_, this->get_buffer_length_());
201 }
202
203 this->disable();
204}
205
207 if (this->reset_pin_ != nullptr) {
208 this->reset_pin_->setup();
209 this->reset_pin_->digital_write(true);
210 delay(1);
211 // Trigger Reset
212 this->reset_pin_->digital_write(false);
213 delay(1);
214 // Wake up
215 this->reset_pin_->digital_write(true);
216 delay(5);
217 }
218}
219
220void ST7789V::backlight_(bool onoff) {
221 if (this->backlight_pin_ != nullptr) {
222 this->backlight_pin_->setup();
223 this->backlight_pin_->digital_write(onoff);
224 }
225}
226
227void ST7789V::write_command_(uint8_t value) {
228 this->enable();
229 this->dc_pin_->digital_write(false);
230 this->write_byte(value);
231 this->dc_pin_->digital_write(true);
232 this->disable();
233}
234
235void ST7789V::write_data_(uint8_t value) {
236 this->dc_pin_->digital_write(true);
237 this->enable();
238 this->write_byte(value);
239 this->disable();
240}
241
242void ST7789V::write_addr_(uint16_t addr1, uint16_t addr2) {
243 uint8_t byte[4];
244 byte[0] = (addr1 >> 8) & 0xFF;
245 byte[1] = addr1 & 0xFF;
246 byte[2] = (addr2 >> 8) & 0xFF;
247 byte[3] = addr2 & 0xFF;
248
249 this->dc_pin_->digital_write(true);
250 this->write_array(byte, 4);
251}
252
253void ST7789V::write_color_(uint16_t color, uint16_t size) {
254 uint8_t byte[TEMP_BUFFER_SIZE];
255 uint16_t remaining = size;
256 this->dc_pin_->digital_write(true);
257 while (remaining > 0) {
258 uint16_t batch = std::min(remaining, static_cast<uint16_t>(sizeof(byte) / 2));
259 int index = 0;
260 for (int i = 0; i < batch; i++) {
261 byte[index++] = (color >> 8) & 0xFF;
262 byte[index++] = color & 0xFF;
263 }
264 this->write_array(byte, batch * 2);
265 remaining -= batch;
266 }
267}
268
270 if (this->eightbitcolor_) {
271 return size_t(this->get_width_internal()) * size_t(this->get_height_internal());
272 }
273 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
274}
275
276// Draw a filled rectangle
277// x1: Start X coordinate
278// y1: Start Y coordinate
279// x2: End X coordinate
280// y2: End Y coordinate
281// color: color
282void ST7789V::draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
283 this->enable();
284 this->dc_pin_->digital_write(false);
285 this->write_byte(ST7789_CASET); // set column(x) address
286 this->dc_pin_->digital_write(true);
287 this->write_addr_(x1, x2);
288
289 this->dc_pin_->digital_write(false);
290 this->write_byte(ST7789_RASET); // set Page(y) address
291 this->dc_pin_->digital_write(true);
292 this->write_addr_(y1, y2);
293 this->dc_pin_->digital_write(false);
294 this->write_byte(ST7789_RAMWR); // begin a write to memory
295 this->dc_pin_->digital_write(true);
296 for (int i = x1; i <= x2; i++) {
297 uint16_t size = y2 - y1 + 1;
298 this->write_color_(color, size);
299 }
300 this->disable();
301}
302
304 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
305 return;
306
307 if (this->eightbitcolor_) {
308 auto color332 = display::ColorUtil::color_to_332(color);
309 uint32_t pos = (x + y * this->get_width_internal());
310 this->buffer_[pos] = color332;
311 } else {
312 auto color565 = display::ColorUtil::color_to_565(color);
313 uint32_t pos = (x + y * this->get_width_internal()) * 2;
314 this->buffer_[pos++] = (color565 >> 8) & 0xff;
315 this->buffer_[pos] = color565 & 0xff;
316 }
317}
318
319} // namespace esphome::st7789v
virtual void setup()=0
virtual void digital_write(bool value)=0
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
static uint8_t color_to_332(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
static Color to_color(uint32_t colorcode, ColorOrder color_order, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
void init_internal_(uint32_t buffer_length)
uint32_t data_rate_
Definition spi.h:412
void dump_config() override
Definition st7789v.cpp:126
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition st7789v.cpp:303
void backlight_(bool onoff)
Definition st7789v.cpp:220
void write_addr_(uint16_t addr1, uint16_t addr2)
Definition st7789v.cpp:242
void draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition st7789v.cpp:282
const char * model_str_
Definition st7789v.h:167
void set_model_str(const char *model_str)
Definition st7789v.cpp:155
void write_command_(uint8_t value)
Definition st7789v.cpp:227
void update() override
Definition st7789v.cpp:150
float get_setup_priority() const override
Definition st7789v.cpp:148
void setup() override
Definition st7789v.cpp:14
int get_height_internal() override
Definition st7789v.h:159
void write_data_(uint8_t value)
Definition st7789v.cpp:235
void write_color_(uint16_t color, uint16_t size)
Definition st7789v.cpp:253
int get_width_internal() override
Definition st7789v.h:160
power_supply::PowerSupplyRequester power_
Definition st7789v.h:143
constexpr float PROCESSOR
For components that use data from sensors like displays.
Definition component.h:45
const char int line
Definition log.h:74
uint16_t size
Definition helpers.cpp:25
size_t size_t pos
Definition helpers.h:1038
void HOT delay(uint32_t ms)
Definition hal.cpp:82
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6