ESPHome 2026.5.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 {
11namespace mipi_spi {
12
13constexpr static const char *const TAG = "display.mipi_spi";
14
15// Maximum bytes to log for commands (truncated if larger)
16static constexpr size_t MIPI_SPI_MAX_CMD_LOG_BYTES = 64;
17static constexpr uint8_t SW_RESET_CMD = 0x01;
18static constexpr uint8_t SLEEP_OUT = 0x11;
19static constexpr uint8_t NORON = 0x13;
20static constexpr uint8_t INVERT_OFF = 0x20;
21static constexpr uint8_t INVERT_ON = 0x21;
22static constexpr uint8_t ALL_ON = 0x23;
23static constexpr uint8_t WRAM = 0x24;
24static constexpr uint8_t MIPI = 0x26;
25static constexpr uint8_t DISPLAY_ON = 0x29;
26static constexpr uint8_t RASET = 0x2B;
27static constexpr uint8_t CASET = 0x2A;
28static constexpr uint8_t WDATA = 0x2C;
29static constexpr uint8_t TEON = 0x35;
30static constexpr uint8_t MADCTL_CMD = 0x36;
31static constexpr uint8_t PIXFMT = 0x3A;
32static constexpr uint8_t BRIGHTNESS = 0x51;
33static constexpr uint8_t SWIRE1 = 0x5A;
34static constexpr uint8_t SWIRE2 = 0x5B;
35static constexpr uint8_t PAGESEL = 0xFE;
36
37static constexpr uint8_t MADCTL_MY = 0x80; // Bit 7 Bottom to top
38static constexpr uint8_t MADCTL_MX = 0x40; // Bit 6 Right to left
39static constexpr uint8_t MADCTL_MV = 0x20; // Bit 5 Swap axes
40static constexpr uint8_t MADCTL_RGB = 0x00; // Bit 3 Red-Green-Blue pixel order
41static constexpr uint8_t MADCTL_BGR = 0x08; // Bit 3 Blue-Green-Red pixel order
42static constexpr uint8_t MADCTL_XFLIP = 0x02; // Mirror the display horizontally
43static constexpr uint8_t MADCTL_YFLIP = 0x01; // Mirror the display vertically
44static constexpr uint16_t MADCTL_FLIP_FLAG = 0x100; // controller uses axis flip bits
45
46static constexpr uint8_t DELAY_FLAG = 0xFF;
47// store a 16 bit value in a buffer, big endian.
48static inline void put16_be(uint8_t *buf, uint16_t value) {
49 buf[0] = value >> 8;
50 buf[1] = value;
51}
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
87template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
88 int WIDTH, int HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, uint16_t MADCTL, bool HAS_HARDWARE_ROTATION>
90 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
91 spi::DATA_RATE_1MHZ> {
92 public:
93 MipiSpi() = default;
94 void update() override { this->stop_poller(); }
95 void draw_pixel_at(int x, int y, Color color) override {}
96 void set_model(const char *model) { this->model_ = model; }
97 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
98 void set_enable_pins(std::vector<GPIOPin *> enable_pins) { this->enable_pins_ = std::move(enable_pins); }
99 void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
100 void set_invert_colors(bool invert_colors) {
101 this->invert_colors_ = invert_colors;
102 this->reset_params_();
103 }
104 void set_brightness(uint8_t brightness) {
105 this->brightness_ = brightness;
106 this->reset_params_();
107 }
108 void set_rotation(display::DisplayRotation rotation) override {
109 this->rotation_ = rotation;
110 if constexpr (HAS_HARDWARE_ROTATION) {
111 this->reset_params_();
112 }
113 }
115
116 int get_width() override {
119 return HEIGHT;
120 return WIDTH;
121 }
122
123 int get_height() override {
126 return WIDTH;
127 return HEIGHT;
128 }
129
130 // If hardware rotation is in use, the actual display width/height changes with rotation
131 int get_width_internal() override {
132 if constexpr (HAS_HARDWARE_ROTATION)
133 return get_width();
134 return WIDTH;
135 }
136 int get_height_internal() override {
137 if constexpr (HAS_HARDWARE_ROTATION)
138 return get_height();
139 return HEIGHT;
140 }
141 void set_init_sequence(const std::vector<uint8_t> &sequence) { this->init_sequence_ = sequence; }
142
143 // reset the display, and write the init sequence
144 void setup() override {
145 this->spi_setup();
146 if (this->dc_pin_ != nullptr) {
147 this->dc_pin_->setup();
148 this->dc_pin_->digital_write(false);
149 }
150 for (auto *pin : this->enable_pins_) {
151 pin->setup();
152 pin->digital_write(true);
153 }
154 if (this->reset_pin_ != nullptr) {
155 this->reset_pin_->setup();
156 this->reset_pin_->digital_write(true);
157 delay(5);
158 this->reset_pin_->digital_write(false);
159 delay(5);
160 this->reset_pin_->digital_write(true);
161 }
162
163 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
164 auto when = millis() + 120;
165 delay(10);
166 size_t index = 0;
167 auto &vec = this->init_sequence_;
168 while (index != vec.size()) {
169 if (vec.size() - index < 2) {
170 esph_log_e(TAG, "Malformed init sequence");
171 this->mark_failed();
172 return;
173 }
174 uint8_t cmd = vec[index++];
175 uint8_t x = vec[index++];
176 if (x == DELAY_FLAG) {
177 esph_log_d(TAG, "Delay %dms", cmd);
178 delay(cmd);
179 } else {
180 uint8_t num_args = x & 0x7F;
181 if (vec.size() - index < num_args) {
182 esph_log_e(TAG, "Malformed init sequence");
183 this->mark_failed();
184 return;
185 }
186 auto arg_byte = vec[index];
187 switch (cmd) {
188 case SLEEP_OUT: {
189 // are we ready, boots?
190 int duration = when - millis();
191 if (duration > 0) {
192 esph_log_d(TAG, "Sleep %dms", duration);
194 }
195 } break;
196
197 case INVERT_ON:
198 this->invert_colors_ = true;
199 break;
200 case BRIGHTNESS:
201 this->brightness_ = arg_byte;
202 break;
203
204 default:
205 break;
206 }
207 const auto *ptr = vec.data() + index;
208 this->write_command_(cmd, ptr, num_args);
209 index += num_args;
210 if (cmd == SLEEP_OUT)
211 delay(10);
212 }
213 }
214 this->reset_params_();
215 // init sequence no longer needed
216 this->init_sequence_.clear();
217 }
218
219 // Drawing operations
220
221 void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
222 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override {
223 if (this->is_failed())
224 return;
225 if (w <= 0 || h <= 0)
226 return;
227 if (get_pixel_mode(bitness) != BUFFERPIXEL || big_endian != IS_BIG_ENDIAN) {
228 // note that the usual logging macros are banned in header files, so use their replacement
229 esph_log_e(TAG, "Unsupported color depth or bit order");
230 return;
231 }
232 this->write_to_display_(x_start, y_start, w, h, reinterpret_cast<const BUFFERTYPE *>(ptr), x_offset, y_offset,
233 x_pad);
234 }
235
236 void dump_config() override {
237 internal_dump_config(this->model_, this->get_width(), this->get_height(), OFFSET_WIDTH, OFFSET_HEIGHT,
238 (uint8_t) MADCTL, this->invert_colors_, DISPLAYPIXEL * 8, IS_BIG_ENDIAN, this->brightness_,
239 this->cs_, this->reset_pin_, this->dc_pin_, this->mode_, this->data_rate_, BUS_TYPE,
240 HAS_HARDWARE_ROTATION);
241 }
242
243 protected:
244 /* METHODS */
245 // convenience functions to write commands with or without data
246 void write_command_(uint8_t cmd, uint8_t data) { this->write_command_(cmd, &data, 1); }
247 void write_command_(uint8_t cmd) { this->write_command_(cmd, &cmd, 0); }
248
249 // Writes a command to the display, with the given bytes.
250 void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) {
251 char hex_buf[format_hex_pretty_size(MIPI_SPI_MAX_CMD_LOG_BYTES)];
252 // Don't spam the log after setup
253 if (this->init_sequence_.empty()) {
254 esph_log_v(TAG, "Command %02X, length %d, bytes %s", cmd, len, format_hex_pretty_to(hex_buf, bytes, len));
255 } else {
256 esph_log_d(TAG, "Command %02X, length %d, bytes %s", cmd, len, format_hex_pretty_to(hex_buf, bytes, len));
257 }
258 if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
259 this->enable();
260 this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len);
261 this->disable();
262 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
263 this->dc_pin_->digital_write(false);
264 this->enable();
265 this->write_cmd_addr_data(0, 0, 0, 0, &cmd, 1, 8);
266 this->disable();
267 this->dc_pin_->digital_write(true);
268 if (len != 0) {
269 this->enable();
270 this->write_cmd_addr_data(0, 0, 0, 0, bytes, len, 8);
271 this->disable();
272 }
273 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE) {
274 this->dc_pin_->digital_write(false);
275 this->enable();
276 this->write_byte(cmd);
277 this->disable();
278 this->dc_pin_->digital_write(true);
279 if (len != 0) {
280 this->enable();
281 this->write_array(bytes, len);
282 this->disable();
283 }
284 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE_16) {
285 this->dc_pin_->digital_write(false);
286 this->enable();
287 this->write_byte(cmd);
288 this->disable();
289 this->dc_pin_->digital_write(true);
290 for (size_t i = 0; i != len; i++) {
291 this->enable();
292 this->write_byte(0);
293 this->write_byte(bytes[i]);
294 this->disable();
295 }
296 }
297 }
298
299 // write changed parameters to the display
301 if (!this->is_ready())
302 return;
303 this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
304 if (this->brightness_.has_value())
305 this->write_command_(BRIGHTNESS, this->brightness_.value());
306
307 // calculate new madctl value from base value adjusted for rotation
308 uint8_t madctl = (uint8_t) MADCTL; // lower 8 bits only
309 constexpr bool use_flips = (MADCTL & MADCTL_FLIP_FLAG) != 0;
310 constexpr uint8_t x_mask = use_flips ? MADCTL_XFLIP : MADCTL_MX;
311 constexpr uint8_t y_mask = use_flips ? MADCTL_YFLIP : MADCTL_MY;
312 if constexpr (HAS_HARDWARE_ROTATION) {
313 switch (this->rotation_) {
314 default:
315 break;
317 madctl ^= x_mask; // flip X axis
318 madctl ^= MADCTL_MV; // swap X and Y axes
319 break;
321 madctl ^= x_mask; // flip X axis
322 madctl ^= y_mask; // flip Y axis
323 break;
325 madctl ^= y_mask; // flip Y axis
326 madctl ^= MADCTL_MV; // swap X and Y axes
327 break;
328 }
329 }
330 esph_log_d(TAG, "Setting MADCTL for rotation %d, value %X", this->rotation_, madctl);
331 this->write_command_(MADCTL_CMD, madctl);
332 }
333
334 uint16_t get_offset_width_() {
335 if constexpr (HAS_HARDWARE_ROTATION) {
338 return OFFSET_HEIGHT;
339 }
340 return OFFSET_WIDTH;
341 }
342
344 if constexpr (HAS_HARDWARE_ROTATION) {
347 return OFFSET_WIDTH;
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 pad) {
391 if (pad == 0) {
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 != static_cast<size_t>(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 += w + pad;
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_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, 0);
463 dptr = dbuffer;
464 }
465 }
466 }
467 // flush any remaining data
468 if (dptr != dbuffer) {
469 this->write_display_data_(dbuffer, dptr - dbuffer, 1, 0);
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
504template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
505 uint16_t WIDTH, uint16_t HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, uint16_t MADCTL,
506 bool HAS_HARDWARE_ROTATION, int FRACTION, unsigned ROUNDING>
507class MipiSpiBuffer : public MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT,
508 OFFSET_WIDTH, OFFSET_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION> {
509 public:
510 // these values define the buffer size needed to write in accordance with the chip pixel alignment
511 // requirements. If the required rounding does not divide the width and height, we round up to the next multiple and
512 // ignore the extra columns and rows when drawing, but use them to write to the display.
513 static constexpr size_t round_buffer(size_t size) { return (size + ROUNDING - 1) / ROUNDING * ROUNDING; }
514
515 MipiSpiBuffer() = default;
516
517 void dump_config() override {
518 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
519 MADCTL, HAS_HARDWARE_ROTATION>::dump_config();
520 esph_log_config(TAG,
521 " Rotation: %d°\n"
522 " Buffer pixels: %d bits\n"
523 " Buffer fraction: 1/%d\n"
524 " Buffer bytes: %zu\n"
525 " Draw rounding: %u",
526 this->rotation_, BUFFERPIXEL * 8, FRACTION,
527 sizeof(BUFFERTYPE) * round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION, ROUNDING);
528 }
529
530 void setup() override {
531 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
532 MADCTL, HAS_HARDWARE_ROTATION>::setup();
533 RAMAllocator<BUFFERTYPE> allocator{};
534 this->buffer_ = allocator.allocate(round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION);
535 if (this->buffer_ == nullptr) {
536 this->mark_failed(LOG_STR("Buffer allocation failed"));
537 }
538 }
539
540 void update() override {
541#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
542 auto now = millis();
543#endif
544 if (this->is_failed()) {
545 return;
546 }
547 // for updates with a small buffer, we repeatedly call the writer_ function, clipping the height to a fraction of
548 // the display height,
549 auto increment = (this->get_height_internal() / FRACTION / ROUNDING) * ROUNDING;
550 for (this->start_line_ = 0; this->start_line_ < this->get_height_internal(); this->start_line_ = this->end_line_) {
551#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
552 auto lap = millis();
553#endif
554 this->end_line_ = clamp_at_most(this->start_line_ + increment, this->get_height_internal());
555 if (this->auto_clear_enabled_) {
556 this->clear();
557 }
558 if (this->page_ != nullptr) {
559 this->page_->get_writer()(*this);
560 } else if (this->writer_.has_value()) {
561 (*this->writer_)(*this);
562 } else {
563 this->test_card();
564 }
565#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
566 esph_log_v(TAG, "Drawing from line %d took %dms", this->start_line_, millis() - lap);
567 lap = millis();
568#endif
569 if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
570 return;
571 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_,
572 this->y_high_);
573 // Some chips require that the drawing window be aligned on certain boundaries
574 this->x_low_ = this->x_low_ / ROUNDING * ROUNDING;
575 this->y_low_ = this->y_low_ / ROUNDING * ROUNDING;
576 this->x_high_ = round_buffer(this->x_high_ + 1) - 1;
577 this->y_high_ = clamp_at_most(round_buffer(this->y_high_ + 1) - 1, this->end_line_ - 1);
578 int w = this->x_high_ - this->x_low_ + 1;
579 int h = this->y_high_ - this->y_low_ + 1;
580 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_,
581 this->y_low_ - this->start_line_,
582 round_buffer(this->get_width_internal()) - w - this->x_low_);
583 // invalidate watermarks
584 this->x_low_ = this->get_width_internal();
585 this->y_low_ = this->get_height_internal();
586 this->x_high_ = 0;
587 this->y_high_ = 0;
588#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
589 esph_log_v(TAG, "Write to display took %dms", millis() - lap);
590 lap = millis();
591#endif
592 }
593#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
594 esph_log_v(TAG, "Total update took %dms", millis() - now);
595#endif
596 }
597
598 // Draw a pixel at the given coordinates.
599 void draw_pixel_at(int x, int y, Color color) override {
600 if (!this->get_clipping().inside(x, y))
601 return;
602 if constexpr (not HAS_HARDWARE_ROTATION) {
604 x = WIDTH - x - 1;
605 y = HEIGHT - y - 1;
607 auto tmp = x;
608 x = WIDTH - y - 1;
609 y = tmp;
611 auto tmp = y;
612 y = HEIGHT - x - 1;
613 x = tmp;
614 }
615 }
617 return;
618 this->buffer_[(y - this->start_line_) * round_buffer(this->get_width_internal()) + x] = convert_color(color);
619 if (x < this->x_low_) {
620 this->x_low_ = x;
621 }
622 if (x > this->x_high_) {
623 this->x_high_ = x;
624 }
625 if (y < this->y_low_) {
626 this->y_low_ = y;
627 }
628 if (y > this->y_high_) {
629 this->y_high_ = y;
630 }
631 }
632
633 // Fills the display with a color.
634 void fill(Color color) override {
635 // If clipping is active, fall back to base implementation
636 if (this->get_clipping().is_set()) {
638 return;
639 }
640
641 this->x_low_ = 0;
642 this->y_low_ = this->start_line_;
643 this->x_high_ = this->get_width_internal() - 1;
644 this->y_high_ = this->end_line_ - 1;
645 std::fill_n(this->buffer_, (this->end_line_ - this->start_line_) * round_buffer(this->get_width_internal()),
646 convert_color(color));
647 }
648
649 protected:
650 // Rotate the coordinates to match the display orientation.
651
652 // Convert a color to the buffer pixel format.
653 static BUFFERTYPE convert_color(const Color &color) {
654 if constexpr (BUFFERPIXEL == PIXEL_MODE_8) {
655 return (color.red & 0xE0) | (color.g & 0xE0) >> 3 | color.b >> 6;
656 } else if constexpr (BUFFERPIXEL == PIXEL_MODE_16) {
657 if constexpr (IS_BIG_ENDIAN) {
658 return (color.r & 0xF8) | color.g >> 5 | (color.g & 0x1C) << 11 | (color.b & 0xF8) << 5;
659 } else {
660 return (color.r & 0xF8) << 8 | (color.g & 0xFC) << 3 | color.b >> 3;
661 }
662 }
663 return static_cast<BUFFERTYPE>(0);
664 }
665
666 BUFFERTYPE *buffer_{};
667 uint16_t x_low_{WIDTH};
668 uint16_t y_low_{HEIGHT};
669 uint16_t x_high_{0};
670 uint16_t y_high_{0};
671 uint16_t start_line_{0};
672 uint16_t end_line_{1};
673};
674
675} // namespace mipi_spi
676} // namespace esphome
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:2212
T * allocate(size_t n)
Definition helpers.h:2229
display_writer_t writer_
Definition display.h:791
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:792
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
DisplayRotation rotation_
Definition display.h:790
const display_writer_t & get_writer() const
Definition display.cpp:898
Class for MIPI SPI displays with a buffer.
Definition mipi_spi.h:508
void fill(Color color) override
Definition mipi_spi.h:634
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:599
static constexpr size_t round_buffer(size_t size)
Definition mipi_spi.h:513
static BUFFERTYPE convert_color(const Color &color)
Definition mipi_spi.h:653
Base class for MIPI SPI displays.
Definition mipi_spi.h:91
void set_brightness(uint8_t brightness)
Definition mipi_spi.h:104
int get_width() override
Definition mipi_spi.h:116
void set_rotation(display::DisplayRotation rotation) override
Definition mipi_spi.h:108
void dump_config() override
Definition mipi_spi.h:236
void set_reset_pin(GPIOPin *reset_pin)
Definition mipi_spi.h:97
optional< uint8_t > brightness_
Definition mipi_spi.h:484
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:221
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
void update() override
Definition mipi_spi.h:94
int get_height_internal() override
Definition mipi_spi.h:136
int get_height() override
Definition mipi_spi.h:123
void set_invert_colors(bool invert_colors)
Definition mipi_spi.h:100
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:95
void set_model(const char *model)
Definition mipi_spi.h:96
void setup() override
Definition mipi_spi.h:144
static PixelMode get_pixel_mode(display::ColorBitness bitness)
Definition mipi_spi.h:372
int get_width_internal() override
Definition mipi_spi.h:131
std::vector< uint8_t > init_sequence_
Definition mipi_spi.h:486
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition mipi_spi.h:98
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:390
void write_command_(uint8_t cmd)
Definition mipi_spi.h:247
uint16_t get_offset_height_()
Definition mipi_spi.h:343
std::vector< GPIOPin * > enable_pins_
Definition mipi_spi.h:479
display::DisplayType get_display_type() override
Definition mipi_spi.h:114
void set_init_sequence(const std::vector< uint8_t > &sequence)
Definition mipi_spi.h:141
void write_command_(uint8_t cmd, uint8_t data)
Definition mipi_spi.h:246
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition mipi_spi.h:353
void set_dc_pin(GPIOPin *dc_pin)
Definition mipi_spi.h:99
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
Definition mipi_spi.h:250
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:138
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:137
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:136
const uint8_t MADCTL_CMD
Definition mipi_dsi.h:26
const uint8_t MADCTL_YFLIP
Definition mipi_dsi.h:37
const uint8_t INVERT_ON
Definition mipi_dsi.h:28
const uint8_t DISPLAY_ON
Definition mipi_dsi.h:29
const uint8_t MADCTL_MV
Definition mipi_dsi.h:35
const uint8_t MADCTL_MX
Definition mipi_dsi.h:33
const uint8_t MADCTL_MY
Definition mipi_dsi.h:34
const uint8_t MADCTL_XFLIP
Definition mipi_dsi.h:36
const uint8_t INVERT_OFF
Definition mipi_dsi.h:27
const uint8_t DELAY_FLAG
Definition mipi_dsi.h:31
const uint8_t SLEEP_OUT
Definition mipi_dsi.h:24
const uint8_t SW_RESET_CMD
Definition mipi_dsi.h:23
const uint8_t MADCTL_BGR
Definition mipi_dsi.h:32
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
T clamp_at_most(T value, U max)
Definition helpers.h:2330
std::string size_t len
Definition helpers.h:1045
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:409
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:1368
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
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