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