ESPHome 2026.10.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 // Toggle D/C only while holding the bus; on boards where D/C doubles as
250 // another bus signal, driving it while another device owns the bus
251 // corrupts that device's transfer.
252 this->enable();
253 this->dc_pin_->digital_write(false);
254 this->write_cmd_addr_data(0, 0, 0, 0, &cmd, 1, 8);
255 this->dc_pin_->digital_write(true);
256 this->disable();
257 if (len != 0) {
258 this->enable();
259 this->write_cmd_addr_data(0, 0, 0, 0, bytes, len, 8);
260 this->disable();
261 }
262 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE) {
263 this->enable();
264 this->dc_pin_->digital_write(false);
265 this->write_byte(cmd);
266 this->dc_pin_->digital_write(true);
267 this->disable();
268 if (len != 0) {
269 this->enable();
270 this->write_array(bytes, len);
271 this->disable();
272 }
273 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE_16) {
274 this->enable();
275 this->dc_pin_->digital_write(false);
276 this->write_byte(cmd);
277 this->dc_pin_->digital_write(true);
278 this->disable();
279 for (size_t i = 0; i != len; i++) {
280 this->enable();
281 this->write_byte(0);
282 this->write_byte(bytes[i]);
283 this->disable();
284 }
285 }
286 }
287
288 // write changed parameters to the display
290 if (!this->is_ready())
291 return;
292 this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
293 if (this->brightness_.has_value())
294 this->write_command_(BRIGHTNESS, this->brightness_.value());
295
296 // calculate new madctl value from base value adjusted for rotation
297 uint8_t madctl = (uint8_t) MADCTL; // lower 8 bits only
298 constexpr bool use_flips = (MADCTL & MADCTL_FLIP_FLAG) != 0;
299 constexpr uint8_t x_mask = use_flips ? MADCTL_XFLIP : MADCTL_MX;
300 constexpr uint8_t y_mask = use_flips ? MADCTL_YFLIP : MADCTL_MY;
301 if constexpr (HAS_HARDWARE_ROTATION) {
302 switch (this->rotation_) {
303 default:
304 break;
306 madctl ^= x_mask; // flip X axis
307 madctl ^= MADCTL_MV; // swap X and Y axes
308 break;
310 madctl ^= x_mask; // flip X axis
311 madctl ^= y_mask; // flip Y axis
312 break;
314 madctl ^= y_mask; // flip Y axis
315 madctl ^= MADCTL_MV; // swap X and Y axes
316 break;
317 }
318 }
319 esph_log_d(TAG, "Setting MADCTL for rotation %d, value %X", this->rotation_, madctl);
320 this->write_command_(MADCTL_CMD, madctl);
321 }
322
323 uint16_t get_offset_width_() const {
324 if constexpr (HAS_HARDWARE_ROTATION) {
325 switch (this->rotation_) {
327 return OFFSET_HEIGHT;
329 return PAD_WIDTH;
331 return PAD_HEIGHT;
332 default:
333 break;
334 }
335 }
336 return OFFSET_WIDTH;
337 }
338
339 uint16_t get_offset_height_() const {
340 if constexpr (HAS_HARDWARE_ROTATION) {
341 switch (this->rotation_) {
343 return PAD_WIDTH;
345 return PAD_HEIGHT;
347 return OFFSET_WIDTH;
348 default:
349 break;
350 }
351 }
352 return OFFSET_HEIGHT;
353 }
354
355 // set the address window for the next data write
356 void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
357 esph_log_v(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
358 uint8_t buf[4];
359 x1 += get_offset_width_();
360 x2 += get_offset_width_();
361 y1 += get_offset_height_();
362 y2 += get_offset_height_();
363 put16_be(buf, y1);
364 put16_be(buf + 2, y2);
365 this->write_command_(RASET, buf, sizeof buf);
366 put16_be(buf, x1);
367 put16_be(buf + 2, x2);
368 this->write_command_(CASET, buf, sizeof buf);
369 if constexpr (BUS_TYPE != BUS_TYPE_QUAD) {
370 this->write_command_(WDATA);
371 }
372 }
373
374 // map the display color bitness to the pixel mode
376 switch (bitness) {
378 return PIXEL_MODE_18; // 18 bits per pixel
380 return PIXEL_MODE_16; // 16 bits per pixel
381 default:
382 return PIXEL_MODE_8; // Default to 8 bits per pixel
383 }
384 }
385
393 void write_display_data_(const uint8_t *ptr, size_t w, size_t h, size_t stride) {
394 if (stride == w) {
395 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
396 this->write_array(ptr, w * h);
397 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
398 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w * h, 4);
399 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
400 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w * h, 8);
401 }
402 } else {
403 for (size_t y = 0; y != h; y++) {
404 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
405 this->write_array(ptr, w);
406 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
407 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w, 4);
408 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
409 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w, 8);
410 }
411 ptr += stride;
412 }
413 }
414 }
415
422 void write_to_display_(int x_start, int y_start, int w, int h, const BUFFERTYPE *ptr, int x_offset, int y_offset,
423 int x_pad) {
424 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
425 this->enable();
426 ptr += y_offset * (x_offset + w + x_pad) + x_offset;
427 if constexpr (BUFFERPIXEL == DISPLAYPIXEL) {
428 this->write_display_data_(reinterpret_cast<const uint8_t *>(ptr), w * sizeof(BUFFERTYPE), h,
429 (x_offset + w + x_pad) * sizeof(BUFFERTYPE));
430 } else {
431 // type conversion required, do it in chunks
432 uint8_t dbuffer[DISPLAYPIXEL * 48];
433 uint8_t *dptr = dbuffer;
434 auto stride = x_offset + w + x_pad; // stride in pixels
435 for (size_t y = 0; y != static_cast<size_t>(h); y++) {
436 for (size_t x = 0; x != static_cast<size_t>(w); x++) {
437 auto color_val = ptr[y * stride + x];
438 if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_16) {
439 // 16 to 18 bit conversion
440 if constexpr (IS_BIG_ENDIAN) {
441 *dptr++ = color_val & 0xF8;
442 *dptr++ = ((color_val & 0x7) << 5) | (color_val & 0xE000) >> 11;
443 *dptr++ = (color_val >> 5) & 0xF8;
444 } else {
445 *dptr++ = (color_val >> 8) & 0xF8; // Blue
446 *dptr++ = (color_val & 0x7E0) >> 3;
447 *dptr++ = color_val << 3;
448 }
449 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_8) {
450 // 8 bit to 18 bit conversion
451 *dptr++ = color_val << 6; // Blue
452 *dptr++ = (color_val & 0x1C) << 3; // Green
453 *dptr++ = (color_val & 0xE0); // Red
454 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_16 && BUFFERPIXEL == PIXEL_MODE_8) {
455 if constexpr (IS_BIG_ENDIAN) {
456 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
457 *dptr++ = (color_val & 3) << 3;
458 } else {
459 *dptr++ = (color_val & 3) << 3;
460 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
461 }
462 }
463 // buffer full? Flush.
464 if (dptr == dbuffer + sizeof(dbuffer)) {
465 this->write_display_data_(dbuffer, sizeof(dbuffer), 1, sizeof(dbuffer));
466 dptr = dbuffer;
467 }
468 }
469 }
470 // flush any remaining data
471 if (dptr != dbuffer) {
472 this->write_display_data_(dbuffer, dptr - dbuffer, 1, dptr - dbuffer);
473 }
474 }
475 this->disable();
476 }
477
478 /* PROPERTIES */
479
480 // GPIO pins
482 std::vector<GPIOPin *> enable_pins_{};
483 GPIOPin *dc_pin_{nullptr};
484
485 // other properties set by configuration
487 optional<uint8_t> brightness_{};
488 const char *model_{"Unknown"};
489 std::vector<uint8_t> init_sequence_{};
490};
491
510template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
511 uint16_t WIDTH, uint16_t HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, int PAD_WIDTH, int PAD_HEIGHT,
512 uint16_t MADCTL, bool HAS_HARDWARE_ROTATION, int FRACTION, unsigned ROUNDING>
514 : public MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH,
515 OFFSET_HEIGHT, PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION> {
516 public:
517 // these values define the buffer size needed to write in accordance with the chip pixel alignment
518 // requirements. If the required rounding does not divide the width and height, we round up to the next multiple and
519 // ignore the extra columns and rows when drawing, but use them to write to the display.
520 static constexpr size_t round_buffer(size_t size) { return (size + ROUNDING - 1) / ROUNDING * ROUNDING; }
521
522 MipiSpiBuffer() = default;
523
524 void dump_config() override {
525 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
526 PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION>::dump_config();
527 esph_log_config(TAG,
528 " Rotation: %d°\n"
529 " Buffer pixels: %d bits\n"
530 " Buffer fraction: 1/%d\n"
531 " Buffer bytes: %zu\n"
532 " Draw rounding: %u",
533 this->rotation_, BUFFERPIXEL * 8, FRACTION,
534 sizeof(BUFFERTYPE) * round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION, ROUNDING);
535 }
536
537 void setup() override {
538 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT,
539 PAD_WIDTH, PAD_HEIGHT, MADCTL, HAS_HARDWARE_ROTATION>::setup();
540 RAMAllocator<BUFFERTYPE> allocator{};
541 this->buffer_ = allocator.allocate(round_buffer(WIDTH) * round_buffer(HEIGHT) / FRACTION);
542 if (this->buffer_ == nullptr) {
543 this->mark_failed(LOG_STR("Buffer allocation failed"));
544 }
545 }
546
547 void update() override {
548#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
549 auto now = millis();
550#endif
551 if (this->is_failed()) {
552 return;
553 }
554 // for updates with a small buffer, we repeatedly call the writer_ function, clipping the height to a fraction of
555 // the display height,
556 auto increment = (this->get_height_internal() / FRACTION / ROUNDING) * ROUNDING;
557 for (this->start_line_ = 0; this->start_line_ < this->get_height_internal(); this->start_line_ = this->end_line_) {
558#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
559 auto lap = millis();
560#endif
561 this->end_line_ = clamp_at_most(this->start_line_ + increment, this->get_height_internal());
562 if (this->auto_clear_enabled_) {
563 this->clear();
564 }
565 if (this->page_ != nullptr) {
566 this->page_->get_writer()(*this);
567 } else if (this->writer_.has_value()) {
568 (*this->writer_)(*this);
569 } else {
570 this->test_card();
571 }
572#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
573 esph_log_v(TAG, "Drawing from line %d took %dms", this->start_line_, millis() - lap);
574 lap = millis();
575#endif
576 if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
577 return;
578 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_,
579 this->y_high_);
580 // Some chips require that the drawing window be aligned on certain boundaries
581 this->x_low_ = this->x_low_ / ROUNDING * ROUNDING;
582 this->y_low_ = this->y_low_ / ROUNDING * ROUNDING;
583 this->x_high_ = round_buffer(this->x_high_ + 1) - 1;
584 this->y_high_ = clamp_at_most(round_buffer(this->y_high_ + 1) - 1, this->end_line_ - 1);
585 int w = this->x_high_ - this->x_low_ + 1;
586 int h = this->y_high_ - this->y_low_ + 1;
587 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_,
588 this->y_low_ - this->start_line_,
589 round_buffer(this->get_width_internal()) - w - this->x_low_);
590 // invalidate watermarks
591 this->x_low_ = this->get_width_internal();
592 this->y_low_ = this->get_height_internal();
593 this->x_high_ = 0;
594 this->y_high_ = 0;
595#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
596 esph_log_v(TAG, "Write to display took %dms", millis() - lap);
597 lap = millis();
598#endif
599 }
600#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
601 esph_log_v(TAG, "Total update took %dms", millis() - now);
602#endif
603 }
604
605 // Draw a pixel at the given coordinates.
606 void draw_pixel_at(int x, int y, Color color) override {
607 if (!this->get_clipping().inside(x, y))
608 return;
609 if constexpr (not HAS_HARDWARE_ROTATION) {
611 x = WIDTH - x - 1;
612 y = HEIGHT - y - 1;
614 auto tmp = x;
615 x = WIDTH - y - 1;
616 y = tmp;
618 auto tmp = y;
619 y = HEIGHT - x - 1;
620 x = tmp;
621 }
622 }
624 return;
625 this->buffer_[(y - this->start_line_) * round_buffer(this->get_width_internal()) + x] = convert_color(color);
626 if (x < this->x_low_) {
627 this->x_low_ = x;
628 }
629 if (x > this->x_high_) {
630 this->x_high_ = x;
631 }
632 if (y < this->y_low_) {
633 this->y_low_ = y;
634 }
635 if (y > this->y_high_) {
636 this->y_high_ = y;
637 }
638 }
639
640 // Fills the display with a color.
641 void fill(Color color) override {
642 // If clipping is active, fall back to base implementation
643 if (this->get_clipping().is_set()) {
645 return;
646 }
647
648 this->x_low_ = 0;
649 this->y_low_ = this->start_line_;
650 this->x_high_ = this->get_width_internal() - 1;
651 this->y_high_ = this->end_line_ - 1;
652 std::fill_n(this->buffer_, (this->end_line_ - this->start_line_) * round_buffer(this->get_width_internal()),
653 convert_color(color));
654 }
655
656 protected:
657 // Rotate the coordinates to match the display orientation.
658
659 // Convert a color to the buffer pixel format.
660 static BUFFERTYPE convert_color(const Color &color) {
661 if constexpr (BUFFERPIXEL == PIXEL_MODE_8) {
662 return (color.red & 0xE0) | (color.g & 0xE0) >> 3 | color.b >> 6;
663 } else if constexpr (BUFFERPIXEL == PIXEL_MODE_16) {
664 if constexpr (IS_BIG_ENDIAN) {
665 return (color.r & 0xF8) | color.g >> 5 | (color.g & 0x1C) << 11 | (color.b & 0xF8) << 5;
666 } else {
667 return (color.r & 0xF8) << 8 | (color.g & 0xFC) << 3 | color.b >> 3;
668 }
669 }
670 return static_cast<BUFFERTYPE>(0);
671 }
672
673 BUFFERTYPE *buffer_{};
674 uint16_t x_low_{WIDTH};
675 uint16_t y_low_{HEIGHT};
676 uint16_t x_high_{0};
677 uint16_t y_high_{0};
678 uint16_t start_line_{0};
679 uint16_t end_line_{1};
680};
681
682} // 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:2107
T * allocate(size_t n)
Definition helpers.h:2134
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:763
DisplayRotation rotation_
Definition display.h:789
const display_writer_t & get_writer() const
Definition display.cpp:892
Class for MIPI SPI displays with a buffer.
Definition mipi_spi.h:515
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:606
static constexpr size_t round_buffer(size_t size)
Definition mipi_spi.h:520
void fill(Color color) override
Definition mipi_spi.h:641
static BUFFERTYPE convert_color(const Color &color)
Definition mipi_spi.h:660
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:339
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:422
int get_width_internal() override
Definition mipi_spi.h:221
static PixelMode get_pixel_mode(display::ColorBitness bitness)
Definition mipi_spi.h:375
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:487
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:323
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:393
display::DisplayType get_display_type() override
Definition mipi_spi.h:120
std::vector< uint8_t > init_sequence_
Definition mipi_spi.h:489
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:356
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:482
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition mipi_spi.h:104
uint32_t data_rate_
Definition spi.h:430
The SPIDevice is what components using the SPI will create.
Definition spi.h:450
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:494
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:2245
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:425
uint16_t size
Definition helpers.cpp:25
T clamp_at_least(T value, U min)
Definition helpers.h:2240
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:1438
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