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