ESPHome 2025.6.3
Loading...
Searching...
No Matches
ili9xxx_display.cpp
Go to the documentation of this file.
1#include "ili9xxx_display.h"
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7namespace esphome {
8namespace ili9xxx {
9
10static const uint16_t SPI_SETUP_US = 100; // estimated fixed overhead in microseconds for an SPI write
11static const uint16_t SPI_MAX_BLOCK_SIZE = 4092; // Max size of continuous SPI transfer
12
13// store a 16 bit value in a buffer, big endian.
14static inline void put16_be(uint8_t *buf, uint16_t value) {
15 buf[0] = value >> 8;
16 buf[1] = value;
17}
18
20 // custom x/y transform and color order
21 uint8_t mad = this->color_order_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
22 if (this->swap_xy_)
23 mad |= MADCTL_MV;
24 if (this->mirror_x_)
25 mad |= MADCTL_MX;
26 if (this->mirror_y_)
27 mad |= MADCTL_MY;
28 this->command(ILI9XXX_MADCTL);
29 this->data(mad);
30 esph_log_d(TAG, "Wrote MADCTL 0x%02X", mad);
31}
32
34 ESP_LOGD(TAG, "Setting up ILI9xxx");
35
36 this->setup_pins_();
37 this->init_lcd_(this->init_sequence_);
38 this->init_lcd_(this->extra_init_sequence_.data());
39 switch (this->pixel_mode_) {
40 case PIXEL_MODE_16:
41 if (this->is_18bitdisplay_) {
42 this->command(ILI9XXX_PIXFMT);
43 this->data(0x55);
44 this->is_18bitdisplay_ = false;
45 }
46 break;
47 case PIXEL_MODE_18:
48 if (!this->is_18bitdisplay_) {
49 this->command(ILI9XXX_PIXFMT);
50 this->data(0x66);
51 this->is_18bitdisplay_ = true;
52 }
53 break;
54 default:
55 break;
56 }
57
58 this->set_madctl();
59 this->command(this->pre_invertcolors_ ? ILI9XXX_INVON : ILI9XXX_INVOFF);
60 this->x_low_ = this->width_;
61 this->y_low_ = this->height_;
62 this->x_high_ = 0;
63 this->y_high_ = 0;
64}
65
67 if (this->buffer_color_mode_ == BITS_16) {
68 this->init_internal_(this->get_buffer_length_() * 2);
69 } else {
71 }
72 if (this->buffer_ == nullptr) {
73 this->mark_failed();
74 }
75}
76
78 this->dc_pin_->setup(); // OUTPUT
79 this->dc_pin_->digital_write(false);
80 if (this->reset_pin_ != nullptr) {
81 this->reset_pin_->setup(); // OUTPUT
82 this->reset_pin_->digital_write(true);
83 }
84
85 this->spi_setup();
86
87 this->reset_();
88}
89
91 LOG_DISPLAY("", "ili9xxx", this);
92 ESP_LOGCONFIG(TAG,
93 " Width Offset: %u\n"
94 " Height Offset: %u",
95 this->offset_x_, this->offset_y_);
96 switch (this->buffer_color_mode_) {
97 case BITS_8_INDEXED:
98 ESP_LOGCONFIG(TAG, " Color mode: 8bit Indexed");
99 break;
100 case BITS_16:
101 ESP_LOGCONFIG(TAG, " Color mode: 16bit");
102 break;
103 default:
104 ESP_LOGCONFIG(TAG, " Color mode: 8bit 332 mode");
105 break;
106 }
107 if (this->is_18bitdisplay_) {
108 ESP_LOGCONFIG(TAG, " 18-Bit Mode: YES");
109 }
110 ESP_LOGCONFIG(TAG, " Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000));
111
112 LOG_PIN(" Reset Pin: ", this->reset_pin_);
113 LOG_PIN(" CS Pin: ", this->cs_);
114 LOG_PIN(" DC Pin: ", this->dc_pin_);
115 LOG_PIN(" Busy Pin: ", this->busy_pin_);
116 ESP_LOGCONFIG(TAG,
117 " Color order: %s\n"
118 " Swap_xy: %s\n"
119 " Mirror_x: %s\n"
120 " Mirror_y: %s\n"
121 " Invert colors: %s",
122 this->color_order_ == display::COLOR_ORDER_BGR ? "BGR" : "RGB", YESNO(this->swap_xy_),
123 YESNO(this->mirror_x_), YESNO(this->mirror_y_), YESNO(this->pre_invertcolors_));
124
125 if (this->is_failed()) {
126 ESP_LOGCONFIG(TAG, " => Failed to init Memory: YES!");
127 }
128 LOG_UPDATE_INTERVAL(this);
129}
130
132
134 if (!this->check_buffer_())
135 return;
136 uint16_t new_color = 0;
137 this->x_low_ = 0;
138 this->y_low_ = 0;
139 this->x_high_ = this->get_width_internal() - 1;
140 this->y_high_ = this->get_height_internal() - 1;
141 switch (this->buffer_color_mode_) {
142 case BITS_8_INDEXED:
144 break;
145 case BITS_16:
146 new_color = display::ColorUtil::color_to_565(color);
147 {
148 const uint32_t buffer_length_16_bits = this->get_buffer_length_() * 2;
149 if (((uint8_t) (new_color >> 8)) == ((uint8_t) new_color)) {
150 // Upper and lower is equal can use quicker memset operation. Takes ~20ms.
151 memset(this->buffer_, (uint8_t) new_color, buffer_length_16_bits);
152 } else {
153 for (uint32_t i = 0; i < buffer_length_16_bits; i = i + 2) {
154 this->buffer_[i] = (uint8_t) (new_color >> 8);
155 this->buffer_[i + 1] = (uint8_t) new_color;
156 }
157 }
158 }
159 return;
160 default:
162 break;
163 }
164 memset(this->buffer_, (uint8_t) new_color, this->get_buffer_length_());
165}
166
168 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
169 return;
170 }
171 if (!this->check_buffer_())
172 return;
173 uint32_t pos = (y * width_) + x;
174 uint16_t new_color;
175 bool updated = false;
176 switch (this->buffer_color_mode_) {
177 case BITS_8_INDEXED:
179 break;
180 case BITS_16:
181 pos = pos * 2;
183 if (this->buffer_[pos] != (uint8_t) (new_color >> 8)) {
184 this->buffer_[pos] = (uint8_t) (new_color >> 8);
185 updated = true;
186 }
187 pos = pos + 1;
188 new_color = new_color & 0xFF;
189 break;
190 default:
192 break;
193 }
194
195 if (this->buffer_[pos] != new_color) {
196 this->buffer_[pos] = new_color;
197 updated = true;
198 }
199 if (updated) {
200 // low and high watermark may speed up drawing from buffer
202 this->x_low_ = x;
204 this->y_low_ = y;
205 if (x > this->x_high_)
206 this->x_high_ = x;
207 if (y > this->y_high_)
208 this->y_high_ = y;
209 }
210}
211
213 if (this->prossing_update_) {
214 this->need_update_ = true;
215 return;
216 }
217 this->prossing_update_ = true;
218 do {
219 this->need_update_ = false;
220 this->do_update_();
221 } while (this->need_update_);
222 this->prossing_update_ = false;
223 this->display_();
224}
225
227 // check if something was displayed
228 if ((this->x_high_ < this->x_low_) || (this->y_high_ < this->y_low_)) {
229 return;
230 }
231
232 // we will only update the changed rows to the display
233 size_t const w = this->x_high_ - this->x_low_ + 1;
234 size_t const h = this->y_high_ - this->y_low_ + 1;
235
236 size_t mhz = this->data_rate_ / 1000000;
237 // estimate time for a single write
238 size_t sw_time = this->width_ * h * 16 / mhz + this->width_ * h * 2 / SPI_MAX_BLOCK_SIZE * SPI_SETUP_US * 2;
239 // estimate time for multiple writes
240 size_t mw_time = (w * h * 16) / mhz + w * h * 2 / ILI9XXX_TRANSFER_BUFFER_SIZE * SPI_SETUP_US;
241 ESP_LOGV(TAG,
242 "Start display(xlow:%d, ylow:%d, xhigh:%d, yhigh:%d, width:%d, "
243 "height:%zu, mode=%d, 18bit=%d, sw_time=%zuus, mw_time=%zuus)",
244 this->x_low_, this->y_low_, this->x_high_, this->y_high_, w, h, this->buffer_color_mode_,
245 this->is_18bitdisplay_, sw_time, mw_time);
246 auto now = millis();
247 if (this->buffer_color_mode_ == BITS_16 && !this->is_18bitdisplay_ && sw_time < mw_time) {
248 // 16 bit mode maps directly to display format
249 ESP_LOGV(TAG, "Doing single write of %zu bytes", this->width_ * h * 2);
250 set_addr_window_(0, this->y_low_, this->width_ - 1, this->y_high_);
251 this->write_array(this->buffer_ + this->y_low_ * this->width_ * 2, h * this->width_ * 2);
252 } else {
253 ESP_LOGV(TAG, "Doing multiple write");
254 uint8_t transfer_buffer[ILI9XXX_TRANSFER_BUFFER_SIZE];
255 size_t rem = h * w; // remaining number of pixels to write
256 set_addr_window_(this->x_low_, this->y_low_, this->x_high_, this->y_high_);
257 size_t idx = 0; // index into transfer_buffer
258 size_t pixel = 0; // pixel number offset
259 size_t pos = this->y_low_ * this->width_ + this->x_low_;
260 while (rem-- != 0) {
261 uint16_t color_val;
262 switch (this->buffer_color_mode_) {
263 case BITS_8:
265 break;
266 case BITS_8_INDEXED:
269 break;
270 default: // case BITS_16:
271 color_val = (this->buffer_[pos * 2] << 8) + this->buffer_[pos * 2 + 1];
272 pos++;
273 break;
274 }
275 if (this->is_18bitdisplay_) {
276 transfer_buffer[idx++] = (uint8_t) ((color_val & 0xF800) >> 8); // Blue
277 transfer_buffer[idx++] = (uint8_t) ((color_val & 0x7E0) >> 3); // Green
278 transfer_buffer[idx++] = (uint8_t) (color_val << 3); // Red
279 } else {
280 put16_be(transfer_buffer + idx, color_val);
281 idx += 2;
282 }
283 if (idx == sizeof(transfer_buffer)) {
284 this->write_array(transfer_buffer, idx);
285 idx = 0;
286 App.feed_wdt();
287 }
288 // end of line? Skip to the next.
289 if (++pixel == w) {
290 pixel = 0;
291 pos += this->width_ - w;
292 }
293 }
294 // flush any balance.
295 if (idx != 0) {
296 this->write_array(transfer_buffer, idx);
297 }
298 }
299 this->end_data_();
300 ESP_LOGV(TAG, "Data write took %dms", (unsigned) (millis() - now));
301 // invalidate watermarks
302 this->x_low_ = this->width_;
303 this->y_low_ = this->height_;
304 this->x_high_ = 0;
305 this->y_high_ = 0;
306}
307
308// note that this bypasses the buffer and writes directly to the display.
309void ILI9XXXDisplay::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr,
310 display::ColorOrder order, display::ColorBitness bitness, bool big_endian,
311 int x_offset, int y_offset, int x_pad) {
312 if (w <= 0 || h <= 0)
313 return;
314 // if color mapping or software rotation is required, hand this off to the parent implementation. This will
315 // do color conversion pixel-by-pixel into the buffer and draw it later. If this is happening the user has not
316 // configured the renderer well.
317 if (this->rotation_ != display::DISPLAY_ROTATION_0_DEGREES || bitness != display::COLOR_BITNESS_565 || !big_endian) {
318 display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
319 x_pad);
320 return;
321 }
322 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
323 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
324 auto stride = x_offset + w + x_pad;
325 if (!this->is_18bitdisplay_) {
326 if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
327 // we could deal here with a non-zero y_offset, but if x_offset is zero, y_offset probably will be so don't bother
328 this->write_array(ptr, w * h * 2);
329 } else {
330 for (size_t y = 0; y != h; y++) {
331 this->write_array(ptr + (y + y_offset) * stride + x_offset, w * 2);
332 }
333 }
334 } else {
335 // 18 bit mode
336 uint8_t transfer_buffer[ILI9XXX_TRANSFER_BUFFER_SIZE * 4];
337 ESP_LOGV(TAG, "Doing multiple write");
338 size_t rem = h * w; // remaining number of pixels to write
339 size_t idx = 0; // index into transfer_buffer
340 size_t pixel = 0; // pixel number offset
341 ptr += (y_offset * stride + x_offset) * 2;
342 while (rem-- != 0) {
343 uint8_t hi_byte = *ptr++;
344 uint8_t lo_byte = *ptr++;
345 transfer_buffer[idx++] = hi_byte & 0xF8; // Blue
346 transfer_buffer[idx++] = ((hi_byte << 5) | (lo_byte) >> 5); // Green
347 transfer_buffer[idx++] = lo_byte << 3; // Red
348 if (idx == sizeof(transfer_buffer)) {
349 this->write_array(transfer_buffer, idx);
350 idx = 0;
351 App.feed_wdt();
352 }
353 // end of line? Skip to the next.
354 if (++pixel == w) {
355 pixel = 0;
356 ptr += (x_pad + x_offset) * 2;
357 }
358 }
359 // flush any balance.
360 if (idx != 0) {
361 this->write_array(transfer_buffer, idx);
362 }
363 }
364 this->end_data_();
365}
366
367// should return the total size: return this->get_width_internal() * this->get_height_internal() * 2 // 16bit color
368// values per bit is huge
370
371void ILI9XXXDisplay::command(uint8_t value) {
372 this->start_command_();
373 this->write_byte(value);
374 this->end_command_();
375}
376
377void ILI9XXXDisplay::data(uint8_t value) {
378 this->start_data_();
379 this->write_byte(value);
380 this->end_data_();
381}
382
383void ILI9XXXDisplay::send_command(uint8_t command_byte, const uint8_t *data_bytes, uint8_t num_data_bytes) {
384 this->command(command_byte); // Send the command byte
385 this->start_data_();
386 this->write_array(data_bytes, num_data_bytes);
387 this->end_data_();
388}
389
391 this->dc_pin_->digital_write(false);
392 this->enable();
393}
395 this->dc_pin_->digital_write(true);
396 this->enable();
397}
398
401
403 if (this->reset_pin_ != nullptr) {
404 this->reset_pin_->digital_write(false);
405 delay(20);
406 this->reset_pin_->digital_write(true);
407 delay(20);
408 }
409}
410
411void ILI9XXXDisplay::init_lcd_(const uint8_t *addr) {
412 if (addr == nullptr)
413 return;
414 uint8_t cmd, x, num_args;
415 while ((cmd = *addr++) != 0) {
416 x = *addr++;
417 if (x == ILI9XXX_DELAY_FLAG) {
418 cmd &= 0x7F;
419 ESP_LOGV(TAG, "Delay %dms", cmd);
420 delay(cmd);
421 } else {
422 num_args = x & 0x7F;
423 ESP_LOGV(TAG, "Command %02X, length %d, bits %02X", cmd, num_args, *addr);
424 this->send_command(cmd, addr, num_args);
425 addr += num_args;
426 if (x & 0x80) {
427 ESP_LOGV(TAG, "Delay 150ms");
428 delay(150); // NOLINT
429 }
430 }
431 }
432}
433
434// Tell the display controller where we want to draw pixels.
435void ILI9XXXDisplay::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
436 x1 += this->offset_x_;
437 x2 += this->offset_x_;
438 y1 += this->offset_y_;
439 y2 += this->offset_y_;
440 this->command(ILI9XXX_CASET);
441 this->data(x1 >> 8);
442 this->data(x1 & 0xFF);
443 this->data(x2 >> 8);
444 this->data(x2 & 0xFF);
445 this->command(ILI9XXX_PASET); // Page address set
446 this->data(y1 >> 8);
447 this->data(y1 & 0xFF);
448 this->data(y2 >> 8);
449 this->data(y2 & 0xFF);
450 this->command(ILI9XXX_RAMWR); // Write to RAM
451 this->start_data_();
452}
453
455 this->pre_invertcolors_ = invert;
456 if (is_ready()) {
457 this->command(invert ? ILI9XXX_INVON : ILI9XXX_INVOFF);
458 }
459}
460
463
464} // namespace ili9xxx
465} // namespace esphome
uint8_t h
Definition bl0906.h:2
void feed_wdt(uint32_t time=0)
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool is_ready() const
virtual void setup()=0
virtual void digital_write(bool value)=0
static Color rgb332_to_color(uint8_t rgb332_color)
static uint8_t color_to_index8_palette888(Color color, const uint8_t *palette)
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
static Color index8_to_color_palette888(uint8_t index, const uint8_t *palette)
static uint8_t color_to_332(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void init_internal_(uint32_t buffer_length)
DisplayRotation rotation_
Definition display.h:682
virtual void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorOrder order, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Given an array of pixels encoded in the nominated format, draw these into the display's buffer.
Definition display.cpp:54
virtual void data(uint8_t value)
int16_t width_
Display width as modified by current rotation.
std::vector< uint8_t > extra_init_sequence_
void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
float get_setup_priority() const override
void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2)
void init_lcd_(const uint8_t *addr)
virtual void command(uint8_t value)
void draw_absolute_pixel_internal(int x, int y, Color color) override
void fill(Color color) override
void send_command(uint8_t command_byte, const uint8_t *data_bytes, uint8_t num_data_bytes)
int16_t height_
Display height as modified by current rotation.
uint32_t data_rate_
Definition spi.h:406
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:135
const size_t ILI9XXX_TRANSFER_BUFFER_SIZE
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:19
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
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6