ESPHome  2025.2.0
ili9xxx_display.h
Go to the documentation of this file.
1 #pragma once
5 #include "ili9xxx_defines.h"
6 #include "ili9xxx_init.h"
7 
8 namespace esphome {
9 namespace ili9xxx {
10 
11 static const char *const TAG = "ili9xxx";
12 const size_t ILI9XXX_TRANSFER_BUFFER_SIZE = 126; // ensure this is divisible by 6
13 
15  BITS_8 = 0x08,
17  BITS_16 = 0x10,
18 };
19 
20 enum PixelMode {
24 };
25 
27  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
28  spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_40MHZ> {
29  public:
30  ILI9XXXDisplay() = default;
31  ILI9XXXDisplay(uint8_t const *init_sequence, int16_t width, int16_t height)
32  : init_sequence_{init_sequence}, width_{width}, height_{height} {
33  uint8_t cmd, num_args, bits;
34  const uint8_t *addr = init_sequence;
35  while ((cmd = *addr++) != 0) {
36  num_args = *addr++;
37  if (num_args == ILI9XXX_DELAY_FLAG)
38  continue;
39  bits = *addr;
40  switch (cmd) {
41  case ILI9XXX_MADCTL: {
42  this->swap_xy_ = (bits & MADCTL_MV) != 0;
43  this->mirror_x_ = (bits & MADCTL_MX) != 0;
44  this->mirror_y_ = (bits & MADCTL_MY) != 0;
45  this->color_order_ = (bits & MADCTL_BGR) ? display::COLOR_ORDER_BGR : display::COLOR_ORDER_RGB;
46  break;
47  }
48 
49  case ILI9XXX_PIXFMT: {
50  if ((bits & 0xF) == 6)
51  this->is_18bitdisplay_ = true;
52  break;
53  }
54 
55  default:
56  break;
57  }
58  addr += (num_args & 0x7F);
59  }
60  }
61 
62  void add_init_sequence(const std::vector<uint8_t> &sequence) { this->extra_init_sequence_ = sequence; }
63  void set_dc_pin(GPIOPin *dc_pin) { dc_pin_ = dc_pin; }
64  float get_setup_priority() const override;
66  void set_palette(const uint8_t *palette) { this->palette_ = palette; }
67  void set_buffer_color_mode(ILI9XXXColorMode color_mode) { this->buffer_color_mode_ = color_mode; }
68  void set_dimensions(int16_t width, int16_t height) {
69  this->height_ = height;
70  this->width_ = width;
71  }
72  void set_offsets(int16_t offset_x, int16_t offset_y) {
73  this->offset_x_ = offset_x;
74  this->offset_y_ = offset_y;
75  }
76  void invert_colors(bool invert);
77  virtual void command(uint8_t value);
78  virtual void data(uint8_t value);
79  void send_command(uint8_t command_byte, const uint8_t *data_bytes, uint8_t num_data_bytes);
80  void set_color_order(display::ColorOrder color_order) { this->color_order_ = color_order; }
81  void set_swap_xy(bool swap_xy) { this->swap_xy_ = swap_xy; }
82  void set_mirror_x(bool mirror_x) { this->mirror_x_ = mirror_x; }
83  void set_mirror_y(bool mirror_y) { this->mirror_y_ = mirror_y; }
85 
86  void update() override;
87 
88  void fill(Color color) override;
89 
90  void dump_config() override;
91  void setup() override;
92  void on_shutdown() override { this->command(ILI9XXX_SLPIN); }
93 
95  void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
96  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override;
97 
98  protected:
99  inline bool check_buffer_() {
100  if (this->buffer_ == nullptr) {
101  if (!this->is_failed())
102  this->alloc_buffer_();
103  return !this->is_failed();
104  }
105  return true;
106  }
107 
108  void draw_absolute_pixel_internal(int x, int y, Color color) override;
109  void setup_pins_();
110 
111  virtual void set_madctl();
112  void display_();
113  void init_lcd_(const uint8_t *addr);
114  void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2);
115  void reset_();
116 
117  uint8_t const *init_sequence_{};
118  std::vector<uint8_t> extra_init_sequence_;
119  int16_t width_{0};
120  int16_t height_{0};
121  int16_t offset_x_{0};
122  int16_t offset_y_{0};
123  uint16_t x_low_{0};
124  uint16_t y_low_{0};
125  uint16_t x_high_{0};
126  uint16_t y_high_{0};
127  const uint8_t *palette_{};
128 
130 
131  uint32_t get_buffer_length_();
132  int get_width_internal() override;
133  int get_height_internal() override;
134 
135  void start_command_();
136  void end_command_();
137  void start_data_();
138  void end_data_();
139  void alloc_buffer_();
140 
141  GPIOPin *reset_pin_{nullptr};
142  GPIOPin *dc_pin_{nullptr};
143  GPIOPin *busy_pin_{nullptr};
144 
145  bool prossing_update_ = false;
146  bool need_update_ = false;
147  bool is_18bitdisplay_ = false;
151  bool swap_xy_{};
152  bool mirror_x_{};
153  bool mirror_y_{};
154 };
155 
156 //----------- M5Stack display --------------
158  public:
159  ILI9XXXM5Stack() : ILI9XXXDisplay(INITCMD_M5STACK, 320, 240) {}
160 };
161 
162 //----------- M5Stack display --------------
164  public:
165  ILI9XXXM5CORE() : ILI9XXXDisplay(INITCMD_M5CORE, 320, 240) {}
166 };
167 
168 //----------- ST7789V display --------------
170  public:
171  ILI9XXXST7789V() : ILI9XXXDisplay(INITCMD_ST7789V, 240, 320) {}
172 };
173 
174 //----------- ILI9XXX_24_TFT display --------------
176  public:
177  ILI9XXXILI9341() : ILI9XXXDisplay(INITCMD_ILI9341, 240, 320) {}
178 };
179 
180 //----------- ILI9XXX_24_TFT rotated display --------------
182  public:
183  ILI9XXXILI9342() : ILI9XXXDisplay(INITCMD_ILI9341, 320, 240) {}
184 };
185 
186 //----------- ILI9XXX_??_TFT rotated display --------------
188  public:
189  ILI9XXXILI9481() : ILI9XXXDisplay(INITCMD_ILI9481, 480, 320) {}
190 };
191 
192 //----------- ILI9481 in 18 bit mode --------------
194  public:
195  ILI9XXXILI948118() : ILI9XXXDisplay(INITCMD_ILI9481_18, 320, 480) {}
196 };
197 
198 //----------- ILI9XXX_35_TFT rotated display --------------
200  public:
201  ILI9XXXILI9486() : ILI9XXXDisplay(INITCMD_ILI9486, 480, 320) {}
202 };
203 
205  public:
206  ILI9XXXILI9488(const uint8_t *seq = INITCMD_ILI9488) : ILI9XXXDisplay(seq, 480, 320) {}
207 
208  protected:
209  void set_madctl() override {
210  uint8_t mad = this->color_order_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
211  uint8_t dfun = 0x22;
212  this->width_ = 320;
213  this->height_ = 480;
214  if (!(this->swap_xy_ || this->mirror_x_ || this->mirror_y_)) {
215  // no transforms
216  } else if (this->mirror_y_ && this->mirror_x_) {
217  // rotate 180
218  dfun = 0x42;
219  } else if (this->swap_xy_) {
220  this->width_ = 480;
221  this->height_ = 320;
222  mad |= 0x20;
223  if (this->mirror_x_) {
224  dfun = 0x02;
225  } else {
226  dfun = 0x62;
227  }
228  }
229  this->command(ILI9XXX_DFUNCTR);
230  this->data(0);
231  this->data(dfun);
232  this->command(ILI9XXX_MADCTL);
233  this->data(mad);
234  }
235 };
236 //----------- Waveshare 3.5 Res Touch - ILI9488 interfaced via 16 bit shift register to parallel */
238  public:
239  WAVESHARERES35() : ILI9XXXILI9488(INITCMD_WAVESHARE_RES_3_5) {}
240  void data(uint8_t value) override {
241  this->start_data_();
242  this->write_byte(0);
243  this->write_byte(value);
244  this->end_data_();
245  }
246 };
247 
248 //----------- ILI9XXX_35_TFT origin colors rotated display --------------
250  public:
251  ILI9XXXILI9488A() : ILI9XXXDisplay(INITCMD_ILI9488_A, 480, 320) {}
252 };
253 
254 //----------- ILI9XXX_35_TFT rotated display --------------
256  public:
257  ILI9XXXST7796() : ILI9XXXDisplay(INITCMD_ST7796, 320, 480) {}
258 };
259 
260 class ILI9XXXS3Box : public ILI9XXXDisplay {
261  public:
262  ILI9XXXS3Box() : ILI9XXXDisplay(INITCMD_S3BOX, 320, 240) {}
263 };
264 
266  public:
267  ILI9XXXS3BoxLite() : ILI9XXXDisplay(INITCMD_S3BOXLITE, 320, 240) {}
268 };
269 
271  public:
272  ILI9XXXGC9A01A() : ILI9XXXDisplay(INITCMD_GC9A01A, 240, 240) {}
273 };
274 
275 //----------- ILI9XXX_24_TFT display --------------
277  public:
278  ILI9XXXST7735() : ILI9XXXDisplay(INITCMD_ST7735, 128, 160) {}
279 };
280 
281 } // namespace ili9xxx
282 } // namespace esphome
void send_command(uint8_t command_byte, const uint8_t *data_bytes, uint8_t num_data_bytes)
void set_pixel_mode(PixelMode mode)
ILI9XXXILI9488(const uint8_t *seq=INITCMD_ILI9488)
void set_palette(const uint8_t *palette)
ILI9XXXDisplay(uint8_t const *init_sequence, int16_t width, int16_t height)
void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2)
bool is_failed() const
Definition: component.cpp:143
uint16_t x
Definition: tt21100.cpp:17
void set_mirror_x(bool mirror_x)
void set_mirror_y(bool mirror_y)
void data(uint8_t value) override
uint8_t h
Definition: bl0906.h:209
uint16_t y
Definition: tt21100.cpp:18
void set_buffer_color_mode(ILI9XXXColorMode color_mode)
int16_t width_
Display width as modified by current rotation.
The SPIDevice is what components using the SPI will create.
Definition: spi.h:410
void draw_absolute_pixel_internal(int x, int y, Color color) override
int16_t height_
Display height as modified by current rotation.
void init_lcd_(const uint8_t *addr)
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
float get_setup_priority() const override
const size_t ILI9XXX_TRANSFER_BUFFER_SIZE
uint16_t reset
Definition: ina226.h:39
virtual void data(uint8_t value)
void set_dimensions(int16_t width, int16_t height)
display::DisplayType get_display_type() override
void add_init_sequence(const std::vector< uint8_t > &sequence)
void set_offsets(int16_t offset_x, int16_t offset_y)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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
void fill(Color color) override
void set_dc_pin(GPIOPin *dc_pin)
void set_reset_pin(GPIOPin *reset)
void set_color_order(display::ColorOrder color_order)
stm32_cmd_t * cmd
Definition: stm32flash.h:96
virtual void command(uint8_t value)