ESPHome 2026.11.0-dev
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
7ESPHOME_LOG_TAG(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
156 uint16_t x1 = this->offset_width_;
157 uint16_t x2 = x1 + get_width_internal() - 1;
158 uint16_t y1 = this->offset_height_;
159 uint16_t y2 = y1 + get_height_internal() - 1;
160
161 this->enable();
162
163 // set column(x) address
164 this->dc_pin_->digital_write(false);
165 this->write_byte(ST7789_CASET);
166 this->dc_pin_->digital_write(true);
167 this->write_addr_(x1, x2);
168 // set page(y) address
169 this->dc_pin_->digital_write(false);
170 this->write_byte(ST7789_RASET);
171 this->dc_pin_->digital_write(true);
172 this->write_addr_(y1, y2);
173 // write display memory
174 this->dc_pin_->digital_write(false);
175 this->write_byte(ST7789_RAMWR);
176 this->dc_pin_->digital_write(true);
177
178 if (this->eightbitcolor_) {
179 uint8_t temp_buffer[TEMP_BUFFER_SIZE];
180 size_t temp_index = 0;
181 size_t width = static_cast<size_t>(this->get_width_internal());
182 for (size_t line = 0; line < this->get_buffer_length_(); line += width) {
183 for (size_t index = 0; index < width; ++index) {
184 auto color = display::ColorUtil::color_to_565(
186 temp_buffer[temp_index++] = (uint8_t) (color >> 8);
187 temp_buffer[temp_index++] = (uint8_t) color;
188 if (temp_index == TEMP_BUFFER_SIZE) {
189 this->write_array(temp_buffer, TEMP_BUFFER_SIZE);
190 temp_index = 0;
191 }
192 }
193 }
194 if (temp_index != 0)
195 this->write_array(temp_buffer, temp_index);
196 } else {
197 this->write_array(this->buffer_, this->get_buffer_length_());
198 }
199
200 this->disable();
201}
202
204 if (this->reset_pin_ != nullptr) {
205 this->reset_pin_->setup();
206 this->reset_pin_->digital_write(true);
207 delay(1);
208 // Trigger Reset
209 this->reset_pin_->digital_write(false);
210 delay(1);
211 // Wake up
212 this->reset_pin_->digital_write(true);
213 delay(5);
214 }
215}
216
217void ST7789V::backlight_(bool onoff) {
218 if (this->backlight_pin_ != nullptr) {
219 this->backlight_pin_->setup();
220 this->backlight_pin_->digital_write(onoff);
221 }
222}
223
224void ST7789V::write_command_(uint8_t value) {
225 this->enable();
226 this->dc_pin_->digital_write(false);
227 this->write_byte(value);
228 this->dc_pin_->digital_write(true);
229 this->disable();
230}
231
232void ST7789V::write_data_(uint8_t value) {
233 this->dc_pin_->digital_write(true);
234 this->enable();
235 this->write_byte(value);
236 this->disable();
237}
238
239void ST7789V::write_addr_(uint16_t addr1, uint16_t addr2) {
240 uint8_t byte[4];
241 byte[0] = (addr1 >> 8) & 0xFF;
242 byte[1] = addr1 & 0xFF;
243 byte[2] = (addr2 >> 8) & 0xFF;
244 byte[3] = addr2 & 0xFF;
245
246 this->dc_pin_->digital_write(true);
247 this->write_array(byte, 4);
248}
249
250void ST7789V::write_color_(uint16_t color, uint16_t size) {
251 uint8_t byte[TEMP_BUFFER_SIZE];
252 uint16_t remaining = size;
253 this->dc_pin_->digital_write(true);
254 while (remaining > 0) {
255 uint16_t batch = std::min(remaining, static_cast<uint16_t>(sizeof(byte) / 2));
256 int index = 0;
257 for (int i = 0; i < batch; i++) {
258 byte[index++] = (color >> 8) & 0xFF;
259 byte[index++] = color & 0xFF;
260 }
261 this->write_array(byte, batch * 2);
262 remaining -= batch;
263 }
264}
265
267 if (this->eightbitcolor_) {
268 return size_t(this->get_width_internal()) * size_t(this->get_height_internal());
269 }
270 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
271}
272
273// Draw a filled rectangle
274// x1: Start X coordinate
275// y1: Start Y coordinate
276// x2: End X coordinate
277// y2: End Y coordinate
278// color: color
279void ST7789V::draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
280 this->enable();
281 this->dc_pin_->digital_write(false);
282 this->write_byte(ST7789_CASET); // set column(x) address
283 this->dc_pin_->digital_write(true);
284 this->write_addr_(x1, x2);
285
286 this->dc_pin_->digital_write(false);
287 this->write_byte(ST7789_RASET); // set Page(y) address
288 this->dc_pin_->digital_write(true);
289 this->write_addr_(y1, y2);
290 this->dc_pin_->digital_write(false);
291 this->write_byte(ST7789_RAMWR); // begin a write to memory
292 this->dc_pin_->digital_write(true);
293 for (int i = x1; i <= x2; i++) {
294 uint16_t size = y2 - y1 + 1;
295 this->write_color_(color, size);
296 }
297 this->disable();
298}
299
301 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
302 return;
303
304 if (this->eightbitcolor_) {
305 auto color332 = display::ColorUtil::color_to_332(color);
306 uint32_t pos = (x + y * this->get_width_internal());
307 this->buffer_[pos] = color332;
308 } else {
309 auto color565 = display::ColorUtil::color_to_565(color);
310 uint32_t pos = (x + y * this->get_width_internal()) * 2;
311 this->buffer_[pos++] = (color565 >> 8) & 0xff;
312 this->buffer_[pos] = color565 & 0xff;
313 }
314}
315
316} // namespace esphome::st7789v
virtual void setup()=0
virtual void digital_write(bool value)=0
static Color to_color(uint32_t colorcode, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
Unpacks a color of the given bitness, red in the most significant bits.
void init_internal_(uint32_t buffer_length)
void line(int x1, int y1, int x2, int y2, Color color=COLOR_ON)
Draw a straight line from the point [x1,y1] to [x2,y2] with the given color.
Definition display.cpp:20
uint32_t data_rate_
Definition spi.h:436
void dump_config() override
Definition st7789v.cpp:126
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition st7789v.cpp:300
void backlight_(bool onoff)
Definition st7789v.cpp:217
void write_addr_(uint16_t addr1, uint16_t addr2)
Definition st7789v.cpp:239
void draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition st7789v.cpp:279
const char * model_str_
Definition st7789v.h:167
void write_command_(uint8_t value)
Definition st7789v.cpp:224
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:232
void write_color_(uint16_t color, uint16_t size)
Definition st7789v.cpp:250
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:47
ESPHOME_LOG_TAG(TAG, "st7789v")
uint16_t size
Definition helpers.cpp:34
size_t size_t pos
Definition helpers.h:1255
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6