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