ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mipi_spi.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstring>
5#include <utility>
6
10#include "esphome/core/hal.h"
12#include "esphome/core/log.h"
13
14namespace esphome::mipi_spi {
15
16ESPHOME_LOG_TAG(TAG, "display.mipi_spi");
17
18// Maximum bytes to log for commands (truncated if larger)
19static constexpr size_t MIPI_SPI_MAX_CMD_LOG_BYTES = 64;
20
21// Command codes for MIPI SPI displays. Not all currently used, kept here for reference.
22static constexpr uint8_t SW_RESET_CMD = 0x01;
23static constexpr uint8_t SLEEP_OUT = 0x11;
24static constexpr uint8_t NORON = 0x13;
25static constexpr uint8_t INVERT_OFF = 0x20;
26static constexpr uint8_t INVERT_ON = 0x21;
27static constexpr uint8_t ALL_ON = 0x23;
28static constexpr uint8_t WRAM = 0x24;
29static constexpr uint8_t MIPI = 0x26;
30static constexpr uint8_t DISPLAY_ON = 0x29;
31static constexpr uint8_t RASET = 0x2B;
32static constexpr uint8_t CASET = 0x2A;
33static constexpr uint8_t WDATA = 0x2C;
34static constexpr uint8_t TEON = 0x35;
35static constexpr uint8_t MADCTL_CMD = 0x36;
36static constexpr uint8_t PIXFMT = 0x3A;
37static constexpr uint8_t BRIGHTNESS = 0x51;
38static constexpr uint8_t SWIRE1 = 0x5A;
39static constexpr uint8_t SWIRE2 = 0x5B;
40static constexpr uint8_t PAGESEL = 0xFE;
41
42static constexpr uint8_t MADCTL_MY = 0x80; // Bit 7 Bottom to top
43static constexpr uint8_t MADCTL_MX = 0x40; // Bit 6 Right to left
44static constexpr uint8_t MADCTL_MV = 0x20; // Bit 5 Swap axes
45static constexpr uint8_t MADCTL_RGB = 0x00; // Bit 3 Red-Green-Blue pixel order
46static constexpr uint8_t MADCTL_BGR = 0x08; // Bit 3 Blue-Green-Red pixel order
47static constexpr uint8_t MADCTL_XFLIP = 0x02; // Mirror the display horizontally
48static constexpr uint8_t MADCTL_YFLIP = 0x01; // Mirror the display vertically
49static constexpr uint16_t MADCTL_FLIP_FLAG = 0x100; // controller uses axis flip bits
50
51static constexpr uint8_t DELAY_FLAG = 0xFF;
52
53// Buffer mode, conveniently also the number of bytes in a pixel
59
60enum BusType {
64 BUS_TYPE_SINGLE_16 = 16, // Single bit bus, but 16 bits per transfer
65};
66
67// Helper function for dump_config - defined in mipi_spi.cpp to allow use of LOG_PIN macro
68void internal_dump_config(const char *model, int width, int height, int offset_width, int offset_height, uint8_t madctl,
69 bool invert_colors, int display_bits, bool is_big_endian, const optional<uint8_t> &brightness,
70 GPIOPin *cs, GPIOPin *reset, GPIOPin *dc, int spi_mode, uint32_t data_rate, int bus_width,
71 bool has_hardware_rotation);
72
73// Lets a light set the display brightness without knowing the display's template parameters.
75 public:
76 virtual void set_brightness(uint8_t brightness) = 0;
77};
78
97template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
98 int WIDTH, int HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, int PAD_WIDTH, int PAD_HEIGHT, uint16_t MADCTL,
99 bool HAS_HARDWARE_ROTATION>
101 public MipiSpiBrightness,
102 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
103 spi::DATA_RATE_1MHZ> {
104 public:
105 MipiSpi() = default;
106 void update() override { this->stop_poller(); }
107 void draw_pixel_at(int x, int y, Color color) override {}
108 void set_model(const char *model) { this->model_ = model; }
109 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
110 void set_enable_pins(std::vector<GPIOPin *> enable_pins) { this->enable_pins_ = std::move(enable_pins); }
111 void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
112 void set_invert_colors(bool invert_colors) {
113 this->invert_colors_ = invert_colors;
114 this->reset_params_();
115 }
116 void set_brightness(uint8_t brightness) override {
117 this->brightness_ = brightness;
118 // Before setup the stored value is applied by reset_params_()
119 if (this->is_ready())
120 this->write_command_(BRIGHTNESS, brightness);
121 }
122 void set_rotation(display::DisplayRotation rotation) override {
123 this->rotation_ = rotation;
124 if constexpr (HAS_HARDWARE_ROTATION) {
125 this->reset_params_();
126 }
127 }
129
130 int get_width() override { return display::rotation_swaps_axes(this->rotation_) ? HEIGHT : WIDTH; }
131 int get_height() override { return display::rotation_swaps_axes(this->rotation_) ? WIDTH : HEIGHT; }
132
133 void set_init_sequence(const uint8_t *sequence, size_t len) {
134 this->init_sequence_ = sequence;
135 this->init_sequence_len_ = len;
136 }
137
138 // reset the display, and write the init sequence
139 void setup() override {
140 this->spi_setup();
141 if (this->dc_pin_ != nullptr) {
142 this->dc_pin_->setup();
143 this->dc_pin_->digital_write(false);
144 }
145 for (auto *pin : this->enable_pins_) {
146 pin->setup();
147 pin->digital_write(true);
148 }
149 // required delay after reset is already in the init sequence, don't duplicate
150 pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
151
152 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
153 auto when = millis() + 120;
154 size_t index = 0;
155 const uint8_t *seq = this->init_sequence_;
156 const size_t len = this->init_sequence_len_;
157 while (index != len) {
158 if (len - index < 2) {
159 esph_log_e(TAG, "Malformed init sequence");
160 this->mark_failed();
161 return;
162 }
163 uint8_t cmd = progmem_read_byte(seq + index++);
164 uint8_t x = progmem_read_byte(seq + index++);
165 if (x == DELAY_FLAG) {
166 if (cmd == 0) {
167 cmd = clamp_at_least((int) (when - millis()), 0);
168 }
169 esph_log_d(TAG, "Delay %dms", cmd);
170 delay(cmd);
171 } else {
172 uint8_t num_args = x & 0x7F;
173 if (len - index < num_args) {
174 esph_log_e(TAG, "Malformed init sequence");
175 this->mark_failed();
176 return;
177 }
178 // The sequence is in flash, which SPI DMA (and ESP8266 byte loads) cannot read
179 uint8_t args[0x80];
180 progmem_memcpy(args, seq + index, num_args);
181 this->write_command_(cmd, args, num_args);
182 index += num_args;
183 }
184 }
185 this->reset_params_();
186 // Marks init as done, so later commands log at verbose level instead of debug
187 this->init_sequence_len_ = 0;
188 }
189
190 void dump_config() override {
191 internal_dump_config(this->model_, this->get_width(), this->get_height(), this->get_offset_width_(),
192 this->get_offset_height_(), (uint8_t) MADCTL, this->invert_colors_, DISPLAYPIXEL * 8,
193 IS_BIG_ENDIAN, this->brightness_, this->cs_, this->reset_pin_, this->dc_pin_, this->mode_,
194 this->data_rate_, BUS_TYPE, HAS_HARDWARE_ROTATION);
195 }
196
197 protected:
198 // Drawing operations
199
200 void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness,
201 bool big_endian, int x_offset, int y_offset, int x_pad) override {
202 if (this->is_failed())
203 return;
204 if (w <= 0 || h <= 0)
205 return;
206 if (!this->is_native_block_(bitness, big_endian)) {
208 return;
209 }
210 this->write_to_display_(x_start, y_start, w, h, reinterpret_cast<const BUFFERTYPE *>(ptr), x_offset, y_offset,
211 x_pad);
212 }
213
214 /* METHODS */
215 // If hardware rotation is in use, the actual display width/height changes with rotation
216 int get_width_internal() override {
217 if constexpr (HAS_HARDWARE_ROTATION)
218 return get_width();
219 return WIDTH;
220 }
221 int get_height_internal() override {
222 if constexpr (HAS_HARDWARE_ROTATION)
223 return get_height();
224 return HEIGHT;
225 }
226 // convenience functions to write commands with or without data
227 void write_command_(uint8_t cmd, uint8_t data) { this->write_command_(cmd, &data, 1); }
228 void write_command_(uint8_t cmd) { this->write_command_(cmd, &cmd, 0); }
229
230 // Writes a command to the display, with the given bytes.
231 void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) {
232 char hex_buf[format_hex_pretty_size(MIPI_SPI_MAX_CMD_LOG_BYTES)];
233 // Don't spam the log after setup
234 if (this->init_sequence_len_ == 0) {
235 esph_log_v(TAG, "Command %02X, length %zu, bytes %s", cmd, len, format_hex_pretty_to(hex_buf, bytes, len));
236 } else {
237 esph_log_d(TAG, "Command %02X, length %zu, bytes %s", cmd, len, format_hex_pretty_to(hex_buf, bytes, len));
238 }
239 if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
240 this->enable();
241 this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len);
242 this->disable();
243 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
244 this->enable();
245 this->dc_pin_->digital_write(false);
246 this->write_cmd_addr_data(0, 0, 0, 0, &cmd, 1, 8);
247 this->dc_pin_->digital_write(true);
248 // hold the bus between command and data to avoid a glitch on the D/C line
249 if (len != 0) {
250 this->write_cmd_addr_data(0, 0, 0, 0, bytes, len, 8);
251 }
252 this->disable();
253 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE) {
254 // Toggle D/C only while holding the bus; works around a quirk in the CoreS3 and W5500 ethernet combination.
255 // See https://github.com/esphome/esphome/pull/18529
256 this->enable();
257 this->dc_pin_->digital_write(false);
258 this->write_byte(cmd);
259 this->dc_pin_->digital_write(true);
260 this->disable();
261 if (len != 0) {
262 this->enable();
263 this->write_array(bytes, len);
264 this->disable();
265 }
266 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE_16) {
267 // DC must be stable before CS as the clock is gated by CS
268 this->dc_pin_->digital_write(false);
269 this->enable();
270 this->write_byte(cmd);
271 this->disable();
272 this->dc_pin_->digital_write(true);
273 for (size_t i = 0; i != len; i++) {
274 // must enable and disable for each byte based on empirical testing
275 this->enable();
276 this->write_byte(0);
277 this->write_byte(bytes[i]);
278 this->disable();
279 }
280 }
281 }
282
283 // write changed parameters to the display
285 if (!this->is_ready())
286 return;
287 this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
288 if (this->brightness_.has_value())
289 this->write_command_(BRIGHTNESS, this->brightness_.value());
290
291 // calculate new madctl value from base value adjusted for rotation
292 uint8_t madctl = (uint8_t) MADCTL; // lower 8 bits only
293 constexpr bool use_flips = (MADCTL & MADCTL_FLIP_FLAG) != 0;
294 constexpr uint8_t x_mask = use_flips ? MADCTL_XFLIP : MADCTL_MX;
295 constexpr uint8_t y_mask = use_flips ? MADCTL_YFLIP : MADCTL_MY;
296 if constexpr (HAS_HARDWARE_ROTATION) {
297 switch (this->rotation_) {
298 default:
299 break;
301 madctl ^= x_mask; // flip X axis
302 madctl ^= MADCTL_MV; // swap X and Y axes
303 break;
305 madctl ^= x_mask; // flip X axis
306 madctl ^= y_mask; // flip Y axis
307 break;
309 madctl ^= y_mask; // flip Y axis
310 madctl ^= MADCTL_MV; // swap X and Y axes
311 break;
312 }
313 }
314 esph_log_d(TAG, "Setting MADCTL for rotation %d, value %X", this->rotation_, madctl);
315 this->write_command_(MADCTL_CMD, madctl);
316 }
317
318 uint16_t get_offset_width_() const {
319 if constexpr (HAS_HARDWARE_ROTATION) {
320 switch (this->rotation_) {
322 return OFFSET_HEIGHT;
324 return PAD_WIDTH;
326 return PAD_HEIGHT;
327 default:
328 break;
329 }
330 }
331 return OFFSET_WIDTH;
332 }
333
334 uint16_t get_offset_height_() const {
335 if constexpr (HAS_HARDWARE_ROTATION) {
336 switch (this->rotation_) {
338 return PAD_WIDTH;
340 return PAD_HEIGHT;
342 return OFFSET_WIDTH;
343 default:
344 break;
345 }
346 }
347 return OFFSET_HEIGHT;
348 }
349
350 // set the address window for the next data write
351 void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
352 esph_log_v(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
353 uint8_t buf[4];
354 x1 += get_offset_width_();
355 x2 += get_offset_width_();
356 y1 += get_offset_height_();
357 y2 += get_offset_height_();
358 put_be16(buf, y1);
359 put_be16(buf + 2, y2);
360 this->write_command_(RASET, buf, sizeof buf);
361 put_be16(buf, x1);
362 put_be16(buf + 2, x2);
363 this->write_command_(CASET, buf, sizeof buf);
364 if constexpr (BUS_TYPE != BUS_TYPE_QUAD) {
365 this->write_command_(WDATA);
366 }
367 }
368
369 // map the display color bitness to the pixel mode
371 switch (bitness) {
373 return PIXEL_MODE_18; // 18 bits per pixel
375 return PIXEL_MODE_16; // 16 bits per pixel
376 default:
377 return PIXEL_MODE_8; // Default to 8 bits per pixel
378 }
379 }
380
388 void write_display_data_(const uint8_t *ptr, size_t w, size_t h, size_t stride) {
389 if (stride == w) {
390 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
391 this->write_array(ptr, w * h);
392 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
393 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w * h, 4);
394 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
395 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w * h, 8);
396 }
397 } else {
398 for (size_t y = 0; y != h; y++) {
399 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
400 this->write_array(ptr, w);
401 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
402 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w, 4);
403 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
404 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w, 8);
405 }
406 ptr += stride;
407 }
408 }
409 }
410
411 // True when the block is already in the buffer pixel format and can be copied without per pixel work
413 return get_pixel_mode(bitness) == BUFFERPIXEL && big_endian == IS_BIG_ENDIAN &&
414 (HAS_HARDWARE_ROTATION || this->rotation_ == display::DISPLAY_ROTATION_0_DEGREES) && !this->is_clipping();
415 }
416
417 // Without a buffer there is nothing to convert a foreign block into, so it is dropped
418 virtual void draw_pixels_slow(int x_start, int y_start, int w, int h, const uint8_t *ptr,
420 esph_log_e(TAG, "Unsupported color depth, bit order or rotation");
421 }
422
429 void write_to_display_(int x_start, int y_start, int w, int h, const BUFFERTYPE *ptr, int x_offset, int y_offset,
430 int x_pad) {
431 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
432 this->enable();
433 ptr += y_offset * (x_offset + w + x_pad) + x_offset;
434 if constexpr (BUFFERPIXEL == DISPLAYPIXEL) {
435 this->write_display_data_(reinterpret_cast<const uint8_t *>(ptr), w * sizeof(BUFFERTYPE), h,
436 (x_offset + w + x_pad) * sizeof(BUFFERTYPE));
437 } else {
438 // type conversion required, do it in chunks
439 uint8_t dbuffer[DISPLAYPIXEL * 48];
440 uint8_t *dptr = dbuffer;
441 auto stride = x_offset + w + x_pad; // stride in pixels
442 for (size_t y = 0; y != static_cast<size_t>(h); y++) {
443 for (size_t x = 0; x != static_cast<size_t>(w); x++) {
444 auto color_val = ptr[y * stride + x];
445 if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_16) {
446 // 16 to 18 bit conversion
447 if constexpr (IS_BIG_ENDIAN) {
448 *dptr++ = color_val & 0xF8;
449 *dptr++ = ((color_val & 0x7) << 5) | (color_val & 0xE000) >> 11;
450 *dptr++ = (color_val >> 5) & 0xF8;
451 } else {
452 *dptr++ = (color_val >> 8) & 0xF8; // Blue
453 *dptr++ = (color_val & 0x7E0) >> 3;
454 *dptr++ = color_val << 3;
455 }
456 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_8) {
457 // 8 bit to 18 bit conversion
458 *dptr++ = color_val << 6; // Blue
459 *dptr++ = (color_val & 0x1C) << 3; // Green
460 *dptr++ = (color_val & 0xE0); // Red
461 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_16 && BUFFERPIXEL == PIXEL_MODE_8) {
462 if constexpr (IS_BIG_ENDIAN) {
463 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
464 *dptr++ = (color_val & 3) << 3;
465 } else {
466 *dptr++ = (color_val & 3) << 3;
467 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
468 }
469 }
470 // buffer full? Flush.
471 if (dptr == dbuffer + sizeof(dbuffer)) {
472 this->write_display_data_(dbuffer, sizeof(dbuffer), 1, sizeof(dbuffer));
473 dptr = dbuffer;
474 }
475 }
476 }
477 // flush any remaining data
478 if (dptr != dbuffer) {
479 this->write_display_data_(dbuffer, dptr - dbuffer, 1, dptr - dbuffer);
480 }
481 }
482 this->disable();
483 }
484
485 /* PROPERTIES */
486
487 // GPIO pins
489 std::vector<GPIOPin *> enable_pins_{};
490 GPIOPin *dc_pin_{nullptr};
491
492 // other properties set by configuration
494 optional<uint8_t> brightness_{};
495 const char *model_{"Unknown"};
496 // Shared PROGMEM table
497 const uint8_t *init_sequence_{nullptr};
499};
500
519template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
520 uint16_t WIDTH, uint16_t HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, int PAD_WIDTH, int PAD_HEIGHT,
521 uint16_t MADCTL, bool HAS_HARDWARE_ROTATION, int FRACTION, unsigned ROUNDING>
523 : public MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH,
524 OFFSET_HEIGHT, PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION> {
525 public:
526 // these values define the buffer size needed to write in accordance with the chip pixel alignment
527 // requirements. If the required rounding does not divide the width and height, we round up to the next multiple and
528 // ignore the extra columns and rows when drawing, but use them to write to the display.
529 static constexpr size_t round_buffer(size_t size) { return (size + ROUNDING - 1) / ROUNDING * ROUNDING; }
530
531 MipiSpiBuffer() = default;
532
533 void dump_config() override {
534 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
535 PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION>::dump_config();
536 esph_log_config(TAG,
537 " Rotation: %d°\n"
538 " Buffer pixels: %d bits\n"
539 " Buffer fraction: 1/%d\n"
540 " Buffer bytes: %zu\n"
541 " Draw rounding: %u",
542 this->rotation_, BUFFERPIXEL * 8, FRACTION,
543 sizeof(BUFFERTYPE) * round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION, ROUNDING);
544 }
545
546 void setup() override {
547 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
548 PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION>::setup();
549 RAMAllocator<BUFFERTYPE> allocator{};
550 this->buffer_ = allocator.allocate(round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION);
551 if (this->buffer_ == nullptr) {
552 this->mark_failed(LOG_STR("Buffer allocation failed"));
553 }
554 }
555
556 void update() override {
557#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
558 auto now = millis();
559#endif
560 if (this->is_failed()) {
561 return;
562 }
563 // for updates with a small buffer, we repeatedly call the writer_ function, clipping the height to a fraction of
564 // the display height,
565 auto increment = (this->get_height_internal() / FRACTION / ROUNDING) * ROUNDING;
566 for (this->start_line_ = 0; this->start_line_ < this->get_height_internal(); this->start_line_ = this->end_line_) {
567#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
568 auto lap = millis();
569#endif
570 this->end_line_ = clamp_at_most(this->start_line_ + increment, this->get_height_internal());
571 if (this->auto_clear_enabled_) {
572 this->clear();
573 }
574 this->updating_ = true;
575 if (this->show_test_card_) {
576 this->test_card();
577 } else if (this->page_ != nullptr) {
578 this->page_->get_writer()(*this);
579 } else if (this->writer_.has_value()) {
580 (*this->writer_)(*this);
581 }
582 this->updating_ = false;
583#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
584 esph_log_v(TAG, "Drawing from line %d took %dms", this->start_line_, millis() - lap);
585 lap = millis();
586#endif
587 if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
588 return;
589 esph_log_v(TAG, "x_low %d, y_low %d, x_high %d, y_high %d", this->x_low_, this->y_low_, this->x_high_,
590 this->y_high_);
591 // Some chips require that the drawing window be aligned on certain boundaries
592 this->x_low_ = this->x_low_ / ROUNDING * ROUNDING;
593 this->y_low_ = this->y_low_ / ROUNDING * ROUNDING;
594 this->x_high_ = round_buffer(this->x_high_ + 1) - 1;
595 this->y_high_ = clamp_at_most(round_buffer(this->y_high_ + 1) - 1, this->end_line_ - 1);
596 int w = this->x_high_ - this->x_low_ + 1;
597 int h = this->y_high_ - this->y_low_ + 1;
598 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_,
599 this->y_low_ - this->start_line_,
600 round_buffer(this->get_width_internal()) - w - this->x_low_);
601 // invalidate watermarks
602 this->x_low_ = this->get_width_internal();
603 this->y_low_ = this->get_height_internal();
604 this->x_high_ = 0;
605 this->y_high_ = 0;
606#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
607 esph_log_v(TAG, "Write to display took %dms", millis() - lap);
608 lap = millis();
609#endif
610 }
611#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
612 esph_log_v(TAG, "Total update took %dms", millis() - now);
613#endif
614 }
615
616 // With hardware rotation the buffer is written in drawn coordinates.
617 bool pixel_axes_swapped() const override { return !HAS_HARDWARE_ROTATION && display::Display::pixel_axes_swapped(); }
618
619 // Draw a pixel at the given coordinates.
620 void draw_pixel_at(int x, int y, Color color) override {
621 if (this->is_point_clipped(x, y))
622 return;
623 if constexpr (not HAS_HARDWARE_ROTATION) {
625 x = WIDTH - x - 1;
626 y = HEIGHT - y - 1;
628 auto tmp = x;
629 x = WIDTH - y - 1;
630 y = tmp;
632 auto tmp = y;
633 y = HEIGHT - x - 1;
634 x = tmp;
635 }
636 }
638 return;
639 this->buffer_[(y - this->start_line_) * round_buffer(this->get_width_internal()) + x] = convert_color(color);
640 if (x < this->x_low_) {
641 this->x_low_ = x;
642 }
643 if (x > this->x_high_) {
644 this->x_high_ = x;
645 }
646 if (y < this->y_low_) {
647 this->y_low_ = y;
648 }
649 if (y > this->y_high_) {
650 this->y_high_ = y;
651 }
652 }
653
654 // Fills the display with a color.
655 void fill(Color color) override {
656 // If clipping is active, fall back to base implementation
657 if (this->get_clipping().is_set()) {
659 return;
660 }
661
662 this->x_low_ = 0;
663 this->y_low_ = this->start_line_;
664 this->x_high_ = this->get_width_internal() - 1;
665 this->y_high_ = this->end_line_ - 1;
666 std::fill_n(this->buffer_, (this->end_line_ - this->start_line_) * round_buffer(this->get_width_internal()),
667 convert_color(color));
668 }
669
670 protected:
671 // While update() runs the writer, a block goes into the buffer slice, or the slice flush would paint over it.
672 // Outside update() (e.g. LVGL) it is written straight to the display.
673 void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness,
674 bool big_endian, int x_offset, int y_offset, int x_pad) override {
675 if (!this->updating_) {
676 MipiSpiBuffer::MipiSpi::draw_pixels_internal(x_start, y_start, w, h, ptr, bitness, big_endian, x_offset, y_offset,
677 x_pad);
678 return;
679 }
680 if (this->is_failed() || w <= 0 || h <= 0)
681 return;
682 if (!this->is_native_block_(bitness, big_endian)) {
684 x_pad);
685 return;
686 }
687 // Only the rows inside the current slice are kept
688 const int first_col = std::max(0, -x_start);
689 const int end_col = std::min(w, this->get_width_internal() - x_start);
690 const int first_row = std::max<int>(0, this->start_line_ - y_start);
691 const int end_row = std::min<int>(h, this->end_line_ - y_start);
692 if (first_col >= end_col || first_row >= end_row)
693 return;
694 const size_t stride = x_offset + w + x_pad;
695 const size_t buffer_stride = round_buffer(this->get_width_internal());
696 for (int y = first_row; y != end_row; y++) {
697 memcpy(this->buffer_ + (y_start + y - this->start_line_) * buffer_stride + x_start + first_col,
698 ptr + ((y_offset + y) * stride + x_offset + first_col) * sizeof(BUFFERTYPE),
699 (end_col - first_col) * sizeof(BUFFERTYPE));
700 }
701 this->x_low_ = std::min<int>(this->x_low_, x_start + first_col);
702 this->y_low_ = std::min<int>(this->y_low_, y_start + first_row);
703 this->x_high_ = std::max<int>(this->x_high_, x_start + end_col - 1);
704 this->y_high_ = std::max<int>(this->y_high_, y_start + end_row - 1);
705 }
706
707 void draw_pixels_slow(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness,
708 bool big_endian, int x_offset, int y_offset, int x_pad) override {
710 }
711 // Convert a color to the buffer pixel format.
712 static BUFFERTYPE convert_color(const Color &color) {
713 if constexpr (BUFFERPIXEL == PIXEL_MODE_8) {
714 return (color.red & 0xE0) | (color.g & 0xE0) >> 3 | color.b >> 6;
715 } else if constexpr (BUFFERPIXEL == PIXEL_MODE_16) {
716 if constexpr (IS_BIG_ENDIAN) {
717 return (color.r & 0xF8) | color.g >> 5 | (color.g & 0x1C) << 11 | (color.b & 0xF8) << 5;
718 } else {
719 return (color.r & 0xF8) << 8 | (color.g & 0xFC) << 3 | color.b >> 3;
720 }
721 }
722 return static_cast<BUFFERTYPE>(0);
723 }
724
725 BUFFERTYPE *buffer_{};
726 uint16_t x_low_{WIDTH};
727 uint16_t y_low_{HEIGHT};
728 uint16_t x_high_{0};
729 uint16_t y_high_{0};
730 uint16_t start_line_{0};
731 uint16_t end_line_{1};
732 bool updating_{false};
733};
734
735} // namespace esphome::mipi_spi
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool is_ready() const
virtual void setup()=0
virtual void digital_write(bool value)=0
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
T * allocate(size_t n)
Definition helpers.h:2423
virtual bool pixel_axes_swapped() const
True when draw_pixel_at() swaps x and y before writing, so pixels that are neighbours along x land a ...
Definition display.h:757
display_writer_t writer_
Definition display.h:864
int int int const uint8_t ColorOrder ColorBitness bitness
Definition display.h:392
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:17
bool is_clipping() const
Definition display.h:805
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
Definition display.h:392
virtual void fill(Color color)
Fill the entire screen with the given color.
Definition display.cpp:16
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
bool ESPHOME_ALWAYS_INLINE is_point_clipped(int x, int y) const
Whether (x, y) falls outside the active clipping rectangle.
Definition display.h:810
DisplayPage * page_
Definition display.h:865
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Definition display.h:392
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:826
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
Definition display.h:392
DisplayRotation rotation_
Definition display.h:863
virtual void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Draws a block for draw_pixels_at(); the parameters are the same.
Definition display.cpp:119
const display_writer_t & get_writer() const
Definition display.cpp:956
virtual void set_brightness(uint8_t brightness)=0
Class for MIPI SPI displays with a buffer.
Definition mipi_spi.h:524
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:620
static constexpr size_t round_buffer(size_t size)
Definition mipi_spi.h:529
void draw_pixels_slow(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
Definition mipi_spi.h:707
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
Definition mipi_spi.h:673
bool pixel_axes_swapped() const override
Definition mipi_spi.h:617
void fill(Color color) override
Definition mipi_spi.h:655
static BUFFERTYPE convert_color(const Color &color)
Definition mipi_spi.h:712
Base class for MIPI SPI displays.
Definition mipi_spi.h:103
void update() override
Definition mipi_spi.h:106
uint16_t get_offset_height_() const
Definition mipi_spi.h:334
void write_to_display_(int x_start, int y_start, int w, int h, const BUFFERTYPE *ptr, int x_offset, int y_offset, int x_pad)
Writes a buffer to the display.
Definition mipi_spi.h:429
int get_width_internal() override
Definition mipi_spi.h:216
void set_init_sequence(const uint8_t *sequence, size_t len)
Definition mipi_spi.h:133
const uint8_t * init_sequence_
Definition mipi_spi.h:497
void set_brightness(uint8_t brightness) override
Definition mipi_spi.h:116
static PixelMode get_pixel_mode(display::ColorBitness bitness)
Definition mipi_spi.h:370
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
Definition mipi_spi.h:231
void set_reset_pin(GPIOPin *reset_pin)
Definition mipi_spi.h:109
int get_width() override
Definition mipi_spi.h:130
optional< uint8_t > brightness_
Definition mipi_spi.h:494
void set_invert_colors(bool invert_colors)
Definition mipi_spi.h:112
int get_height_internal() override
Definition mipi_spi.h:221
void dump_config() override
Definition mipi_spi.h:190
uint16_t get_offset_width_() const
Definition mipi_spi.h:318
int get_height() override
Definition mipi_spi.h:131
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:107
void write_command_(uint8_t cmd)
Definition mipi_spi.h:228
void write_display_data_(const uint8_t *ptr, size_t w, size_t h, size_t stride)
Writes a buffer to the display.
Definition mipi_spi.h:388
display::DisplayType get_display_type() override
Definition mipi_spi.h:128
void set_dc_pin(GPIOPin *dc_pin)
Definition mipi_spi.h:111
void set_rotation(display::DisplayRotation rotation) override
Definition mipi_spi.h:122
void write_command_(uint8_t cmd, uint8_t data)
Definition mipi_spi.h:227
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition mipi_spi.h:351
bool is_native_block_(display::ColorBitness bitness, bool big_endian) const
Definition mipi_spi.h:412
void setup() override
Definition mipi_spi.h:139
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
Definition mipi_spi.h:200
void set_model(const char *model)
Definition mipi_spi.h:108
virtual void draw_pixels_slow(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)
Definition mipi_spi.h:418
std::vector< GPIOPin * > enable_pins_
Definition mipi_spi.h:489
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition mipi_spi.h:110
uint32_t data_rate_
Definition spi.h:436
The SPIDevice is what components using the SPI will create.
Definition spi.h:456
void write_cmd_addr_data(size_t cmd_bits, uint32_t cmd, size_t addr_bits, uint32_t address, const uint8_t *data, size_t length, uint8_t bus_width=1)
Definition spi.h:500
uint16_t reset
Definition ina226.h:5
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:136
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:139
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:138
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:137
constexpr bool rotation_swaps_axes(DisplayRotation rotation)
Whether a rotation swaps the x and y axes.
Definition display.h:143
void internal_dump_config(const char *model, int width, int height, int offset_width, int offset_height, uint8_t madctl, bool invert_colors, int display_bits, bool is_big_endian, const optional< uint8_t > &brightness, GPIOPin *cs, GPIOPin *reset, GPIOPin *dc, int spi_mode, uint32_t data_rate, int bus_width, bool has_hardware_rotation)
Definition mipi_spi.cpp:6
ESPHOME_LOG_TAG(TAG, "display.mipi_spi")
T clamp_at_most(T value, U max)
Definition helpers.h:2576
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
const void size_t len
Definition hal.h:68
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:498
uint16_t size
Definition helpers.cpp:34
T clamp_at_least(T value, U min)
Definition helpers.h:2571
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1687
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1112
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:163
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t
uint8_t red
Definition color.h:37
uint8_t g
Definition color.h:40
uint8_t b
Definition color.h:44
uint8_t r
Definition color.h:36
uint16_t seq
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6