ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ili9xxx_display.h
Go to the documentation of this file.
1#pragma once
5#include "esphome/core/log.h"
6#include "ili9xxx_defines.h"
7#include "ili9xxx_init.h"
8
9namespace esphome::ili9xxx {
10
11ESPHOME_LOG_TAG(TAG, "ili9xxx");
12const size_t ILI9XXX_TRANSFER_BUFFER_SIZE = 126; // ensure this is divisible by 6
13
15 BITS_8 = 0x08,
17 BITS_16 = 0x10,
18};
19
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;
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_powerdown() override { this->command(ILI9XXX_SLPIN); }
93
95
96 protected:
97 void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness,
98 bool big_endian, int x_offset, int y_offset, int x_pad) override;
99
100 inline bool check_buffer_() {
101 if (this->buffer_ == nullptr) {
102 if (!this->is_failed())
103 this->alloc_buffer_();
104 return !this->is_failed();
105 }
106 return true;
107 }
108
109 void draw_absolute_pixel_internal(int x, int y, Color color) override;
110 void setup_pins_();
111
112 virtual void set_madctl();
113 void display_();
114 void init_lcd_(const uint8_t *addr);
115 void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2);
116 void reset_();
117
118 uint8_t const *init_sequence_{};
119 std::vector<uint8_t> extra_init_sequence_;
120 int16_t width_{0};
121 int16_t height_{0};
122 int16_t offset_x_{0};
123 int16_t offset_y_{0};
124 uint16_t x_low_{0};
125 uint16_t y_low_{0};
126 uint16_t x_high_{0};
127 uint16_t y_high_{0};
128 const uint8_t *palette_{};
129
131
133 int get_width_internal() override;
134 int get_height_internal() override;
135
136 void start_command_();
137 void end_command_();
138 void start_data_();
139 void end_data_();
140 void alloc_buffer_();
141
143 GPIOPin *dc_pin_{nullptr};
145
146 bool prossing_update_ = false;
147 bool need_update_ = false;
148 bool is_18bitdisplay_ = false;
152 bool swap_xy_{};
153 bool mirror_x_{};
154 bool mirror_y_{};
155};
156
157//----------- M5Stack display --------------
159 public:
160 ILI9XXXM5Stack() : ILI9XXXDisplay(INITCMD_M5STACK, 320, 240) {}
161};
162
163//----------- M5Stack display --------------
165 public:
166 ILI9XXXM5CORE() : ILI9XXXDisplay(INITCMD_M5CORE, 320, 240) {}
167};
168
169//----------- ST7789V display --------------
171 public:
172 ILI9XXXST7789V() : ILI9XXXDisplay(INITCMD_ST7789V, 240, 320) {}
173};
174
175//----------- ILI9XXX_24_TFT display --------------
177 public:
178 ILI9XXXILI9341() : ILI9XXXDisplay(INITCMD_ILI9341, 240, 320) {}
179};
180
181//----------- ILI9XXX_24_TFT rotated display --------------
183 public:
184 ILI9XXXILI9342() : ILI9XXXDisplay(INITCMD_ILI9341, 320, 240) {}
185};
186
187//----------- ILI9XXX_??_TFT rotated display --------------
189 public:
190 ILI9XXXILI9481() : ILI9XXXDisplay(INITCMD_ILI9481, 480, 320) {}
191};
192
193//----------- ILI9481 in 18 bit mode --------------
195 public:
196 ILI9XXXILI948118() : ILI9XXXDisplay(INITCMD_ILI9481_18, 320, 480) {}
197};
198
199//----------- ILI9XXX_35_TFT rotated display --------------
201 public:
202 ILI9XXXILI9486() : ILI9XXXDisplay(INITCMD_ILI9486, 480, 320) {}
203};
204
206 public:
207 ILI9XXXILI9488(const uint8_t *seq = INITCMD_ILI9488) : ILI9XXXDisplay(seq, 480, 320) {}
208
209 protected:
210 void set_madctl() override {
211 uint8_t mad = this->color_order_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
212 uint8_t dfun = 0x22;
213 this->width_ = 320;
214 this->height_ = 480;
215 if (!(this->swap_xy_ || this->mirror_x_ || this->mirror_y_)) {
216 // no transforms
217 } else if (this->mirror_y_ && this->mirror_x_) {
218 // rotate 180
219 dfun = 0x42;
220 } else if (this->swap_xy_) {
221 this->width_ = 480;
222 this->height_ = 320;
223 mad |= 0x20;
224 if (this->mirror_x_) {
225 dfun = 0x02;
226 } else {
227 dfun = 0x62;
228 }
229 }
230 this->command(ILI9XXX_DFUNCTR);
231 this->data(0);
232 this->data(dfun);
233 this->command(ILI9XXX_MADCTL);
234 this->data(mad);
235 }
236};
237//----------- Waveshare 3.5 Res Touch - ILI9488 interfaced via 16 bit shift register to parallel */
239 public:
240 WAVESHARERES35() : ILI9XXXILI9488(INITCMD_WAVESHARE_RES_3_5) {}
241 void data(uint8_t value) override {
242 this->start_data_();
243 this->write_byte(0);
244 this->write_byte(value);
245 this->end_data_();
246 }
247};
248
249//----------- ILI9XXX_35_TFT origin colors rotated display --------------
251 public:
252 ILI9XXXILI9488A() : ILI9XXXDisplay(INITCMD_ILI9488_A, 480, 320) {}
253};
254
255//----------- ILI9XXX_35_TFT rotated display --------------
257 public:
258 ILI9XXXST7796() : ILI9XXXDisplay(INITCMD_ST7796, 320, 480) {}
259};
260
262 public:
263 ILI9XXXS3Box() : ILI9XXXDisplay(INITCMD_S3BOX, 320, 240) {}
264};
265
267 public:
268 ILI9XXXS3BoxLite() : ILI9XXXDisplay(INITCMD_S3BOXLITE, 320, 240) {}
269};
270
272 public:
273 ILI9XXXGC9A01A() : ILI9XXXDisplay(INITCMD_GC9A01A, 240, 240) {}
274};
275
277 public:
278 ILI9XXXGC9D01N() : ILI9XXXDisplay(INITCMD_GC9D01N, 160, 160) {}
279};
280
281//----------- ILI9XXX_24_TFT display --------------
283 public:
284 ILI9XXXST7735() : ILI9XXXDisplay(INITCMD_ST7735, 128, 160) {}
285};
286
287} // namespace esphome::ili9xxx
BedjetMode mode
BedJet operating mode.
bool is_failed() const
Definition component.h:274
int int int const uint8_t ColorOrder ColorBitness bitness
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int int int x_pad
Definition display.h:392
int int int const uint8_t * ptr
Definition display.h:391
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
Definition display.h:392
void set_pixel_mode(PixelMode mode)
virtual void data(uint8_t value)
void set_dc_pin(GPIOPin *dc_pin)
int16_t width_
Display width as modified by current rotation.
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
std::vector< uint8_t > extra_init_sequence_
float get_setup_priority() const override
void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2)
void set_color_order(display::ColorOrder color_order)
void add_init_sequence(const std::vector< uint8_t > &sequence)
display::DisplayType get_display_type() override
void init_lcd_(const uint8_t *addr)
void set_offsets(int16_t offset_x, int16_t offset_y)
void set_palette(const uint8_t *palette)
virtual void command(uint8_t value)
void draw_absolute_pixel_internal(int x, int y, Color color) override
void fill(Color color) override
void set_reset_pin(GPIOPin *reset)
void set_dimensions(int16_t width, int16_t height)
void set_buffer_color_mode(ILI9XXXColorMode color_mode)
ILI9XXXDisplay(uint8_t const *init_sequence, int16_t width, int16_t height)
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.
ILI9XXXILI9488(const uint8_t *seq=INITCMD_ILI9488)
void data(uint8_t value) override
The SPIDevice is what components using the SPI will create.
Definition spi.h:456
uint16_t reset
Definition ina226.h:5
const size_t ILI9XXX_TRANSFER_BUFFER_SIZE
ESPHOME_LOG_TAG(TAG, "ili9xxx")
static void uint32_t
uint16_t seq
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6