ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
mipi_spi.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
8
9namespace esphome {
10namespace mipi_spi {
11
12constexpr static const char *const TAG = "display.mipi_spi";
13static constexpr uint8_t SW_RESET_CMD = 0x01;
14static constexpr uint8_t SLEEP_OUT = 0x11;
15static constexpr uint8_t NORON = 0x13;
16static constexpr uint8_t INVERT_OFF = 0x20;
17static constexpr uint8_t INVERT_ON = 0x21;
18static constexpr uint8_t ALL_ON = 0x23;
19static constexpr uint8_t WRAM = 0x24;
20static constexpr uint8_t MIPI = 0x26;
21static constexpr uint8_t DISPLAY_ON = 0x29;
22static constexpr uint8_t RASET = 0x2B;
23static constexpr uint8_t CASET = 0x2A;
24static constexpr uint8_t WDATA = 0x2C;
25static constexpr uint8_t TEON = 0x35;
26static constexpr uint8_t MADCTL_CMD = 0x36;
27static constexpr uint8_t PIXFMT = 0x3A;
28static constexpr uint8_t BRIGHTNESS = 0x51;
29static constexpr uint8_t SWIRE1 = 0x5A;
30static constexpr uint8_t SWIRE2 = 0x5B;
31static constexpr uint8_t PAGESEL = 0xFE;
32
33static constexpr uint8_t MADCTL_MY = 0x80; // Bit 7 Bottom to top
34static constexpr uint8_t MADCTL_MX = 0x40; // Bit 6 Right to left
35static constexpr uint8_t MADCTL_MV = 0x20; // Bit 5 Swap axes
36static constexpr uint8_t MADCTL_RGB = 0x00; // Bit 3 Red-Green-Blue pixel order
37static constexpr uint8_t MADCTL_BGR = 0x08; // Bit 3 Blue-Green-Red pixel order
38static constexpr uint8_t MADCTL_XFLIP = 0x02; // Mirror the display horizontally
39static constexpr uint8_t MADCTL_YFLIP = 0x01; // Mirror the display vertically
40
41static const uint8_t DELAY_FLAG = 0xFF;
42// store a 16 bit value in a buffer, big endian.
43static inline void put16_be(uint8_t *buf, uint16_t value) {
44 buf[0] = value >> 8;
45 buf[1] = value;
46}
47
48// Buffer mode, conveniently also the number of bytes in a pixel
54
55enum BusType {
59 BUS_TYPE_SINGLE_16 = 16, // Single bit bus, but 16 bits per transfer
60};
61
76template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
77 int WIDTH, int HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT>
79 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
80 spi::DATA_RATE_1MHZ> {
81 public:
83 void update() override { this->stop_poller(); }
84 void draw_pixel_at(int x, int y, Color color) override {}
85 void set_model(const char *model) { this->model_ = model; }
86 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
87 void set_enable_pins(std::vector<GPIOPin *> enable_pins) { this->enable_pins_ = std::move(enable_pins); }
88 void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
89 void set_invert_colors(bool invert_colors) {
90 this->invert_colors_ = invert_colors;
91 this->reset_params_();
92 }
93 void set_brightness(uint8_t brightness) {
94 this->brightness_ = brightness;
95 this->reset_params_();
96 }
98
99 int get_width_internal() override { return WIDTH; }
100 int get_height_internal() override { return HEIGHT; }
101 void set_init_sequence(const std::vector<uint8_t> &sequence) { this->init_sequence_ = sequence; }
102 void set_draw_rounding(unsigned rounding) { this->draw_rounding_ = rounding; }
103
104 // reset the display, and write the init sequence
105 void setup() override {
106 this->spi_setup();
107 if (this->dc_pin_ != nullptr) {
108 this->dc_pin_->setup();
109 this->dc_pin_->digital_write(false);
110 }
111 for (auto *pin : this->enable_pins_) {
112 pin->setup();
113 pin->digital_write(true);
114 }
115 if (this->reset_pin_ != nullptr) {
116 this->reset_pin_->setup();
117 this->reset_pin_->digital_write(true);
118 delay(5);
119 this->reset_pin_->digital_write(false);
120 delay(5);
121 this->reset_pin_->digital_write(true);
122 }
123
124 // need to know when the display is ready for SLPOUT command - will be 120ms after reset
125 auto when = millis() + 120;
126 delay(10);
127 size_t index = 0;
128 auto &vec = this->init_sequence_;
129 while (index != vec.size()) {
130 if (vec.size() - index < 2) {
131 esph_log_e(TAG, "Malformed init sequence");
132 this->mark_failed();
133 return;
134 }
135 uint8_t cmd = vec[index++];
136 uint8_t x = vec[index++];
137 if (x == DELAY_FLAG) {
138 esph_log_d(TAG, "Delay %dms", cmd);
139 delay(cmd);
140 } else {
141 uint8_t num_args = x & 0x7F;
142 if (vec.size() - index < num_args) {
143 esph_log_e(TAG, "Malformed init sequence");
144 this->mark_failed();
145 return;
146 }
147 auto arg_byte = vec[index];
148 switch (cmd) {
149 case SLEEP_OUT: {
150 // are we ready, boots?
151 int duration = when - millis();
152 if (duration > 0) {
153 esph_log_d(TAG, "Sleep %dms", duration);
155 }
156 } break;
157
158 case INVERT_ON:
159 this->invert_colors_ = true;
160 break;
161 case MADCTL_CMD:
162 this->madctl_ = arg_byte;
163 break;
164 case BRIGHTNESS:
165 this->brightness_ = arg_byte;
166 break;
167
168 default:
169 break;
170 }
171 const auto *ptr = vec.data() + index;
172 esph_log_d(TAG, "Command %02X, length %d, byte %02X", cmd, num_args, arg_byte);
173 this->write_command_(cmd, ptr, num_args);
174 index += num_args;
175 if (cmd == SLEEP_OUT)
176 delay(10);
177 }
178 }
179 // init sequence no longer needed
180 this->init_sequence_.clear();
181 }
182
183 // Drawing operations
184
185 void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
186 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override {
187 if (this->is_failed())
188 return;
189 if (w <= 0 || h <= 0)
190 return;
191 if (get_pixel_mode(bitness) != BUFFERPIXEL || big_endian != IS_BIG_ENDIAN) {
192 // note that the usual logging macros are banned in header files, so use their replacement
193 esph_log_e(TAG, "Unsupported color depth or bit order");
194 return;
195 }
196 this->write_to_display_(x_start, y_start, w, h, reinterpret_cast<const BUFFERTYPE *>(ptr), x_offset, y_offset,
197 x_pad);
198 }
199
200 void dump_config() override {
201 esph_log_config(TAG,
202 "MIPI_SPI Display\n"
203 " Model: %s\n"
204 " Width: %u\n"
205 " Height: %u",
206 this->model_, WIDTH, HEIGHT);
207 if constexpr (OFFSET_WIDTH != 0)
208 esph_log_config(TAG, " Offset width: %u", OFFSET_WIDTH);
209 if constexpr (OFFSET_HEIGHT != 0)
210 esph_log_config(TAG, " Offset height: %u", OFFSET_HEIGHT);
211 esph_log_config(TAG,
212 " Swap X/Y: %s\n"
213 " Mirror X: %s\n"
214 " Mirror Y: %s\n"
215 " Invert colors: %s\n"
216 " Color order: %s\n"
217 " Display pixels: %d bits\n"
218 " Endianness: %s\n",
219 YESNO(this->madctl_ & MADCTL_MV), YESNO(this->madctl_ & (MADCTL_MX | MADCTL_XFLIP)),
220 YESNO(this->madctl_ & (MADCTL_MY | MADCTL_YFLIP)), YESNO(this->invert_colors_),
221 this->madctl_ & MADCTL_BGR ? "BGR" : "RGB", DISPLAYPIXEL * 8, IS_BIG_ENDIAN ? "Big" : "Little");
222 if (this->brightness_.has_value())
223 esph_log_config(TAG, " Brightness: %u", this->brightness_.value());
224 if (this->cs_ != nullptr)
225 esph_log_config(TAG, " CS Pin: %s", this->cs_->dump_summary().c_str());
226 if (this->reset_pin_ != nullptr)
227 esph_log_config(TAG, " Reset Pin: %s", this->reset_pin_->dump_summary().c_str());
228 if (this->dc_pin_ != nullptr)
229 esph_log_config(TAG, " DC Pin: %s", this->dc_pin_->dump_summary().c_str());
230 esph_log_config(TAG,
231 " SPI Mode: %d\n"
232 " SPI Data rate: %dMHz\n"
233 " SPI Bus width: %d",
234 this->mode_, static_cast<unsigned>(this->data_rate_ / 1000000), BUS_TYPE);
235 }
236
237 protected:
238 /* METHODS */
239 // convenience functions to write commands with or without data
240 void write_command_(uint8_t cmd, uint8_t data) { this->write_command_(cmd, &data, 1); }
241 void write_command_(uint8_t cmd) { this->write_command_(cmd, &cmd, 0); }
242
243 // Writes a command to the display, with the given bytes.
244 void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len) {
245 esph_log_v(TAG, "Command %02X, length %d, bytes %s", cmd, len, format_hex_pretty(bytes, len).c_str());
246 if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
247 this->enable();
248 this->write_cmd_addr_data(8, 0x02, 24, cmd << 8, bytes, len);
249 this->disable();
250 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
251 this->dc_pin_->digital_write(false);
252 this->enable();
253 this->write_cmd_addr_data(0, 0, 0, 0, &cmd, 1, 8);
254 this->disable();
255 this->dc_pin_->digital_write(true);
256 if (len != 0) {
257 this->enable();
258 this->write_cmd_addr_data(0, 0, 0, 0, bytes, len, 8);
259 this->disable();
260 }
261 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE) {
262 this->dc_pin_->digital_write(false);
263 this->enable();
264 this->write_byte(cmd);
265 this->disable();
266 this->dc_pin_->digital_write(true);
267 if (len != 0) {
268 this->enable();
269 this->write_array(bytes, len);
270 this->disable();
271 }
272 } else if constexpr (BUS_TYPE == BUS_TYPE_SINGLE_16) {
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 for (size_t i = 0; i != len; i++) {
279 this->enable();
280 this->write_byte(0);
281 this->write_byte(bytes[i]);
282 this->disable();
283 }
284 }
285 }
286
287 // write changed parameters to the display
289 if (!this->is_ready())
290 return;
291 this->write_command_(this->invert_colors_ ? INVERT_ON : INVERT_OFF);
292 if (this->brightness_.has_value())
293 this->write_command_(BRIGHTNESS, this->brightness_.value());
294 }
295
296 // set the address window for the next data write
297 void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
298 esph_log_v(TAG, "Set addr %d/%d, %d/%d", x1, y1, x2, y2);
299 uint8_t buf[4];
300 x1 += OFFSET_WIDTH;
301 x2 += OFFSET_WIDTH;
302 y1 += OFFSET_HEIGHT;
303 y2 += OFFSET_HEIGHT;
304 put16_be(buf, y1);
305 put16_be(buf + 2, y2);
306 this->write_command_(RASET, buf, sizeof buf);
307 put16_be(buf, x1);
308 put16_be(buf + 2, x2);
309 this->write_command_(CASET, buf, sizeof buf);
310 if constexpr (BUS_TYPE != BUS_TYPE_QUAD) {
311 this->write_command_(WDATA);
312 }
313 }
314
315 // map the display color bitness to the pixel mode
317 switch (bitness) {
319 return PIXEL_MODE_18; // 18 bits per pixel
321 return PIXEL_MODE_16; // 16 bits per pixel
322 default:
323 return PIXEL_MODE_8; // Default to 8 bits per pixel
324 }
325 }
326
333 void write_display_data_(const uint8_t *ptr, size_t w, size_t h, size_t pad) {
334 if (pad == 0) {
335 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
336 this->write_array(ptr, w * h);
337 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
338 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w * h, 4);
339 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
340 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w * h, 8);
341 }
342 } else {
343 for (size_t y = 0; y != h; y++) {
344 if constexpr (BUS_TYPE == BUS_TYPE_SINGLE || BUS_TYPE == BUS_TYPE_SINGLE_16) {
345 this->write_array(ptr, w);
346 } else if constexpr (BUS_TYPE == BUS_TYPE_QUAD) {
347 this->write_cmd_addr_data(8, 0x32, 24, WDATA << 8, ptr, w, 4);
348 } else if constexpr (BUS_TYPE == BUS_TYPE_OCTAL) {
349 this->write_cmd_addr_data(0, 0, 0, 0, ptr, w, 8);
350 }
351 ptr += w + pad;
352 }
353 }
354 }
355
362 void write_to_display_(int x_start, int y_start, int w, int h, const BUFFERTYPE *ptr, int x_offset, int y_offset,
363 int x_pad) {
364 this->set_addr_window_(x_start, y_start, x_start + w - 1, y_start + h - 1);
365 this->enable();
366 ptr += y_offset * (x_offset + w + x_pad) + x_offset;
367 if constexpr (BUFFERPIXEL == DISPLAYPIXEL) {
368 this->write_display_data_(reinterpret_cast<const uint8_t *>(ptr), w * sizeof(BUFFERTYPE), h,
369 x_pad * sizeof(BUFFERTYPE));
370 } else {
371 // type conversion required, do it in chunks
372 uint8_t dbuffer[DISPLAYPIXEL * 48];
373 uint8_t *dptr = dbuffer;
374 auto stride = x_offset + w + x_pad; // stride in pixels
375 for (size_t y = 0; y != h; y++) {
376 for (size_t x = 0; x != w; x++) {
377 auto color_val = ptr[y * stride + x];
378 if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_16) {
379 // 16 to 18 bit conversion
380 if constexpr (IS_BIG_ENDIAN) {
381 *dptr++ = color_val & 0xF8;
382 *dptr++ = ((color_val & 0x7) << 5) | (color_val & 0xE000) >> 11;
383 *dptr++ = (color_val >> 5) & 0xF8;
384 } else {
385 *dptr++ = (color_val >> 8) & 0xF8; // Blue
386 *dptr++ = (color_val & 0x7E0) >> 3;
387 *dptr++ = color_val << 3;
388 }
389 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_18 && BUFFERPIXEL == PIXEL_MODE_8) {
390 // 8 bit to 18 bit conversion
391 *dptr++ = color_val << 6; // Blue
392 *dptr++ = (color_val & 0x1C) << 3; // Green
393 *dptr++ = (color_val & 0xE0); // Red
394 } else if constexpr (DISPLAYPIXEL == PIXEL_MODE_16 && BUFFERPIXEL == PIXEL_MODE_8) {
395 if constexpr (IS_BIG_ENDIAN) {
396 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
397 *dptr++ = (color_val & 3) << 3;
398 } else {
399 *dptr++ = (color_val & 3) << 3;
400 *dptr++ = (color_val & 0xE0) | ((color_val & 0x1C) >> 2);
401 }
402 }
403 // buffer full? Flush.
404 if (dptr == dbuffer + sizeof(dbuffer)) {
405 this->write_display_data_(dbuffer, sizeof(dbuffer), 1, 0);
406 dptr = dbuffer;
407 }
408 }
409 }
410 // flush any remaining data
411 if (dptr != dbuffer) {
412 this->write_display_data_(dbuffer, dptr - dbuffer, 1, 0);
413 }
414 }
415 this->disable();
416 }
417
418 /* PROPERTIES */
419
420 // GPIO pins
422 std::vector<GPIOPin *> enable_pins_{};
423 GPIOPin *dc_pin_{nullptr};
424
425 // other properties set by configuration
427 unsigned draw_rounding_{2};
429 const char *model_{"Unknown"};
430 std::vector<uint8_t> init_sequence_{};
431 uint8_t madctl_{};
432};
433
448template<typename BUFFERTYPE, PixelMode BUFFERPIXEL, bool IS_BIG_ENDIAN, PixelMode DISPLAYPIXEL, BusType BUS_TYPE,
449 int WIDTH, int HEIGHT, int OFFSET_WIDTH, int OFFSET_HEIGHT, display::DisplayRotation ROTATION, int FRACTION>
450class MipiSpiBuffer : public MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT,
451 OFFSET_WIDTH, OFFSET_HEIGHT> {
452 public:
453 MipiSpiBuffer() { this->rotation_ = ROTATION; }
454
455 void dump_config() override {
456 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH,
457 OFFSET_HEIGHT>::dump_config();
458 esph_log_config(TAG,
459 " Rotation: %d°\n"
460 " Buffer pixels: %d bits\n"
461 " Buffer fraction: 1/%d\n"
462 " Buffer bytes: %zu\n"
463 " Draw rounding: %u",
464 this->rotation_, BUFFERPIXEL * 8, FRACTION, sizeof(BUFFERTYPE) * WIDTH * HEIGHT / FRACTION,
465 this->draw_rounding_);
466 }
467
468 void setup() override {
469 MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH,
470 OFFSET_HEIGHT>::setup();
471 RAMAllocator<BUFFERTYPE> allocator{};
472 this->buffer_ = allocator.allocate(WIDTH * HEIGHT / FRACTION);
473 if (this->buffer_ == nullptr) {
474 this->mark_failed("Buffer allocation failed");
475 }
476 }
477
478 void update() override {
479#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
480 auto now = millis();
481#endif
482 if (this->is_failed()) {
483 return;
484 }
485 // for updates with a small buffer, we repeatedly call the writer_ function, clipping the height to a fraction of
486 // the display height,
487 for (this->start_line_ = 0; this->start_line_ < HEIGHT; this->start_line_ += HEIGHT / FRACTION) {
488#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
489 auto lap = millis();
490#endif
491 this->end_line_ = this->start_line_ + HEIGHT / FRACTION;
492 if (this->auto_clear_enabled_) {
493 this->clear();
494 }
495 if (this->page_ != nullptr) {
496 this->page_->get_writer()(*this);
497 } else if (this->writer_.has_value()) {
498 (*this->writer_)(*this);
499 } else {
500 this->test_card();
501 }
502#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
503 esph_log_v(TAG, "Drawing from line %d took %dms", this->start_line_, millis() - lap);
504 lap = millis();
505#endif
506 if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
507 return;
508 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_,
509 this->y_high_);
510 // Some chips require that the drawing window be aligned on certain boundaries
511 auto dr = this->draw_rounding_;
512 this->x_low_ = this->x_low_ / dr * dr;
513 this->y_low_ = this->y_low_ / dr * dr;
514 this->x_high_ = (this->x_high_ + dr) / dr * dr - 1;
515 this->y_high_ = (this->y_high_ + dr) / dr * dr - 1;
516 int w = this->x_high_ - this->x_low_ + 1;
517 int h = this->y_high_ - this->y_low_ + 1;
518 this->write_to_display_(this->x_low_, this->y_low_, w, h, this->buffer_, this->x_low_,
519 this->y_low_ - this->start_line_, WIDTH - w);
520 // invalidate watermarks
521 this->x_low_ = WIDTH;
522 this->y_low_ = HEIGHT;
523 this->x_high_ = 0;
524 this->y_high_ = 0;
525#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
526 esph_log_v(TAG, "Write to display took %dms", millis() - lap);
527 lap = millis();
528#endif
529 }
530#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
531 esph_log_v(TAG, "Total update took %dms", millis() - now);
532#endif
533 }
534
535 // Draw a pixel at the given coordinates.
536 void draw_pixel_at(int x, int y, Color color) override {
537 if (!this->get_clipping().inside(x, y))
538 return;
540 if (x < 0 || x >= WIDTH || y < this->start_line_ || y >= this->end_line_)
541 return;
542 this->buffer_[(y - this->start_line_) * WIDTH + x] = convert_color_(color);
543 if (x < this->x_low_) {
544 this->x_low_ = x;
545 }
546 if (x > this->x_high_) {
547 this->x_high_ = x;
548 }
549 if (y < this->y_low_) {
550 this->y_low_ = y;
551 }
552 if (y > this->y_high_) {
553 this->y_high_ = y;
554 }
555 }
556
557 // Fills the display with a color.
558 void fill(Color color) override {
559 this->x_low_ = 0;
560 this->y_low_ = this->start_line_;
561 this->x_high_ = WIDTH - 1;
562 this->y_high_ = this->end_line_ - 1;
563 std::fill_n(this->buffer_, HEIGHT * WIDTH / FRACTION, convert_color_(color));
564 }
565
566 int get_width() override {
568 return HEIGHT;
569 return WIDTH;
570 }
571
572 int get_height() override {
574 return WIDTH;
575 return HEIGHT;
576 }
577
578 protected:
579 // Rotate the coordinates to match the display orientation.
580 void rotate_coordinates_(int &x, int &y) const {
581 if constexpr (ROTATION == display::DISPLAY_ROTATION_180_DEGREES) {
582 x = WIDTH - x - 1;
583 y = HEIGHT - y - 1;
584 } else if constexpr (ROTATION == display::DISPLAY_ROTATION_90_DEGREES) {
585 auto tmp = x;
586 x = WIDTH - y - 1;
587 y = tmp;
588 } else if constexpr (ROTATION == display::DISPLAY_ROTATION_270_DEGREES) {
589 auto tmp = y;
590 y = HEIGHT - x - 1;
591 x = tmp;
592 }
593 }
594
595 // Convert a color to the buffer pixel format.
596 BUFFERTYPE convert_color_(Color &color) const {
597 if constexpr (BUFFERPIXEL == PIXEL_MODE_8) {
598 return (color.red & 0xE0) | (color.g & 0xE0) >> 3 | color.b >> 6;
599 } else if constexpr (BUFFERPIXEL == PIXEL_MODE_16) {
600 if constexpr (IS_BIG_ENDIAN) {
601 return (color.r & 0xF8) | color.g >> 5 | (color.g & 0x1C) << 11 | (color.b & 0xF8) << 5;
602 } else {
603 return (color.r & 0xF8) << 8 | (color.g & 0xFC) << 3 | color.b >> 3;
604 }
605 }
606 return static_cast<BUFFERTYPE>(0);
607 }
608
609 BUFFERTYPE *buffer_{};
610 uint16_t x_low_{WIDTH};
611 uint16_t y_low_{HEIGHT};
612 uint16_t x_high_{0};
613 uint16_t y_high_{0};
614 uint16_t start_line_{0};
615 uint16_t end_line_{1};
616};
617
618} // namespace mipi_spi
619} // namespace esphome
uint8_t h
Definition bl0906.h:2
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool is_ready() const
virtual void setup()=0
virtual std::string dump_summary() const =0
virtual void digital_write(bool value)=0
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:818
T * allocate(size_t n)
Definition helpers.h:838
void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:17
DisplayPage * page_
Definition display.h:682
optional< display_writer_t > writer_
Definition display.h:681
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:715
DisplayRotation rotation_
Definition display.h:680
const display_writer_t & get_writer() const
Definition display.cpp:836
Class for MIPI SPI displays with a buffer.
Definition mipi_spi.h:451
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:536
void fill(Color color) override
Definition mipi_spi.h:558
BUFFERTYPE convert_color_(Color &color) const
Definition mipi_spi.h:596
void rotate_coordinates_(int &x, int &y) const
Definition mipi_spi.h:580
Base class for MIPI SPI displays.
Definition mipi_spi.h:80
std::vector< GPIOPin * > enable_pins_
Definition mipi_spi.h:422
void update() override
Definition mipi_spi.h:83
void set_draw_rounding(unsigned rounding)
Definition mipi_spi.h:102
void write_command_(uint8_t cmd, uint8_t data)
Definition mipi_spi.h:240
int get_height_internal() override
Definition mipi_spi.h:100
static PixelMode get_pixel_mode(display::ColorBitness bitness)
Definition mipi_spi.h:316
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition mipi_spi.h:297
int get_width_internal() override
Definition mipi_spi.h:99
void set_init_sequence(const std::vector< uint8_t > &sequence)
Definition mipi_spi.h:101
void set_reset_pin(GPIOPin *reset_pin)
Definition mipi_spi.h:86
void write_command_(uint8_t cmd)
Definition mipi_spi.h:241
void set_dc_pin(GPIOPin *dc_pin)
Definition mipi_spi.h:88
void set_invert_colors(bool invert_colors)
Definition mipi_spi.h:89
void setup() override
Definition mipi_spi.h:105
void set_brightness(uint8_t brightness)
Definition mipi_spi.h:93
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
Definition mipi_spi.h:244
void set_model(const char *model)
Definition mipi_spi.h:85
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:333
void dump_config() override
Definition mipi_spi.h:200
display::DisplayType get_display_type() override
Definition mipi_spi.h:97
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:185
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_spi.h:84
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition mipi_spi.h:87
optional< uint8_t > brightness_
Definition mipi_spi.h:428
std::vector< uint8_t > init_sequence_
Definition mipi_spi.h:430
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:362
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
uint32_t data_rate_
Definition spi.h:410
The SPIDevice is what components using the SPI will create.
Definition spi.h:427
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:471
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Definition helpers.cpp:280
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
uint8_t red
Definition color.h:22
uint8_t g
Definition color.h:25
uint8_t b
Definition color.h:29
uint8_t r
Definition color.h:21
uint8_t pad
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6