ESPHome 2026.5.1
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
86template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
87 int WIDTH, int HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, uint16_t MADCTL, bool HAS_HARDWARE_ROTATION>
89 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
90 spi::DATA_RATE_1MHZ> {
91 public:
92 MipiSpi() = default;
93 void update() override { this->stop_poller(); }
94 void draw_pixel_at(int x, int y, Color color) override {}
95 void set_model(const char *model) { this->model_ = model; }
96 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
97 void set_enable_pins(std::vector<GPIOPin *> enable_pins) { this->enable_pins_ = std::move(enable_pins); }
98 void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
99 void set_invert_colors(bool invert_colors) {
100 this->invert_colors_ = invert_colors;
101 this->reset_params_();
102 }
103 void set_brightness(uint8_t brightness) {
104 this->brightness_ = brightness;
105 this->reset_params_();
106 }
107 void set_rotation(display::DisplayRotation rotation) override {
108 this->rotation_ = rotation;
109 if constexpr (HAS_HARDWARE_ROTATION) {
110 this->reset_params_();
111 }
112 }
114
115 int get_width() override {
118 return HEIGHT;
119 return WIDTH;
120 }
121
122 int get_height() override {
125 return WIDTH;
126 return HEIGHT;
127 }
128
129 // If hardware rotation is in use, the actual display width/height changes with rotation
130 int get_width_internal() override {
131 if constexpr (HAS_HARDWARE_ROTATION)
132 return get_width();
133 return WIDTH;
134 }
135 int get_height_internal() override {
136 if constexpr (HAS_HARDWARE_ROTATION)
137 return get_height();
138 return HEIGHT;
139 }
140 void set_init_sequence(const std::vector<uint8_t> &sequence) { this->init_sequence_ = sequence; }
141
142 // reset the display, and write the init sequence
143 void setup() override {
144 this->spi_setup();
145 if (this->dc_pin_ != nullptr) {
146 this->dc_pin_->setup();
147 this->dc_pin_->digital_write(false);
148 }
149 for (auto *pin : this->enable_pins_) {
150 pin->setup();
151 pin->digital_write(true);
152 }
153 if (this->reset_pin_ != nullptr) {
154 this->reset_pin_->setup();
155 this->reset_pin_->digital_write(true);
156 delay(5);
157 this->reset_pin_->digital_write(false);
158 delay(5);
159 this->reset_pin_->digital_write(true);
160 }
161
162 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
163 auto when = millis() + 120;
164 delay(10);
165 size_t index = 0;
166 auto &vec = this->init_sequence_;
167 while (index != vec.size()) {
168 if (vec.size() - index < 2) {
169 esph_log_e(TAG, "Malformed init sequence");
170 this->mark_failed();
171 return;
172 }
173 uint8_t cmd = vec[index++];
174 uint8_t x = vec[index++];
175 if (x == DELAY_FLAG) {
176 esph_log_d(TAG, "Delay %dms", cmd);
177 delay(cmd);
178 } else {
179 uint8_t num_args = x & 0x7F;
180 if (vec.size() - index < num_args) {
181 esph_log_e(TAG, "Malformed init sequence");
182 this->mark_failed();
183 return;
184 }
185 auto arg_byte = vec[index];
186 switch (cmd) {
187 case SLEEP_OUT: {
188 // are we ready, boots?
189 int duration = when - millis();
190 if (duration > 0) {
191 esph_log_d(TAG, "Sleep %dms", duration);
193 }
194 } break;
195
196 case INVERT_ON:
197 this->invert_colors_ = true;
198 break;
199 case BRIGHTNESS:
200 this->brightness_ = arg_byte;
201 break;
202
203 default:
204 break;
205 }
206 const auto *ptr = vec.data() + index;
207 this->write_command_(cmd, ptr, num_args);
208 index += num_args;
209 if (cmd == SLEEP_OUT)
210 delay(10);
211 }
212 }
213 this->reset_params_();
214 // init sequence no longer needed
215 this->init_sequence_.clear();
216 }
217
218 // Drawing operations
219
220 void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
221 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override {
222 if (this->is_failed())
223 return;
224 if (w <= 0 || h <= 0)
225 return;
226 if (get_pixel_mode(bitness) != BUFFERPIXEL || big_endian != IS_BIG_ENDIAN) {
227 // note that the usual logging macros are banned in header files, so use their replacement
228 esph_log_e(TAG, "Unsupported color depth or bit order");
229 return;
230 }
231 this->write_to_display_(x_start, y_start, w, h, reinterpret_cast<const BUFFERTYPE *>(ptr), x_offset, y_offset,
232 x_pad);
233 }
234
235 void dump_config() override {
236 internal_dump_config(this->model_, this->get_width(), this->get_height(), OFFSET_WIDTH, OFFSET_HEIGHT,
237 (uint8_t) MADCTL, this->invert_colors_, DISPLAYPIXEL * 8, IS_BIG_ENDIAN, this->brightness_,
238 this->cs_, this->reset_pin_, this->dc_pin_, this->mode_, this->data_rate_, BUS_TYPE,
239 HAS_HARDWARE_ROTATION);
240 }
241
242 protected:
243 /* METHODS */
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_() {
334 if constexpr (HAS_HARDWARE_ROTATION) {
337 return OFFSET_HEIGHT;
338 }
339 return OFFSET_WIDTH;
340 }
341
343 if constexpr (HAS_HARDWARE_ROTATION) {
346 return OFFSET_WIDTH;
347 }
348 return OFFSET_HEIGHT;
349 }
350
351 // set the address window for the next data write
352 void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
353 esph_log_v(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
354 uint8_t buf[4];
355 x1 += get_offset_width_();
356 x2 += get_offset_width_();
357 y1 += get_offset_height_();
358 y2 += get_offset_height_();
359 put16_be(buf, y1);
360 put16_be(buf + 2, y2);
361 this->write_command_(RASET, buf, sizeof buf);
362 put16_be(buf, x1);
363 put16_be(buf + 2, x2);
364 this->write_command_(CASET, buf, sizeof buf);
365 if constexpr (BUS_TYPE != BUS_TYPE_QUAD) {
366 this->write_command_(WDATA);
367 }
368 }
369
370 // map the display color bitness to the pixel mode
372 switch (bitness) {
374 return PIXEL_MODE_18; // 18 bits per pixel
376 return PIXEL_MODE_16; // 16 bits per pixel
377 default:
378 return PIXEL_MODE_8; // Default to 8 bits per pixel
379 }
380 }
381
389 void write_display_data_(const uint8_t *ptr, size_t w, size_t h, size_t pad) {
390 if (pad == 0) {
391 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
392 this->write_array(ptr, w * h);
393 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
394 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w * h, 4);
395 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
396 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w * h, 8);
397 }
398 } else {
399 for (size_t y = 0; y != static_cast<size_t>(h); y++) {
400 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
401 this->write_array(ptr, w);
402 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
403 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w, 4);
404 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
405 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w, 8);
406 }
407 ptr += w + pad;
408 }
409 }
410 }
411
418 void write_to_display_(int x_start, int y_start, int w, int h, const BUFFERTYPE *ptr, int x_offset, int y_offset,
419 int x_pad) {
420 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
421 this->enable();
422 ptr += y_offset * (x_offset + w + x_pad) + x_offset;
423 if constexpr (BUFFERPIXEL == DISPLAYPIXEL) {
424 this->write_display_data_(reinterpret_cast<const uint8_t *>(ptr), w * sizeof(BUFFERTYPE), h,
425 x_pad * sizeof(BUFFERTYPE));
426 } else {
427 // type conversion required, do it in chunks
428 uint8_t dbuffer[DISPLAYPIXEL * 48];
429 uint8_t *dptr = dbuffer;
430 auto stride = x_offset + w + x_pad; // stride in pixels
431 for (size_t y = 0; y != static_cast<size_t>(h); y++) {
432 for (size_t x = 0; x != static_cast<size_t>(w); x++) {
433 auto color_val = ptr[y * stride + x];
434 if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_16) {
435 // 16 to 18 bit conversion
436 if constexpr (IS_BIG_ENDIAN) {
437 *dptr++ = color_val & 0xF8;
438 *dptr++ = ((color_val & 0x7) << 5) | (color_val & 0xE000) >> 11;
439 *dptr++ = (color_val >> 5) & 0xF8;
440 } else {
441 *dptr++ = (color_val >> 8) & 0xF8; // Blue
442 *dptr++ = (color_val & 0x7E0) >> 3;
443 *dptr++ = color_val << 3;
444 }
445 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_8) {
446 // 8 bit to 18 bit conversion
447 *dptr++ = color_val << 6; // Blue
448 *dptr++ = (color_val & 0x1C) << 3; // Green
449 *dptr++ = (color_val & 0xE0); // Red
450 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_16 && BUFFERPIXEL == PIXEL_MODE_8) {
451 if constexpr (IS_BIG_ENDIAN) {
452 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
453 *dptr++ = (color_val & 3) << 3;
454 } else {
455 *dptr++ = (color_val & 3) << 3;
456 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
457 }
458 }
459 // buffer full? Flush.
460 if (dptr == dbuffer + sizeof(dbuffer)) {
461 this->write_display_data_(dbuffer, sizeof(dbuffer), 1, 0);
462 dptr = dbuffer;
463 }
464 }
465 }
466 // flush any remaining data
467 if (dptr != dbuffer) {
468 this->write_display_data_(dbuffer, dptr - dbuffer, 1, 0);
469 }
470 }
471 this->disable();
472 }
473
474 /* PROPERTIES */
475
476 // GPIO pins
478 std::vector<GPIOPin *> enable_pins_{};
479 GPIOPin *dc_pin_{nullptr};
480
481 // other properties set by configuration
483 optional<uint8_t> brightness_{};
484 const char *model_{"Unknown"};
485 std::vector<uint8_t> init_sequence_{};
486};
487
503template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
504 uint16_t WIDTH, uint16_t HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, uint16_t MADCTL,
505 bool HAS_HARDWARE_ROTATION, int FRACTION, unsigned ROUNDING>
506class MipiSpiBuffer : public MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT,
507 OFFSET_WIDTH, OFFSET_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION> {
508 public:
509 // these values define the buffer size needed to write in accordance with the chip pixel alignment
510 // requirements. If the required rounding does not divide the width and height, we round up to the next multiple and
511 // ignore the extra columns and rows when drawing, but use them to write to the display.
512 static constexpr size_t round_buffer(size_t size) { return (size + ROUNDING - 1) / ROUNDING * ROUNDING; }
513
514 MipiSpiBuffer() = default;
515
516 void dump_config() override {
517 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
518 MADCTL, HAS_HARDWARE_ROTATION>::dump_config();
519 esph_log_config(TAG,
520 " Rotation: %d°\n"
521 " Buffer pixels: %d bits\n"
522 " Buffer fraction: 1/%d\n"
523 " Buffer bytes: %zu\n"
524 " Draw rounding: %u",
525 this->rotation_, BUFFERPIXEL * 8, FRACTION,
526 sizeof(BUFFERTYPE) * round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION, ROUNDING);
527 }
528
529 void setup() override {
530 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
531 MADCTL, HAS_HARDWARE_ROTATION>::setup();
532 RAMAllocator<BUFFERTYPE> allocator{};
533 this->buffer_ = allocator.allocate(round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION);
534 if (this->buffer_ == nullptr) {
535 this->mark_failed(LOG_STR("Buffer allocation failed"));
536 }
537 }
538
539 void update() override {
540#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
541 auto now = millis();
542#endif
543 if (this->is_failed()) {
544 return;
545 }
546 // for updates with a small buffer, we repeatedly call the writer_ function, clipping the height to a fraction of
547 // the display height,
548 auto increment = (this->get_height_internal() / FRACTION / ROUNDING) * ROUNDING;
549 for (this->start_line_ = 0; this->start_line_ < this->get_height_internal(); this->start_line_ = this->end_line_) {
550#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
551 auto lap = millis();
552#endif
553 this->end_line_ = clamp_at_most(this->start_line_ + increment, this->get_height_internal());
554 if (this->auto_clear_enabled_) {
555 this->clear();
556 }
557 if (this->page_ != nullptr) {
558 this->page_->get_writer()(*this);
559 } else if (this->writer_.has_value()) {
560 (*this->writer_)(*this);
561 } else {
562 this->test_card();
563 }
564#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
565 esph_log_v(TAG, "Drawing from line %d took %dms", this->start_line_, millis() - lap);
566 lap = millis();
567#endif
568 if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
569 return;
570 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_,
571 this->y_high_);
572 // Some chips require that the drawing window be aligned on certain boundaries
573 this->x_low_ = this->x_low_ / ROUNDING * ROUNDING;
574 this->y_low_ = this->y_low_ / ROUNDING * ROUNDING;
575 this->x_high_ = round_buffer(this->x_high_ + 1) - 1;
576 this->y_high_ = clamp_at_most(round_buffer(this->y_high_ + 1) - 1, this->end_line_ - 1);
577 int w = this->x_high_ - this->x_low_ + 1;
578 int h = this->y_high_ - this->y_low_ + 1;
579 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_,
580 this->y_low_ - this->start_line_,
581 round_buffer(this->get_width_internal()) - w - this->x_low_);
582 // invalidate watermarks
583 this->x_low_ = this->get_width_internal();
584 this->y_low_ = this->get_height_internal();
585 this->x_high_ = 0;
586 this->y_high_ = 0;
587#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
588 esph_log_v(TAG, "Write to display took %dms", millis() - lap);
589 lap = millis();
590#endif
591 }
592#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
593 esph_log_v(TAG, "Total update took %dms", millis() - now);
594#endif
595 }
596
597 // Draw a pixel at the given coordinates.
598 void draw_pixel_at(int x, int y, Color color) override {
599 if (!this->get_clipping().inside(x, y))
600 return;
601 if constexpr (not HAS_HARDWARE_ROTATION) {
603 x = WIDTH - x - 1;
604 y = HEIGHT - y - 1;
606 auto tmp = x;
607 x = WIDTH - y - 1;
608 y = tmp;
610 auto tmp = y;
611 y = HEIGHT - x - 1;
612 x = tmp;
613 }
614 }
616 return;
617 this->buffer_[(y - this->start_line_) * round_buffer(this->get_width_internal()) + x] = convert_color(color);
618 if (x < this->x_low_) {
619 this->x_low_ = x;
620 }
621 if (x > this->x_high_) {
622 this->x_high_ = x;
623 }
624 if (y < this->y_low_) {
625 this->y_low_ = y;
626 }
627 if (y > this->y_high_) {
628 this->y_high_ = y;
629 }
630 }
631
632 // Fills the display with a color.
633 void fill(Color color) override {
634 // If clipping is active, fall back to base implementation
635 if (this->get_clipping().is_set()) {
637 return;
638 }
639
640 this->x_low_ = 0;
641 this->y_low_ = this->start_line_;
642 this->x_high_ = this->get_width_internal() - 1;
643 this->y_high_ = this->end_line_ - 1;
644 std::fill_n(this->buffer_, (this->end_line_ - this->start_line_) * round_buffer(this->get_width_internal()),
645 convert_color(color));
646 }
647
648 protected:
649 // Rotate the coordinates to match the display orientation.
650
651 // Convert a color to the buffer pixel format.
652 static BUFFERTYPE convert_color(const Color &color) {
653 if constexpr (BUFFERPIXEL == PIXEL_MODE_8) {
654 return (color.red & 0xE0) | (color.g & 0xE0) >> 3 | color.b >> 6;
655 } else if constexpr (BUFFERPIXEL == PIXEL_MODE_16) {
656 if constexpr (IS_BIG_ENDIAN) {
657 return (color.r & 0xF8) | color.g >> 5 | (color.g & 0x1C) << 11 | (color.b & 0xF8) << 5;
658 } else {
659 return (color.r & 0xF8) << 8 | (color.g & 0xFC) << 3 | color.b >> 3;
660 }
661 }
662 return static_cast<BUFFERTYPE>(0);
663 }
664
665 BUFFERTYPE *buffer_{};
666 uint16_t x_low_{WIDTH};
667 uint16_t y_low_{HEIGHT};
668 uint16_t x_high_{0};
669 uint16_t y_high_{0};
670 uint16_t start_line_{0};
671 uint16_t end_line_{1};
672};
673
674} // 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:284
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:14
virtual void fill(Color color)
Fill the entire screen with the given color.
Definition display.cpp:13
DisplayPage * page_
Definition display.h:791
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:765
DisplayRotation rotation_
Definition display.h:789
const display_writer_t & get_writer() const
Definition display.cpp:897
Class for MIPI SPI displays with a buffer.
Definition mipi_spi.h:507
void fill(Color color) override
Definition mipi_spi.h:633
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:598
static constexpr size_t round_buffer(size_t size)
Definition mipi_spi.h:512
static BUFFERTYPE convert_color(const Color &color)
Definition mipi_spi.h:652
Base class for MIPI SPI displays.
Definition mipi_spi.h:90
void set_brightness(uint8_t brightness)
Definition mipi_spi.h:103
int get_width() override
Definition mipi_spi.h:115
void set_rotation(display::DisplayRotation rotation) override
Definition mipi_spi.h:107
void dump_config() override
Definition mipi_spi.h:235
void set_reset_pin(GPIOPin *reset_pin)
Definition mipi_spi.h:96
optional< uint8_t > brightness_
Definition mipi_spi.h:483
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:220
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:418
void update() override
Definition mipi_spi.h:93
int get_height_internal() override
Definition mipi_spi.h:135
int get_height() override
Definition mipi_spi.h:122
void set_invert_colors(bool invert_colors)
Definition mipi_spi.h:99
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:94
void set_model(const char *model)
Definition mipi_spi.h:95
void setup() override
Definition mipi_spi.h:143
static PixelMode get_pixel_mode(display::ColorBitness bitness)
Definition mipi_spi.h:371
int get_width_internal() override
Definition mipi_spi.h:130
std::vector< uint8_t > init_sequence_
Definition mipi_spi.h:485
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition mipi_spi.h:97
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:389
void write_command_(uint8_t cmd)
Definition mipi_spi.h:246
uint16_t get_offset_height_()
Definition mipi_spi.h:342
std::vector< GPIOPin * > enable_pins_
Definition mipi_spi.h:478
display::DisplayType get_display_type() override
Definition mipi_spi.h:113
void set_init_sequence(const std::vector< uint8_t > &sequence)
Definition mipi_spi.h:140
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:352
void set_dc_pin(GPIOPin *dc_pin)
Definition mipi_spi.h:98
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
Definition mipi_spi.h:249
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
std::string size_t len
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:341
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