ESPHome 2026.6.5
Loading...
Searching...
No Matches
mipi_spi.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
9
10namespace esphome::mipi_spi {
11
12constexpr static const char *const TAG = "display.mipi_spi";
13
14// Maximum bytes to log for commands (truncated if larger)
15static constexpr size_t MIPI_SPI_MAX_CMD_LOG_BYTES = 64;
16static constexpr uint8_t SW_RESET_CMD = 0x01;
17static constexpr uint8_t SLEEP_OUT = 0x11;
18static constexpr uint8_t NORON = 0x13;
19static constexpr uint8_t INVERT_OFF = 0x20;
20static constexpr uint8_t INVERT_ON = 0x21;
21static constexpr uint8_t ALL_ON = 0x23;
22static constexpr uint8_t WRAM = 0x24;
23static constexpr uint8_t MIPI = 0x26;
24static constexpr uint8_t DISPLAY_ON = 0x29;
25static constexpr uint8_t RASET = 0x2B;
26static constexpr uint8_t CASET = 0x2A;
27static constexpr uint8_t WDATA = 0x2C;
28static constexpr uint8_t TEON = 0x35;
29static constexpr uint8_t MADCTL_CMD = 0x36;
30static constexpr uint8_t PIXFMT = 0x3A;
31static constexpr uint8_t BRIGHTNESS = 0x51;
32static constexpr uint8_t SWIRE1 = 0x5A;
33static constexpr uint8_t SWIRE2 = 0x5B;
34static constexpr uint8_t PAGESEL = 0xFE;
35
36static constexpr uint8_t MADCTL_MY = 0x80; // Bit 7 Bottom to top
37static constexpr uint8_t MADCTL_MX = 0x40; // Bit 6 Right to left
38static constexpr uint8_t MADCTL_MV = 0x20; // Bit 5 Swap axes
39static constexpr uint8_t MADCTL_RGB = 0x00; // Bit 3 Red-Green-Blue pixel order
40static constexpr uint8_t MADCTL_BGR = 0x08; // Bit 3 Blue-Green-Red pixel order
41static constexpr uint8_t MADCTL_XFLIP = 0x02; // Mirror the display horizontally
42static constexpr uint8_t MADCTL_YFLIP = 0x01; // Mirror the display vertically
43static constexpr uint16_t MADCTL_FLIP_FLAG = 0x100; // controller uses axis flip bits
44
45static constexpr uint8_t DELAY_FLAG = 0xFF;
46// store a 16 bit value in a buffer, big endian.
47static inline void put16_be(uint8_t *buf, uint16_t value) {
48 buf[0] = value >> 8;
49 buf[1] = value;
50}
51
52// Buffer mode, conveniently also the number of bytes in a pixel
58
59enum BusType {
63 BUS_TYPE_SINGLE_16 = 16, // Single bit bus, but 16 bits per transfer
64};
65
66// Helper function for dump_config - defined in mipi_spi.cpp to allow use of LOG_PIN macro
67void internal_dump_config(const char *model, int width, int height, int offset_width, int offset_height, uint8_t madctl,
68 bool invert_colors, int display_bits, bool is_big_endian, const optional<uint8_t> &brightness,
69 GPIOPin *cs, GPIOPin *reset, GPIOPin *dc, int spi_mode, uint32_t data_rate, int bus_width,
70 bool has_hardware_rotation);
71
90template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
91 int WIDTH, int HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, int PAD_WIDTH, int PAD_HEIGHT, uint16_t MADCTL,
92 bool HAS_HARDWARE_ROTATION>
94 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
95 spi::DATA_RATE_1MHZ> {
96 public:
97 MipiSpi() = default;
98 void update() override { this->stop_poller(); }
99 void draw_pixel_at(int x, int y, Color color) override {}
100 void set_model(const char *model) { this->model_ = model; }
101 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
102 void set_enable_pins(std::vector<GPIOPin *> enable_pins) { this->enable_pins_ = std::move(enable_pins); }
103 void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
104 void set_invert_colors(bool invert_colors) {
105 this->invert_colors_ = invert_colors;
106 this->reset_params_();
107 }
108 void set_brightness(uint8_t brightness) {
109 this->brightness_ = brightness;
110 this->reset_params_();
111 }
112 void set_rotation(display::DisplayRotation rotation) override {
113 this->rotation_ = rotation;
114 if constexpr (HAS_HARDWARE_ROTATION) {
115 this->reset_params_();
116 }
117 }
119
120 int get_width() override {
123 return HEIGHT;
124 return WIDTH;
125 }
126
127 int get_height() override {
130 return WIDTH;
131 return HEIGHT;
132 }
133
134 void set_init_sequence(const std::vector<uint8_t> &sequence) { this->init_sequence_ = sequence; }
135
136 // reset the display, and write the init sequence
137 void setup() override {
138 this->spi_setup();
139 if (this->dc_pin_ != nullptr) {
140 this->dc_pin_->setup();
141 this->dc_pin_->digital_write(false);
142 }
143 for (auto *pin : this->enable_pins_) {
144 pin->setup();
145 pin->digital_write(true);
146 }
147 if (this->reset_pin_ != nullptr) {
148 this->reset_pin_->setup();
149 this->reset_pin_->digital_write(true);
150 delay(5);
151 this->reset_pin_->digital_write(false);
152 delay(5);
153 this->reset_pin_->digital_write(true);
154 } else {
155 // no reset pin, send software reset command
156 this->write_command_(SW_RESET_CMD);
157 }
158
159 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
160 auto when = millis() + 120;
161 delay(10);
162 size_t index = 0;
163 auto &vec = this->init_sequence_;
164 while (index != vec.size()) {
165 if (vec.size() - index < 2) {
166 esph_log_e(TAG, "Malformed init sequence");
167 this->mark_failed();
168 return;
169 }
170 uint8_t cmd = vec[index++];
171 uint8_t x = vec[index++];
172 if (x == DELAY_FLAG) {
173 esph_log_d(TAG, "Delay %dms", cmd);
174 delay(cmd);
175 } else {
176 uint8_t num_args = x & 0x7F;
177 if (vec.size() - index < num_args) {
178 esph_log_e(TAG, "Malformed init sequence");
179 this->mark_failed();
180 return;
181 }
182 switch (cmd) {
183 case SLEEP_OUT: {
184 // are we ready, boots?
185 int duration = when - millis();
186 if (duration > 0) {
187 esph_log_d(TAG, "Sleep %dms", duration);
189 }
190 } break;
191
192 default:
193 break;
194 }
195 const auto *ptr = vec.data() + index;
196 this->write_command_(cmd, ptr, num_args);
197 index += num_args;
198 if (cmd == SLEEP_OUT)
199 delay(10);
200 }
201 }
202 this->reset_params_();
203 // init sequence no longer needed
204 this->init_sequence_.clear();
205 }
206
207 // Drawing operations
208
209 void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
210 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override {
211 if (this->is_failed())
212 return;
213 if (w <= 0 || h <= 0)
214 return;
215 if (get_pixel_mode(bitness) != BUFFERPIXEL || big_endian != IS_BIG_ENDIAN) {
216 // note that the usual logging macros are banned in header files, so use their replacement
217 esph_log_e(TAG, "Unsupported color depth or bit order");
218 return;
219 }
220 this->write_to_display_(x_start, y_start, w, h, reinterpret_cast<const BUFFERTYPE *>(ptr), x_offset, y_offset,
221 x_pad);
222 }
223
224 void dump_config() override {
225 internal_dump_config(this->model_, this->get_width(), this->get_height(), this->get_offset_width_(),
226 this->get_offset_height_(), (uint8_t) MADCTL, this->invert_colors_, DISPLAYPIXEL * 8,
227 IS_BIG_ENDIAN, this->brightness_, this->cs_, this->reset_pin_, this->dc_pin_, this->mode_,
228 this->data_rate_, BUS_TYPE, HAS_HARDWARE_ROTATION);
229 }
230
231 protected:
232 /* METHODS */
233 // If hardware rotation is in use, the actual display width/height changes with rotation
234 int get_width_internal() override {
235 if constexpr (HAS_HARDWARE_ROTATION)
236 return get_width();
237 return WIDTH;
238 }
239 int get_height_internal() override {
240 if constexpr (HAS_HARDWARE_ROTATION)
241 return get_height();
242 return HEIGHT;
243 }
244 // convenience functions to write commands with or without data
245 void write_command_(uint8_t cmd, uint8_t data) { this->write_command_(cmd, &data, 1); }
246 void write_command_(uint8_t cmd) { this->write_command_(cmd, &cmd, 0); }
247
248 // Writes a command to the display, with the given bytes.
249 void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) {
250 char hex_buf[format_hex_pretty_size(MIPI_SPI_MAX_CMD_LOG_BYTES)];
251 // Don't spam the log after setup
252 if (this->init_sequence_.empty()) {
253 esph_log_v(TAG, "Command %02X, length %d, bytes %s", cmd, len, format_hex_pretty_to(hex_buf, bytes, len));
254 } else {
255 esph_log_d(TAG, "Command %02X, length %d, bytes %s", cmd, len, format_hex_pretty_to(hex_buf, bytes, len));
256 }
257 if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
258 this->enable();
259 this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len);
260 this->disable();
261 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
262 this->dc_pin_->digital_write(false);
263 this->enable();
264 this->write_cmd_addr_data(0, 0, 0, 0, &cmd, 1, 8);
265 this->disable();
266 this->dc_pin_->digital_write(true);
267 if (len != 0) {
268 this->enable();
269 this->write_cmd_addr_data(0, 0, 0, 0, bytes, len, 8);
270 this->disable();
271 }
272 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE) {
273 this->dc_pin_->digital_write(false);
274 this->enable();
275 this->write_byte(cmd);
276 this->disable();
277 this->dc_pin_->digital_write(true);
278 if (len != 0) {
279 this->enable();
280 this->write_array(bytes, len);
281 this->disable();
282 }
283 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE_16) {
284 this->dc_pin_->digital_write(false);
285 this->enable();
286 this->write_byte(cmd);
287 this->disable();
288 this->dc_pin_->digital_write(true);
289 for (size_t i = 0; i != len; i++) {
290 this->enable();
291 this->write_byte(0);
292 this->write_byte(bytes[i]);
293 this->disable();
294 }
295 }
296 }
297
298 // write changed parameters to the display
300 if (!this->is_ready())
301 return;
302 this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
303 if (this->brightness_.has_value())
304 this->write_command_(BRIGHTNESS, this->brightness_.value());
305
306 // calculate new madctl value from base value adjusted for rotation
307 uint8_t madctl = (uint8_t) MADCTL; // lower 8 bits only
308 constexpr bool use_flips = (MADCTL & MADCTL_FLIP_FLAG) != 0;
309 constexpr uint8_t x_mask = use_flips ? MADCTL_XFLIP : MADCTL_MX;
310 constexpr uint8_t y_mask = use_flips ? MADCTL_YFLIP : MADCTL_MY;
311 if constexpr (HAS_HARDWARE_ROTATION) {
312 switch (this->rotation_) {
313 default:
314 break;
316 madctl ^= x_mask; // flip X axis
317 madctl ^= MADCTL_MV; // swap X and Y axes
318 break;
320 madctl ^= x_mask; // flip X axis
321 madctl ^= y_mask; // flip Y axis
322 break;
324 madctl ^= y_mask; // flip Y axis
325 madctl ^= MADCTL_MV; // swap X and Y axes
326 break;
327 }
328 }
329 esph_log_d(TAG, "Setting MADCTL for rotation %d, value %X", this->rotation_, madctl);
330 this->write_command_(MADCTL_CMD, madctl);
331 }
332
333 uint16_t get_offset_width_() const {
334 if constexpr (HAS_HARDWARE_ROTATION) {
335 switch (this->rotation_) {
337 return OFFSET_HEIGHT;
339 return PAD_WIDTH;
341 return PAD_HEIGHT;
342 default:
343 break;
344 }
345 }
346 return OFFSET_WIDTH;
347 }
348
349 uint16_t get_offset_height_() const {
350 if constexpr (HAS_HARDWARE_ROTATION) {
351 switch (this->rotation_) {
353 return PAD_WIDTH;
355 return PAD_HEIGHT;
357 return OFFSET_WIDTH;
358 default:
359 break;
360 }
361 }
362 return OFFSET_HEIGHT;
363 }
364
365 // set the address window for the next data write
366 void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
367 esph_log_v(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
368 uint8_t buf[4];
369 x1 += get_offset_width_();
370 x2 += get_offset_width_();
371 y1 += get_offset_height_();
372 y2 += get_offset_height_();
373 put16_be(buf, y1);
374 put16_be(buf + 2, y2);
375 this->write_command_(RASET, buf, sizeof buf);
376 put16_be(buf, x1);
377 put16_be(buf + 2, x2);
378 this->write_command_(CASET, buf, sizeof buf);
379 if constexpr (BUS_TYPE != BUS_TYPE_QUAD) {
380 this->write_command_(WDATA);
381 }
382 }
383
384 // map the display color bitness to the pixel mode
386 switch (bitness) {
388 return PIXEL_MODE_18; // 18 bits per pixel
390 return PIXEL_MODE_16; // 16 bits per pixel
391 default:
392 return PIXEL_MODE_8; // Default to 8 bits per pixel
393 }
394 }
395
403 void write_display_data_(const uint8_t *ptr, size_t w, size_t h, size_t pad) {
404 if (pad == 0) {
405 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
406 this->write_array(ptr, w * h);
407 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
408 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w * h, 4);
409 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
410 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w * h, 8);
411 }
412 } else {
413 for (size_t y = 0; y != h; y++) {
414 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
415 this->write_array(ptr, w);
416 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
417 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w, 4);
418 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
419 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w, 8);
420 }
421 ptr += w + pad;
422 }
423 }
424 }
425
432 void write_to_display_(int x_start, int y_start, int w, int h, const BUFFERTYPE *ptr, int x_offset, int y_offset,
433 int x_pad) {
434 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
435 this->enable();
436 ptr += y_offset * (x_offset + w + x_pad) + x_offset;
437 if constexpr (BUFFERPIXEL == DISPLAYPIXEL) {
438 this->write_display_data_(reinterpret_cast<const uint8_t *>(ptr), w * sizeof(BUFFERTYPE), h,
439 x_pad * sizeof(BUFFERTYPE));
440 } else {
441 // type conversion required, do it in chunks
442 uint8_t dbuffer[DISPLAYPIXEL * 48];
443 uint8_t *dptr = dbuffer;
444 auto stride = x_offset + w + x_pad; // stride in pixels
445 for (size_t y = 0; y != static_cast<size_t>(h); y++) {
446 for (size_t x = 0; x != static_cast<size_t>(w); x++) {
447 auto color_val = ptr[y * stride + x];
448 if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_16) {
449 // 16 to 18 bit conversion
450 if constexpr (IS_BIG_ENDIAN) {
451 *dptr++ = color_val & 0xF8;
452 *dptr++ = ((color_val & 0x7) << 5) | (color_val & 0xE000) >> 11;
453 *dptr++ = (color_val >> 5) & 0xF8;
454 } else {
455 *dptr++ = (color_val >> 8) & 0xF8; // Blue
456 *dptr++ = (color_val & 0x7E0) >> 3;
457 *dptr++ = color_val << 3;
458 }
459 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_8) {
460 // 8 bit to 18 bit conversion
461 *dptr++ = color_val << 6; // Blue
462 *dptr++ = (color_val & 0x1C) << 3; // Green
463 *dptr++ = (color_val & 0xE0); // Red
464 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_16 && BUFFERPIXEL == PIXEL_MODE_8) {
465 if constexpr (IS_BIG_ENDIAN) {
466 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
467 *dptr++ = (color_val & 3) << 3;
468 } else {
469 *dptr++ = (color_val & 3) << 3;
470 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
471 }
472 }
473 // buffer full? Flush.
474 if (dptr == dbuffer + sizeof(dbuffer)) {
475 this->write_display_data_(dbuffer, sizeof(dbuffer), 1, 0);
476 dptr = dbuffer;
477 }
478 }
479 }
480 // flush any remaining data
481 if (dptr != dbuffer) {
482 this->write_display_data_(dbuffer, dptr - dbuffer, 1, 0);
483 }
484 }
485 this->disable();
486 }
487
488 /* PROPERTIES */
489
490 // GPIO pins
492 std::vector<GPIOPin *> enable_pins_{};
493 GPIOPin *dc_pin_{nullptr};
494
495 // other properties set by configuration
497 optional<uint8_t> brightness_{};
498 const char *model_{"Unknown"};
499 std::vector<uint8_t> init_sequence_{};
500};
501
520template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
521 uint16_t WIDTH, uint16_t HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, int PAD_WIDTH, int PAD_HEIGHT,
522 uint16_t MADCTL, bool HAS_HARDWARE_ROTATION, int FRACTION, unsigned ROUNDING>
524 : public MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH,
525 OFFSET_HEIGHT, PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION> {
526 public:
527 // these values define the buffer size needed to write in accordance with the chip pixel alignment
528 // requirements. If the required rounding does not divide the width and height, we round up to the next multiple and
529 // ignore the extra columns and rows when drawing, but use them to write to the display.
530 static constexpr size_t round_buffer(size_t size) { return (size + ROUNDING - 1) / ROUNDING * ROUNDING; }
531
532 MipiSpiBuffer() = default;
533
534 void dump_config() override {
535 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
536 PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION>::dump_config();
537 esph_log_config(TAG,
538 " Rotation: %d°\n"
539 " Buffer pixels: %d bits\n"
540 " Buffer fraction: 1/%d\n"
541 " Buffer bytes: %zu\n"
542 " Draw rounding: %u",
543 this->rotation_, BUFFERPIXEL * 8, FRACTION,
544 sizeof(BUFFERTYPE) * round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION, ROUNDING);
545 }
546
547 void setup() override {
548 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
549 PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION>::setup();
550 RAMAllocator<BUFFERTYPE> allocator{};
551 this->buffer_ = allocator.allocate(round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION);
552 if (this->buffer_ == nullptr) {
553 this->mark_failed(LOG_STR("Buffer allocation failed"));
554 }
555 }
556
557 void update() override {
558#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
559 auto now = millis();
560#endif
561 if (this->is_failed()) {
562 return;
563 }
564 // for updates with a small buffer, we repeatedly call the writer_ function, clipping the height to a fraction of
565 // the display height,
566 auto increment = (this->get_height_internal() / FRACTION / ROUNDING) * ROUNDING;
567 for (this->start_line_ = 0; this->start_line_ < this->get_height_internal(); this->start_line_ = this->end_line_) {
568#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
569 auto lap = millis();
570#endif
571 this->end_line_ = clamp_at_most(this->start_line_ + increment, this->get_height_internal());
572 if (this->auto_clear_enabled_) {
573 this->clear();
574 }
575 if (this->page_ != nullptr) {
576 this->page_->get_writer()(*this);
577 } else if (this->writer_.has_value()) {
578 (*this->writer_)(*this);
579 } else {
580 this->test_card();
581 }
582#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
583 esph_log_v(TAG, "Drawing from line %d took %dms", this->start_line_, millis() - lap);
584 lap = millis();
585#endif
586 if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
587 return;
588 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_,
589 this->y_high_);
590 // Some chips require that the drawing window be aligned on certain boundaries
591 this->x_low_ = this->x_low_ / ROUNDING * ROUNDING;
592 this->y_low_ = this->y_low_ / ROUNDING * ROUNDING;
593 this->x_high_ = round_buffer(this->x_high_ + 1) - 1;
594 this->y_high_ = clamp_at_most(round_buffer(this->y_high_ + 1) - 1, this->end_line_ - 1);
595 int w = this->x_high_ - this->x_low_ + 1;
596 int h = this->y_high_ - this->y_low_ + 1;
597 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_,
598 this->y_low_ - this->start_line_,
599 round_buffer(this->get_width_internal()) - w - this->x_low_);
600 // invalidate watermarks
601 this->x_low_ = this->get_width_internal();
602 this->y_low_ = this->get_height_internal();
603 this->x_high_ = 0;
604 this->y_high_ = 0;
605#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
606 esph_log_v(TAG, "Write to display took %dms", millis() - lap);
607 lap = millis();
608#endif
609 }
610#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
611 esph_log_v(TAG, "Total update took %dms", millis() - now);
612#endif
613 }
614
615 // Draw a pixel at the given coordinates.
616 void draw_pixel_at(int x, int y, Color color) override {
617 if (!this->get_clipping().inside(x, y))
618 return;
619 if constexpr (not HAS_HARDWARE_ROTATION) {
621 x = WIDTH - x - 1;
622 y = HEIGHT - y - 1;
624 auto tmp = x;
625 x = WIDTH - y - 1;
626 y = tmp;
628 auto tmp = y;
629 y = HEIGHT - x - 1;
630 x = tmp;
631 }
632 }
634 return;
635 this->buffer_[(y - this->start_line_) * round_buffer(this->get_width_internal()) + x] = convert_color(color);
636 if (x < this->x_low_) {
637 this->x_low_ = x;
638 }
639 if (x > this->x_high_) {
640 this->x_high_ = x;
641 }
642 if (y < this->y_low_) {
643 this->y_low_ = y;
644 }
645 if (y > this->y_high_) {
646 this->y_high_ = y;
647 }
648 }
649
650 // Fills the display with a color.
651 void fill(Color color) override {
652 // If clipping is active, fall back to base implementation
653 if (this->get_clipping().is_set()) {
655 return;
656 }
657
658 this->x_low_ = 0;
659 this->y_low_ = this->start_line_;
660 this->x_high_ = this->get_width_internal() - 1;
661 this->y_high_ = this->end_line_ - 1;
662 std::fill_n(this->buffer_, (this->end_line_ - this->start_line_) * round_buffer(this->get_width_internal()),
663 convert_color(color));
664 }
665
666 protected:
667 // Rotate the coordinates to match the display orientation.
668
669 // Convert a color to the buffer pixel format.
670 static BUFFERTYPE convert_color(const Color &color) {
671 if constexpr (BUFFERPIXEL == PIXEL_MODE_8) {
672 return (color.red & 0xE0) | (color.g & 0xE0) >> 3 | color.b >> 6;
673 } else if constexpr (BUFFERPIXEL == PIXEL_MODE_16) {
674 if constexpr (IS_BIG_ENDIAN) {
675 return (color.r & 0xF8) | color.g >> 5 | (color.g & 0x1C) << 11 | (color.b & 0xF8) << 5;
676 } else {
677 return (color.r & 0xF8) << 8 | (color.g & 0xFC) << 3 | color.b >> 3;
678 }
679 }
680 return static_cast<BUFFERTYPE>(0);
681 }
682
683 BUFFERTYPE *buffer_{};
684 uint16_t x_low_{WIDTH};
685 uint16_t y_low_{HEIGHT};
686 uint16_t x_high_{0};
687 uint16_t y_high_{0};
688 uint16_t start_line_{0};
689 uint16_t end_line_{1};
690};
691
692} // namespace esphome::mipi_spi
uint8_t h
Definition bl0906.h:2
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
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:2053
T * allocate(size_t n)
Definition helpers.h:2080
display_writer_t writer_
Definition display.h:790
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:15
virtual void fill(Color color)
Fill the entire screen with the given color.
Definition display.cpp:14
DisplayPage * page_
Definition display.h:791
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
DisplayRotation rotation_
Definition display.h:789
const display_writer_t & get_writer() const
Definition display.cpp:898
Class for MIPI SPI displays with a buffer.
Definition mipi_spi.h:525
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:616
static constexpr size_t round_buffer(size_t size)
Definition mipi_spi.h:530
void fill(Color color) override
Definition mipi_spi.h:651
static BUFFERTYPE convert_color(const Color &color)
Definition mipi_spi.h:670
Base class for MIPI SPI displays.
Definition mipi_spi.h:95
void update() override
Definition mipi_spi.h:98
uint16_t get_offset_height_() const
Definition mipi_spi.h:349
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:432
int get_width_internal() override
Definition mipi_spi.h:234
static PixelMode get_pixel_mode(display::ColorBitness bitness)
Definition mipi_spi.h:385
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
Definition mipi_spi.h:249
void set_reset_pin(GPIOPin *reset_pin)
Definition mipi_spi.h:101
int get_width() override
Definition mipi_spi.h:120
optional< uint8_t > brightness_
Definition mipi_spi.h:497
void set_invert_colors(bool invert_colors)
Definition mipi_spi.h:104
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
Definition mipi_spi.h:209
int get_height_internal() override
Definition mipi_spi.h:239
void write_display_data_(const uint8_t *ptr, size_t w, size_t h, size_t pad)
Writes a buffer to the display.
Definition mipi_spi.h:403
void dump_config() override
Definition mipi_spi.h:224
uint16_t get_offset_width_() const
Definition mipi_spi.h:333
void set_brightness(uint8_t brightness)
Definition mipi_spi.h:108
int get_height() override
Definition mipi_spi.h:127
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:99
void write_command_(uint8_t cmd)
Definition mipi_spi.h:246
display::DisplayType get_display_type() override
Definition mipi_spi.h:118
std::vector< uint8_t > init_sequence_
Definition mipi_spi.h:499
void set_dc_pin(GPIOPin *dc_pin)
Definition mipi_spi.h:103
void set_rotation(display::DisplayRotation rotation) override
Definition mipi_spi.h:112
void write_command_(uint8_t cmd, uint8_t data)
Definition mipi_spi.h:245
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition mipi_spi.h:366
void setup() override
Definition mipi_spi.h:137
void set_init_sequence(const std::vector< uint8_t > &sequence)
Definition mipi_spi.h:134
void set_model(const char *model)
Definition mipi_spi.h:100
std::vector< GPIOPin * > enable_pins_
Definition mipi_spi.h:492
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition mipi_spi.h:102
uint32_t data_rate_
Definition spi.h:412
The SPIDevice is what components using the SPI will create.
Definition spi.h:429
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:473
uint16_t reset
Definition ina226.h:5
uint8_t duration
Definition msa3xx.h:0
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:137
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:136
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:135
const uint8_t MADCTL_CMD
Definition mipi_dsi.h:25
const uint8_t MADCTL_YFLIP
Definition mipi_dsi.h:36
const uint8_t INVERT_ON
Definition mipi_dsi.h:27
const uint8_t DISPLAY_ON
Definition mipi_dsi.h:28
const uint8_t MADCTL_MV
Definition mipi_dsi.h:34
const uint8_t MADCTL_MX
Definition mipi_dsi.h:32
const uint8_t MADCTL_MY
Definition mipi_dsi.h:33
const uint8_t MADCTL_XFLIP
Definition mipi_dsi.h:35
const uint8_t INVERT_OFF
Definition mipi_dsi.h:26
const uint8_t DELAY_FLAG
Definition mipi_dsi.h:30
const uint8_t SLEEP_OUT
Definition mipi_dsi.h:23
const uint8_t SW_RESET_CMD
Definition mipi_dsi.h:22
const uint8_t MADCTL_BGR
Definition mipi_dsi.h:31
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
T clamp_at_most(T value, U max)
Definition helpers.h:2191
const void size_t len
Definition hal.h:64
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:340
uint16_t size
Definition helpers.cpp:25
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:1386
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t
uint8_t red
Definition color.h:31
uint8_t g
Definition color.h:34
uint8_t b
Definition color.h:38
uint8_t r
Definition color.h:30
uint8_t pad
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6