ESPHome  2024.12.2
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  this->alloc_buffer_();
102  return !this->is_failed();
103  }
104  return true;
105  }
106 
107  void draw_absolute_pixel_internal(int x, int y, Color color) override;
108  void setup_pins_();
109 
110  virtual void set_madctl();
111  void display_();
112  void init_lcd_(const uint8_t *addr);
113  void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2);
114  void reset_();
115 
116  uint8_t const *init_sequence_{};
117  std::vector<uint8_t> extra_init_sequence_;
118  int16_t width_{0};
119  int16_t height_{0};
120  int16_t offset_x_{0};
121  int16_t offset_y_{0};
122  uint16_t x_low_{0};
123  uint16_t y_low_{0};
124  uint16_t x_high_{0};
125  uint16_t y_high_{0};
126  const uint8_t *palette_{};
127 
129 
130  uint32_t get_buffer_length_();
131  int get_width_internal() override;
132  int get_height_internal() override;
133 
134  void start_command_();
135  void end_command_();
136  void start_data_();
137  void end_data_();
138  void alloc_buffer_();
139 
140  GPIOPin *reset_pin_{nullptr};
141  GPIOPin *dc_pin_{nullptr};
142  GPIOPin *busy_pin_{nullptr};
143 
144  bool prossing_update_ = false;
145  bool need_update_ = false;
146  bool is_18bitdisplay_ = false;
150  bool swap_xy_{};
151  bool mirror_x_{};
152  bool mirror_y_{};
153 };
154 
155 //----------- M5Stack display --------------
157  public:
158  ILI9XXXM5Stack() : ILI9XXXDisplay(INITCMD_M5STACK, 320, 240) {}
159 };
160 
161 //----------- M5Stack display --------------
163  public:
164  ILI9XXXM5CORE() : ILI9XXXDisplay(INITCMD_M5CORE, 320, 240) {}
165 };
166 
167 //----------- ST7789V display --------------
169  public:
170  ILI9XXXST7789V() : ILI9XXXDisplay(INITCMD_ST7789V, 240, 320) {}
171 };
172 
173 //----------- ILI9XXX_24_TFT display --------------
175  public:
176  ILI9XXXILI9341() : ILI9XXXDisplay(INITCMD_ILI9341, 240, 320) {}
177 };
178 
179 //----------- ILI9XXX_24_TFT rotated display --------------
181  public:
182  ILI9XXXILI9342() : ILI9XXXDisplay(INITCMD_ILI9341, 320, 240) {}
183 };
184 
185 //----------- ILI9XXX_??_TFT rotated display --------------
187  public:
188  ILI9XXXILI9481() : ILI9XXXDisplay(INITCMD_ILI9481, 480, 320) {}
189 };
190 
191 //----------- ILI9481 in 18 bit mode --------------
193  public:
194  ILI9XXXILI948118() : ILI9XXXDisplay(INITCMD_ILI9481_18, 320, 480) {}
195 };
196 
197 //----------- ILI9XXX_35_TFT rotated display --------------
199  public:
200  ILI9XXXILI9486() : ILI9XXXDisplay(INITCMD_ILI9486, 480, 320) {}
201 };
202 
204  public:
205  ILI9XXXILI9488(const uint8_t *seq = INITCMD_ILI9488) : ILI9XXXDisplay(seq, 480, 320) {}
206 
207  protected:
208  void set_madctl() override {
209  uint8_t mad = this->color_order_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
210  uint8_t dfun = 0x22;
211  this->width_ = 320;
212  this->height_ = 480;
213  if (!(this->swap_xy_ || this->mirror_x_ || this->mirror_y_)) {
214  // no transforms
215  } else if (this->mirror_y_ && this->mirror_x_) {
216  // rotate 180
217  dfun = 0x42;
218  } else if (this->swap_xy_) {
219  this->width_ = 480;
220  this->height_ = 320;
221  mad |= 0x20;
222  if (this->mirror_x_) {
223  dfun = 0x02;
224  } else {
225  dfun = 0x62;
226  }
227  }
228  this->command(ILI9XXX_DFUNCTR);
229  this->data(0);
230  this->data(dfun);
231  this->command(ILI9XXX_MADCTL);
232  this->data(mad);
233  }
234 };
235 //----------- Waveshare 3.5 Res Touch - ILI9488 interfaced via 16 bit shift register to parallel */
237  public:
238  WAVESHARERES35() : ILI9XXXILI9488(INITCMD_WAVESHARE_RES_3_5) {}
239  void data(uint8_t value) override {
240  this->start_data_();
241  this->write_byte(0);
242  this->write_byte(value);
243  this->end_data_();
244  }
245 };
246 
247 //----------- ILI9XXX_35_TFT origin colors rotated display --------------
249  public:
250  ILI9XXXILI9488A() : ILI9XXXDisplay(INITCMD_ILI9488_A, 480, 320) {}
251 };
252 
253 //----------- ILI9XXX_35_TFT rotated display --------------
255  public:
256  ILI9XXXST7796() : ILI9XXXDisplay(INITCMD_ST7796, 320, 480) {}
257 };
258 
259 class ILI9XXXS3Box : public ILI9XXXDisplay {
260  public:
261  ILI9XXXS3Box() : ILI9XXXDisplay(INITCMD_S3BOX, 320, 240) {}
262 };
263 
265  public:
266  ILI9XXXS3BoxLite() : ILI9XXXDisplay(INITCMD_S3BOXLITE, 320, 240) {}
267 };
268 
270  public:
271  ILI9XXXGC9A01A() : ILI9XXXDisplay(INITCMD_GC9A01A, 240, 240) {}
272 };
273 
274 //----------- ILI9XXX_24_TFT display --------------
276  public:
277  ILI9XXXST7735() : ILI9XXXDisplay(INITCMD_ST7735, 128, 160) {}
278 };
279 
280 } // namespace ili9xxx
281 } // 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:391
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)