ESPHome  2024.4.1
ili9xxx_display.cpp
Go to the documentation of this file.
1 #include "ili9xxx_display.h"
3 #include "esphome/core/hal.h"
4 #include "esphome/core/helpers.h"
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace ili9xxx {
9 
10 static const uint16_t SPI_SETUP_US = 100; // estimated fixed overhead in microseconds for an SPI write
11 static 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.
14 static 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_();
38 
39  this->set_madctl();
40  this->command(this->pre_invertcolors_ ? ILI9XXX_INVON : ILI9XXX_INVOFF);
41  this->x_low_ = this->width_;
42  this->y_low_ = this->height_;
43  this->x_high_ = 0;
44  this->y_high_ = 0;
45 }
46 
48  if (this->buffer_color_mode_ == BITS_16) {
49  this->init_internal_(this->get_buffer_length_() * 2);
50  if (this->buffer_ != nullptr) {
51  return;
52  }
53  this->buffer_color_mode_ = BITS_8;
54  }
55  this->init_internal_(this->get_buffer_length_());
56  if (this->buffer_ == nullptr) {
57  this->mark_failed();
58  }
59 }
60 
62  this->dc_pin_->setup(); // OUTPUT
63  this->dc_pin_->digital_write(false);
64  if (this->reset_pin_ != nullptr) {
65  this->reset_pin_->setup(); // OUTPUT
66  this->reset_pin_->digital_write(true);
67  }
68 
69  this->spi_setup();
70 
71  this->reset_();
72 }
73 
75  LOG_DISPLAY("", "ili9xxx", this);
76  ESP_LOGCONFIG(TAG, " Width Offset: %u", this->offset_x_);
77  ESP_LOGCONFIG(TAG, " Height Offset: %u", this->offset_y_);
78  switch (this->buffer_color_mode_) {
79  case BITS_8_INDEXED:
80  ESP_LOGCONFIG(TAG, " Color mode: 8bit Indexed");
81  break;
82  case BITS_16:
83  ESP_LOGCONFIG(TAG, " Color mode: 16bit");
84  break;
85  default:
86  ESP_LOGCONFIG(TAG, " Color mode: 8bit 332 mode");
87  break;
88  }
89  if (this->is_18bitdisplay_) {
90  ESP_LOGCONFIG(TAG, " 18-Bit Mode: YES");
91  }
92  ESP_LOGCONFIG(TAG, " Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000));
93 
94  LOG_PIN(" Reset Pin: ", this->reset_pin_);
95  LOG_PIN(" CS Pin: ", this->cs_);
96  LOG_PIN(" DC Pin: ", this->dc_pin_);
97  LOG_PIN(" Busy Pin: ", this->busy_pin_);
98  ESP_LOGCONFIG(TAG, " Color order: %s", this->color_order_ == display::COLOR_ORDER_BGR ? "BGR" : "RGB");
99  ESP_LOGCONFIG(TAG, " Swap_xy: %s", YESNO(this->swap_xy_));
100  ESP_LOGCONFIG(TAG, " Mirror_x: %s", YESNO(this->mirror_x_));
101  ESP_LOGCONFIG(TAG, " Mirror_y: %s", YESNO(this->mirror_y_));
102 
103  if (this->is_failed()) {
104  ESP_LOGCONFIG(TAG, " => Failed to init Memory: YES!");
105  }
106  LOG_UPDATE_INTERVAL(this);
107 }
108 
110 
112  if (!this->check_buffer_())
113  return;
114  uint16_t new_color = 0;
115  this->x_low_ = 0;
116  this->y_low_ = 0;
117  this->x_high_ = this->get_width_internal() - 1;
118  this->y_high_ = this->get_height_internal() - 1;
119  switch (this->buffer_color_mode_) {
120  case BITS_8_INDEXED:
122  break;
123  case BITS_16:
124  new_color = display::ColorUtil::color_to_565(color);
125  {
126  const uint32_t buffer_length_16_bits = this->get_buffer_length_() * 2;
127  if (((uint8_t) (new_color >> 8)) == ((uint8_t) new_color)) {
128  // Upper and lower is equal can use quicker memset operation. Takes ~20ms.
129  memset(this->buffer_, (uint8_t) new_color, buffer_length_16_bits);
130  } else {
131  for (uint32_t i = 0; i < buffer_length_16_bits; i = i + 2) {
132  this->buffer_[i] = (uint8_t) (new_color >> 8);
133  this->buffer_[i + 1] = (uint8_t) new_color;
134  }
135  }
136  }
137  return;
138  break;
139  default:
141  break;
142  }
143  memset(this->buffer_, (uint8_t) new_color, this->get_buffer_length_());
144 }
145 
147  if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
148  return;
149  }
150  if (!this->check_buffer_())
151  return;
152  uint32_t pos = (y * width_) + x;
153  uint16_t new_color;
154  bool updated = false;
155  switch (this->buffer_color_mode_) {
156  case BITS_8_INDEXED:
158  break;
159  case BITS_16:
160  pos = pos * 2;
162  if (this->buffer_[pos] != (uint8_t) (new_color >> 8)) {
163  this->buffer_[pos] = (uint8_t) (new_color >> 8);
164  updated = true;
165  }
166  pos = pos + 1;
167  new_color = new_color & 0xFF;
168  break;
169  default:
171  break;
172  }
173 
174  if (this->buffer_[pos] != new_color) {
175  this->buffer_[pos] = new_color;
176  updated = true;
177  }
178  if (updated) {
179  // low and high watermark may speed up drawing from buffer
180  if (x < this->x_low_)
181  this->x_low_ = x;
182  if (y < this->y_low_)
183  this->y_low_ = y;
184  if (x > this->x_high_)
185  this->x_high_ = x;
186  if (y > this->y_high_)
187  this->y_high_ = y;
188  }
189 }
190 
192  if (this->prossing_update_) {
193  this->need_update_ = true;
194  return;
195  }
196  this->prossing_update_ = true;
197  do {
198  this->need_update_ = false;
199  this->do_update_();
200  } while (this->need_update_);
201  this->prossing_update_ = false;
202  this->display_();
203 }
204 
206  uint8_t transfer_buffer[ILI9XXX_TRANSFER_BUFFER_SIZE];
207  // check if something was displayed
208  if ((this->x_high_ < this->x_low_) || (this->y_high_ < this->y_low_)) {
209  return;
210  }
211 
212  // we will only update the changed rows to the display
213  size_t const w = this->x_high_ - this->x_low_ + 1;
214  size_t const h = this->y_high_ - this->y_low_ + 1;
215 
216  size_t mhz = this->data_rate_ / 1000000;
217  // estimate time for a single write
218  size_t sw_time = this->width_ * h * 16 / mhz + this->width_ * h * 2 / SPI_MAX_BLOCK_SIZE * SPI_SETUP_US * 2;
219  // estimate time for multiple writes
220  size_t mw_time = (w * h * 16) / mhz + w * h * 2 / ILI9XXX_TRANSFER_BUFFER_SIZE * SPI_SETUP_US;
221  ESP_LOGV(TAG,
222  "Start display(xlow:%d, ylow:%d, xhigh:%d, yhigh:%d, width:%d, "
223  "height:%zu, mode=%d, 18bit=%d, sw_time=%zuus, mw_time=%zuus)",
224  this->x_low_, this->y_low_, this->x_high_, this->y_high_, w, h, this->buffer_color_mode_,
225  this->is_18bitdisplay_, sw_time, mw_time);
226  auto now = millis();
227  if (this->buffer_color_mode_ == BITS_16 && !this->is_18bitdisplay_ && sw_time < mw_time) {
228  // 16 bit mode maps directly to display format
229  ESP_LOGV(TAG, "Doing single write of %zu bytes", this->width_ * h * 2);
230  set_addr_window_(0, this->y_low_, this->width_ - 1, this->y_high_);
231  this->write_array(this->buffer_ + this->y_low_ * this->width_ * 2, h * this->width_ * 2);
232  } else {
233  ESP_LOGV(TAG, "Doing multiple write");
234  size_t rem = h * w; // remaining number of pixels to write
235  set_addr_window_(this->x_low_, this->y_low_, this->x_high_, this->y_high_);
236  size_t idx = 0; // index into transfer_buffer
237  size_t pixel = 0; // pixel number offset
238  size_t pos = this->y_low_ * this->width_ + this->x_low_;
239  while (rem-- != 0) {
240  uint16_t color_val;
241  switch (this->buffer_color_mode_) {
242  case BITS_8:
244  break;
245  case BITS_8_INDEXED:
248  break;
249  default: // case BITS_16:
250  color_val = (buffer_[pos * 2] << 8) + buffer_[pos * 2 + 1];
251  pos++;
252  break;
253  }
254  if (this->is_18bitdisplay_) {
255  transfer_buffer[idx++] = (uint8_t) ((color_val & 0xF800) >> 8); // Blue
256  transfer_buffer[idx++] = (uint8_t) ((color_val & 0x7E0) >> 3); // Green
257  transfer_buffer[idx++] = (uint8_t) (color_val << 3); // Red
258  } else {
259  put16_be(transfer_buffer + idx, color_val);
260  idx += 2;
261  }
262  if (idx == ILI9XXX_TRANSFER_BUFFER_SIZE) {
263  this->write_array(transfer_buffer, idx);
264  idx = 0;
265  App.feed_wdt();
266  }
267  // end of line? Skip to the next.
268  if (++pixel == w) {
269  pixel = 0;
270  pos += this->width_ - w;
271  }
272  }
273  // flush any balance.
274  if (idx != 0) {
275  this->write_array(transfer_buffer, idx);
276  }
277  }
278  this->end_data_();
279  ESP_LOGV(TAG, "Data write took %dms", (unsigned) (millis() - now));
280  // invalidate watermarks
281  this->x_low_ = this->width_;
282  this->y_low_ = this->height_;
283  this->x_high_ = 0;
284  this->y_high_ = 0;
285 }
286 
287 // note that this bypasses the buffer and writes directly to the display.
288 void ILI9XXXDisplay::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr,
289  display::ColorOrder order, display::ColorBitness bitness, bool big_endian,
290  int x_offset, int y_offset, int x_pad) {
291  if (w <= 0 || h <= 0)
292  return;
293  // if color mapping or software rotation is required, hand this off to the parent implementation. This will
294  // do color conversion pixel-by-pixel into the buffer and draw it later. If this is happening the user has not
295  // configured the renderer well.
296  if (this->rotation_ != display::DISPLAY_ROTATION_0_DEGREES || bitness != display::COLOR_BITNESS_565 || !big_endian ||
297  this->is_18bitdisplay_) {
298  return display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
299  x_pad);
300  }
301  this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
302  // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
303  if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
304  // 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
305  this->write_array(ptr, w * h * 2);
306  } else {
307  auto stride = x_offset + w + x_pad;
308  for (size_t y = 0; y != h; y++) {
309  this->write_array(ptr + (y + y_offset) * stride + x_offset, w * 2);
310  }
311  }
312  this->end_data_();
313 }
314 
315 // should return the total size: return this->get_width_internal() * this->get_height_internal() * 2 // 16bit color
316 // values per bit is huge
318 
319 void ILI9XXXDisplay::command(uint8_t value) {
320  this->start_command_();
321  this->write_byte(value);
322  this->end_command_();
323 }
324 
325 void ILI9XXXDisplay::data(uint8_t value) {
326  this->start_data_();
327  this->write_byte(value);
328  this->end_data_();
329 }
330 
331 void ILI9XXXDisplay::send_command(uint8_t command_byte, const uint8_t *data_bytes, uint8_t num_data_bytes) {
332  this->command(command_byte); // Send the command byte
333  this->start_data_();
334  this->write_array(data_bytes, num_data_bytes);
335  this->end_data_();
336 }
337 
339  this->dc_pin_->digital_write(false);
340  this->enable();
341 }
343  this->dc_pin_->digital_write(true);
344  this->enable();
345 }
346 
349 
351  if (this->reset_pin_ != nullptr) {
352  this->reset_pin_->digital_write(false);
353  delay(20);
354  this->reset_pin_->digital_write(true);
355  delay(20);
356  }
357 }
358 
360  uint8_t cmd, x, num_args;
361  const uint8_t *addr = this->init_sequence_;
362  while ((cmd = *addr++) > 0) {
363  x = *addr++;
364  num_args = x & 0x7F;
365  this->send_command(cmd, addr, num_args);
366  addr += num_args;
367  if (x & 0x80)
368  delay(150); // NOLINT
369  }
370 }
371 
372 // Tell the display controller where we want to draw pixels.
373 void ILI9XXXDisplay::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
374  x1 += this->offset_x_;
375  x2 += this->offset_x_;
376  y1 += this->offset_y_;
377  y2 += this->offset_y_;
378  this->command(ILI9XXX_CASET);
379  this->data(x1 >> 8);
380  this->data(x1 & 0xFF);
381  this->data(x2 >> 8);
382  this->data(x2 & 0xFF);
383  this->command(ILI9XXX_PASET); // Page address set
384  this->data(y1 >> 8);
385  this->data(y1 & 0xFF);
386  this->data(y2 >> 8);
387  this->data(y2 & 0xFF);
388  this->command(ILI9XXX_RAMWR); // Write to RAM
389  this->start_data_();
390 }
391 
392 void ILI9XXXDisplay::invert_colors(bool invert) {
393  this->pre_invertcolors_ = invert;
394  if (is_ready()) {
395  this->command(invert ? ILI9XXX_INVON : ILI9XXX_INVOFF);
396  }
397 }
398 
401 
402 } // namespace ili9xxx
403 } // namespace esphome
void send_command(uint8_t command_byte, const uint8_t *data_bytes, uint8_t num_data_bytes)
virtual void digital_write(bool value)=0
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2)
uint16_t x
Definition: tt21100.cpp:17
GPIOPin * cs_
Definition: spi.h:395
virtual void setup()=0
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
uint16_t y
Definition: tt21100.cpp:18
int16_t width_
Display width as modified by current rotation.
void init_internal_(uint32_t buffer_length)
void draw_absolute_pixel_internal(int x, int y, Color color) override
int16_t height_
Display height as modified by current rotation.
static uint8_t color_to_332(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
float get_setup_priority() const override
static Color index8_to_color_palette888(uint8_t index, const uint8_t *palette)
const size_t ILI9XXX_TRANSFER_BUFFER_SIZE
Application App
Global storage of Application pointer - only one Application can exist.
virtual void data(uint8_t value)
uint32_t data_rate_
Definition: spi.h:393
DisplayRotation rotation_
Definition: display.h:655
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
static uint8_t color_to_index8_palette888(Color color, const uint8_t *palette)
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
uint8_t h
Definition: bl0939.h:21
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
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
void fill(Color color) override
static Color rgb332_to_color(uint8_t rgb332_color)
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&#39;s buffer...
Definition: display.cpp:54
stm32_cmd_t * cmd
Definition: stm32flash.h:96
virtual void command(uint8_t value)
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26