ESPHome 2025.12.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 size_t width = static_cast<size_t>(this->get_width_internal());
180 for (size_t line = 0; line < this->get_buffer_length_(); line += width) {
181 for (size_t index = 0; index < width; ++index) {
185 temp_buffer[temp_index++] = (uint8_t) (color >> 8);
186 temp_buffer[temp_index++] = (uint8_t) color;
187 if (temp_index == TEMP_BUFFER_SIZE) {
188 this->write_array(temp_buffer, TEMP_BUFFER_SIZE);
189 temp_index = 0;
190 }
191 }
192 }
193 if (temp_index != 0)
194 this->write_array(temp_buffer, temp_index);
195 } else {
196 this->write_array(this->buffer_, this->get_buffer_length_());
197 }
198
199 this->disable();
200}
201
203 if (this->reset_pin_ != nullptr) {
204 this->reset_pin_->setup();
205 this->reset_pin_->digital_write(true);
206 delay(1);
207 // Trigger Reset
208 this->reset_pin_->digital_write(false);
209 delay(1);
210 // Wake up
211 this->reset_pin_->digital_write(true);
212 delay(5);
213 }
214}
215
216void ST7789V::backlight_(bool onoff) {
217 if (this->backlight_pin_ != nullptr) {
218 this->backlight_pin_->setup();
219 this->backlight_pin_->digital_write(onoff);
220 }
221}
222
223void ST7789V::write_command_(uint8_t value) {
224 this->enable();
225 this->dc_pin_->digital_write(false);
226 this->write_byte(value);
227 this->dc_pin_->digital_write(true);
228 this->disable();
229}
230
231void ST7789V::write_data_(uint8_t value) {
232 this->dc_pin_->digital_write(true);
233 this->enable();
234 this->write_byte(value);
235 this->disable();
236}
237
238void ST7789V::write_addr_(uint16_t addr1, uint16_t addr2) {
239 static uint8_t byte[4];
240 byte[0] = (addr1 >> 8) & 0xFF;
241 byte[1] = addr1 & 0xFF;
242 byte[2] = (addr2 >> 8) & 0xFF;
243 byte[3] = addr2 & 0xFF;
244
245 this->dc_pin_->digital_write(true);
246 this->write_array(byte, 4);
247}
248
249void ST7789V::write_color_(uint16_t color, uint16_t size) {
250 static uint8_t byte[1024];
251 int index = 0;
252 for (int i = 0; i < size; i++) {
253 byte[index++] = (color >> 8) & 0xFF;
254 byte[index++] = color & 0xFF;
255 }
256
257 this->dc_pin_->digital_write(true);
258 write_array(byte, size * 2);
259}
260
262 if (this->eightbitcolor_) {
263 return size_t(this->get_width_internal()) * size_t(this->get_height_internal());
264 }
265 return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
266}
267
268// Draw a filled rectangle
269// x1: Start X coordinate
270// y1: Start Y coordinate
271// x2: End X coordinate
272// y2: End Y coordinate
273// color: color
274void ST7789V::draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
275 this->enable();
276 this->dc_pin_->digital_write(false);
277 this->write_byte(ST7789_CASET); // set column(x) address
278 this->dc_pin_->digital_write(true);
279 this->write_addr_(x1, x2);
280
281 this->dc_pin_->digital_write(false);
282 this->write_byte(ST7789_RASET); // set Page(y) address
283 this->dc_pin_->digital_write(true);
284 this->write_addr_(y1, y2);
285 this->dc_pin_->digital_write(false);
286 this->write_byte(ST7789_RAMWR); // begin a write to memory
287 this->dc_pin_->digital_write(true);
288 for (int i = x1; i <= x2; i++) {
289 uint16_t size = y2 - y1 + 1;
290 this->write_color_(color, size);
291 }
292 this->disable();
293}
294
296 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
297 return;
298
299 if (this->eightbitcolor_) {
300 auto color332 = display::ColorUtil::color_to_332(color);
301 uint32_t pos = (x + y * this->get_width_internal());
302 this->buffer_[pos] = color332;
303 } else {
304 auto color565 = display::ColorUtil::color_to_565(color);
305 uint32_t pos = (x + y * this->get_width_internal()) * 2;
306 this->buffer_[pos++] = (color565 >> 8) & 0xff;
307 this->buffer_[pos] = color565 & 0xff;
308 }
309}
310
311} // namespace st7789v
312} // 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:295
void backlight_(bool onoff)
Definition st7789v.cpp:216
void write_addr_(uint16_t addr1, uint16_t addr2)
Definition st7789v.cpp:238
void draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition st7789v.cpp:274
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:223
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:231
void write_color_(uint16_t color, uint16_t size)
Definition st7789v.cpp:249
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:60
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:31
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6