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