ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mipi_rgb.cpp
Go to the documentation of this file.
1#if defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S31)
2#include "mipi_rgb.h"
3#include "esphome/core/gpio.h"
4#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7
8#include <algorithm>
9#include <cstring>
10#include <driver/gpio.h>
11#include <esp_lcd_panel_ops.h>
12#include <span>
13
15
16static const uint8_t DELAY_FLAG = 0xFF;
17
18// Maximum bytes to log for init commands (truncated if larger)
19static constexpr size_t MIPI_RGB_MAX_CMD_LOG_BYTES = 64;
20static constexpr uint8_t MADCTL_MY = 0x80; // Bit 7 Bottom to top
21static constexpr uint8_t MADCTL_MX = 0x40; // Bit 6 Right to left
22static constexpr uint8_t MADCTL_MV = 0x20; // Bit 5 Swap axes
23static constexpr uint8_t MADCTL_ML = 0x10; // Bit 4 Refresh bottom to top
24static constexpr uint8_t MADCTL_BGR = 0x08; // Bit 3 Blue-Green-Red pixel order
25static constexpr uint8_t MADCTL_XFLIP = 0x02; // Mirror the display horizontally
26static constexpr uint8_t MADCTL_YFLIP = 0x01; // Mirror the display vertically
27
29 if (!this->enable_pins_.empty()) {
30 for (auto *pin : this->enable_pins_) {
31 pin->setup();
32 pin->digital_write(true);
33 }
34 delay(10);
35 }
36 pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
37}
38
39#ifdef USE_SPI
41 this->setup_enables_();
42 this->spi_setup();
44 if (this->is_failed())
45 return;
46 this->common_setup_();
47}
49 this->enable();
50 if (this->dc_pin_ == nullptr) {
51 this->write(value, 9);
52 } else {
53 this->dc_pin_->digital_write(false);
54 this->write_byte(value);
55 this->dc_pin_->digital_write(true);
56 }
57 this->disable();
58}
59
61 this->enable();
62 if (this->dc_pin_ == nullptr) {
63 this->write(value | 0x100, 9);
64 } else {
65 this->dc_pin_->digital_write(true);
66 this->write_byte(value);
67 }
68 this->disable();
69}
70
76 size_t index = 0;
77 const uint8_t *seq = this->init_sequence_;
78 const size_t len = this->init_sequence_len_;
79 while (index != len) {
80 if (len - index < 2) {
81 this->mark_failed(LOG_STR("Malformed init sequence"));
82 return;
83 }
84 uint8_t cmd = seq[index++];
85 uint8_t x = seq[index++];
86 if (x == DELAY_FLAG) {
87 ESP_LOGD(TAG, "Delay %dms", cmd);
88 delay(cmd);
89 } else {
90 uint8_t num_args = x & 0x7F;
91 if (len - index < num_args) {
92 this->mark_failed(LOG_STR("Malformed init sequence"));
93 return;
94 }
95 if (cmd == SLEEP_OUT) {
96 delay(120); // NOLINT
97 }
98 const auto *ptr = seq + index;
99 char hex_buf[format_hex_pretty_size(MIPI_RGB_MAX_CMD_LOG_BYTES)];
100 ESP_LOGD(TAG, "Write command %02X, length %d, byte(s) %s", cmd, num_args,
101 format_hex_pretty_to(hex_buf, ptr, num_args, '.'));
102 index += num_args;
103 this->write_command_(cmd);
104 while (num_args-- != 0)
105 this->write_data_(*ptr++);
106 if (cmd == SLEEP_OUT)
107 delay(10);
108 }
109 }
110 this->spi_teardown();
111 delay(10);
112}
113
116 LOG_PIN(" CS Pin: ", this->cs_);
117 LOG_PIN(" DC Pin: ", this->dc_pin_);
118 ESP_LOGCONFIG(TAG, " SPI Data rate: %uMHz", (unsigned) (this->data_rate_ / 1000000));
119}
120
121#endif // USE_SPI
122
124 this->setup_enables_();
125 this->common_setup_();
126}
127
129 esp_lcd_rgb_panel_config_t config{};
130 config.flags.fb_in_psram = 1;
131 config.bounce_buffer_size_px = this->width_ * 10;
132 config.num_fbs = 1;
133 config.timings.h_res = this->width_;
134 config.timings.v_res = this->height_;
135 config.timings.hsync_pulse_width = this->hsync_pulse_width_;
136 config.timings.hsync_back_porch = this->hsync_back_porch_;
137 config.timings.hsync_front_porch = this->hsync_front_porch_;
138 config.timings.vsync_pulse_width = this->vsync_pulse_width_;
139 config.timings.vsync_back_porch = this->vsync_back_porch_;
140 config.timings.vsync_front_porch = this->vsync_front_porch_;
141 config.timings.flags.pclk_active_neg = this->pclk_inverted_;
142 config.timings.pclk_hz = this->pclk_frequency_;
143 config.clk_src = LCD_CLK_SRC_PLL160M;
144 size_t data_pin_count = sizeof(this->data_pins_) / sizeof(this->data_pins_[0]);
145 for (size_t i = 0; i != data_pin_count; i++) {
146 config.data_gpio_nums[i] = static_cast<gpio_num_t>(this->data_pins_[i]->get_pin());
147 }
148 config.data_width = data_pin_count;
149 config.disp_gpio_num = GPIO_NUM_NC;
150 if (this->hsync_pin_) {
151 config.hsync_gpio_num = static_cast<gpio_num_t>(this->hsync_pin_->get_pin());
152 } else {
153 config.hsync_gpio_num = GPIO_NUM_NC;
154 }
155 if (this->vsync_pin_) {
156 config.vsync_gpio_num = static_cast<gpio_num_t>(this->vsync_pin_->get_pin());
157 } else {
158 config.vsync_gpio_num = GPIO_NUM_NC;
159 }
160 if (this->de_pin_) {
161 config.de_gpio_num = static_cast<gpio_num_t>(this->de_pin_->get_pin());
162 } else {
163 config.de_gpio_num = GPIO_NUM_NC;
164 }
165 config.pclk_gpio_num = static_cast<gpio_num_t>(this->pclk_pin_->get_pin());
166 esp_err_t err = esp_lcd_new_rgb_panel(&config, &this->handle_);
167 if (err == ESP_OK)
168 err = esp_lcd_panel_reset(this->handle_);
169 if (err == ESP_OK)
170 err = esp_lcd_panel_init(this->handle_);
171 if (err == ESP_OK) {
172 void *fb = nullptr;
173 err = esp_lcd_rgb_panel_get_frame_buffer(this->handle_, 1, &fb);
174 if (err == ESP_OK && fb != nullptr) {
175 this->fb_ = reinterpret_cast<uint16_t *>(fb);
176 } else if (err == ESP_OK) {
177 err = ESP_ERR_NO_MEM;
178 }
179 }
180 if (err != ESP_OK) {
181 ESP_LOGE(TAG, "lcd setup failed: %s", esp_err_to_name(err));
182 this->mark_failed(LOG_STR("lcd setup failed"));
183 }
184 ESP_LOGCONFIG(TAG, "MipiRgb setup complete");
185}
186
188 if (this->is_failed())
189 return;
190 if (this->auto_clear_enabled_) {
191 this->clear();
192 }
193 this->updating_ = true;
194 if (this->show_test_card_) {
195 this->test_card();
196 } else if (this->page_ != nullptr) {
197 this->page_->get_writer()(*this);
198 } else if (this->writer_.has_value()) {
199 (*this->writer_)(*this);
200 } else {
201 this->stop_poller();
202 }
203 this->updating_ = false;
204 this->flush_dirty_();
205}
206
208 // Inside update() the one flush at its end sends everything
209 if (this->updating_ || this->buffer_ == nullptr || this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_)
210 return;
211 ESP_LOGV(TAG, "x_low %d, y_low %d, x_high %d, y_high %d", this->x_low_, this->y_low_, this->x_high_, this->y_high_);
212 int w = this->x_high_ - this->x_low_ + 1;
213 int h = this->y_high_ - this->y_low_ + 1;
214 this->write_to_display_(this->x_low_, this->y_low_, w, h, reinterpret_cast<const uint8_t *>(this->buffer_),
215 this->x_low_, this->y_low_, this->width_ - w - this->x_low_);
216 // invalidate watermarks
217 this->x_low_ = this->width_;
218 this->y_low_ = this->height_;
219 this->x_high_ = 0;
220 this->y_high_ = 0;
221}
222
223void MipiRgb::draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr,
224 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset,
225 int x_pad) {
226 if (w <= 0 || h <= 0 || this->is_failed())
227 return;
229 Display::draw_pixels_internal(x_start, y_start, w, h, ptr, bitness, big_endian, x_offset, y_offset, x_pad);
230 this->mark_dirty_(x_start, y_start, w, h);
231 this->flush_dirty_();
232 return;
233 }
234
235 const int log_w = this->get_width();
236 const int log_h = this->get_height();
237
238 if (x_start < 0 || y_start < 0 || x_start + w > log_w || y_start + h > log_h) {
239 return;
240 }
241
242 uint16_t *dst = this->buffer_ != nullptr ? this->buffer_ : this->fb_;
243 const size_t stride = static_cast<size_t>(x_offset) + w + x_pad;
244 const auto *src_origin =
245 reinterpret_cast<const uint16_t *>(ptr) + (static_cast<size_t>(y_offset) * stride + x_offset);
246
247 blit_rotated_565(dst, this->width_, this->height_, this->rotation_, src_origin, x_start, y_start, w, h, stride);
248 if (dst == this->buffer_) {
249 this->mark_dirty_(x_start, y_start, w, h);
250 if (!this->updating_) {
251 this->flush_dirty_();
252 }
253 }
254}
255
256void MipiRgb::write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
257 int x_pad) {
258 esp_err_t err = ESP_OK;
259 auto stride = (x_offset + w + x_pad) * 2;
260 ptr += y_offset * stride + x_offset * 2; // skip to the first pixel
261 // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
262 if (x_offset == 0 && x_pad == 0) {
263 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
264 } else {
265 // draw line by line
266 for (int y = 0; y != h; y++) {
267 err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1, ptr);
268 if (err != ESP_OK)
269 break;
270 ptr += stride; // next line
271 }
272 }
273 if (err != ESP_OK) {
274 ESP_LOGE(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
275 }
276}
277
279 if (this->is_failed())
280 return false;
281 if (this->buffer_ != nullptr)
282 return true;
283 // this is dependent on the enum values.
284 RAMAllocator<uint16_t> allocator;
285 this->buffer_ = allocator.allocate(this->height_ * this->width_);
286 if (this->buffer_ == nullptr) {
287 this->mark_failed(LOG_STR("Could not allocate buffer for display!"));
288 return false;
289 }
290 return true;
291}
292
293// Widen the dirty box to the drawn rectangle, even where the buffer already held the same values.
294void MipiRgb::mark_dirty_(int x_start, int y_start, int w, int h) {
295 int x1 = x_start, y1 = y_start;
296 int x2 = x_start + w - 1, y2 = y_start + h - 1;
297 this->rotate_(x1, y1);
298 this->rotate_(x2, y2);
299 const int right = static_cast<int>(this->width_) - 1;
300 const int bottom = static_cast<int>(this->height_) - 1;
301 const int left = std::clamp(std::min(x1, x2), 0, right);
302 const int top = std::clamp(std::min(y1, y2), 0, bottom);
303 x2 = std::clamp(std::max(x1, x2), 0, right);
304 y2 = std::clamp(std::max(y1, y2), 0, bottom);
305 // An empty box is low above high, so it is replaced rather than widened
306 if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_) {
307 this->x_low_ = left;
308 this->x_high_ = x2;
309 this->y_low_ = top;
310 this->y_high_ = y2;
311 return;
312 }
313 this->x_low_ = std::min<int>(this->x_low_, left);
314 this->x_high_ = std::max<int>(this->x_high_, x2);
315 this->y_low_ = std::min<int>(this->y_low_, top);
316 this->y_high_ = std::max<int>(this->y_high_, y2);
317}
318
319void MipiRgb::draw_pixel_at(int x, int y, Color color) {
320 if (this->is_point_clipped(x, y) || this->is_failed())
321 return;
322
323 this->rotate_(x, y);
324 if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0) {
325 return;
326 }
327 if (!this->check_buffer_())
328 return;
329 size_t pos = (y * this->width_) + x;
330 uint16_t new_color = this->to_panel_565_(color);
331 if (this->buffer_[pos] == new_color)
332 return;
333 this->buffer_[pos] = new_color;
334 // low and high watermark may speed up drawing from buffer
336 this->x_low_ = x;
338 this->y_low_ = y;
339 if (x > this->x_high_)
340 this->x_high_ = x;
341 if (y > this->y_high_)
342 this->y_high_ = y;
343}
344void MipiRgb::fill(Color color) {
345 if (!this->check_buffer_())
346 return;
347
348 // If clipping is active, fall back to base implementation
349 if (this->get_clipping().is_set()) {
350 Display::fill(color);
351 return;
352 }
353
354 auto *ptr_16 = reinterpret_cast<uint16_t *>(this->buffer_);
355 uint16_t new_color = this->to_panel_565_(color);
356 std::fill_n(ptr_16, this->width_ * this->height_, new_color);
357 this->x_low_ = 0;
358 this->y_low_ = 0;
359 this->x_high_ = this->width_ - 1;
360 this->y_high_ = this->height_ - 1;
361}
362
374
386
387[[maybe_unused]] static const char *get_pin_name(GPIOPin *pin, std::span<char, GPIO_SUMMARY_MAX_LEN> buffer) {
388 if (pin == nullptr)
389 return "None";
390 pin->dump_summary(buffer.data(), buffer.size());
391 return buffer.data();
392}
393
394void MipiRgb::dump_pins_(uint8_t start, uint8_t end, const char *name, uint8_t offset) {
395 char pin_summary[GPIO_SUMMARY_MAX_LEN];
396 for (uint8_t i = start; i != end; i++) {
397 this->data_pins_[i]->dump_summary(pin_summary, sizeof(pin_summary));
398 ESP_LOGCONFIG(TAG, " %s pin %d: %s", name, offset++, pin_summary);
399 }
400}
401
403 char reset_buf[GPIO_SUMMARY_MAX_LEN];
404 char de_buf[GPIO_SUMMARY_MAX_LEN];
405 char pclk_buf[GPIO_SUMMARY_MAX_LEN];
406 char hsync_buf[GPIO_SUMMARY_MAX_LEN];
407 char vsync_buf[GPIO_SUMMARY_MAX_LEN];
408 ESP_LOGCONFIG(TAG,
409 "MIPI_RGB LCD"
410 "\n Model: %s"
411 "\n Width: %u"
412 "\n Height: %u"
413 "\n Rotation: %d degrees"
414 "\n PCLK Inverted: %s"
415 "\n HSync Pulse Width: %u"
416 "\n HSync Back Porch: %u"
417 "\n HSync Front Porch: %u"
418 "\n VSync Pulse Width: %u"
419 "\n VSync Back Porch: %u"
420 "\n VSync Front Porch: %u"
421 "\n Invert Colors: %s"
422 "\n Pixel Clock: %uMHz"
423 "\n Reset Pin: %s"
424 "\n DE Pin: %s"
425 "\n PCLK Pin: %s"
426 "\n HSYNC Pin: %s"
427 "\n VSYNC Pin: %s",
428 this->model_, this->width_, this->height_, this->rotation_, YESNO(this->pclk_inverted_),
430 this->vsync_back_porch_, this->vsync_front_porch_, YESNO(this->invert_colors_),
431 (unsigned) (this->pclk_frequency_ / 1000000), get_pin_name(this->reset_pin_, reset_buf),
432 get_pin_name(this->de_pin_, de_buf), get_pin_name(this->pclk_pin_, pclk_buf),
433 get_pin_name(this->hsync_pin_, hsync_buf), get_pin_name(this->vsync_pin_, vsync_buf));
434
435 this->dump_pins_(8, 13, "Blue", 0);
436 this->dump_pins_(13, 16, "Green", 0);
437 this->dump_pins_(0, 3, "Green", 3);
438 this->dump_pins_(3, 8, "Red", 0);
439}
440
441} // namespace esphome::mipi_rgb
442#endif // defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32P4) ||
443 // defined(USE_ESP32_VARIANT_ESP32S31)
uint8_t h
Definition bl0906.h:2
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
virtual void digital_write(bool value)=0
virtual size_t dump_summary(char *buffer, size_t len) const
Write a summary of this pin to the provided buffer.
Definition gpio_pin.h:126
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
T * allocate(size_t n)
Definition helpers.h:2423
display_writer_t writer_
Definition display.h:864
int int int const uint8_t ColorOrder ColorBitness bitness
Definition display.h:392
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Definition display.cpp:17
bool is_clipping() const
Definition display.h:805
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
Definition display.h:392
int int int const uint8_t ColorOrder ColorBitness bool int int int x_pad
Definition display.h:392
int int int const uint8_t * ptr
Definition display.h:391
bool ESPHOME_ALWAYS_INLINE is_point_clipped(int x, int y) const
Whether (x, y) falls outside the active clipping rectangle.
Definition display.h:810
DisplayPage * page_
Definition display.h:865
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Definition display.h:392
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:826
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
Definition display.h:392
DisplayRotation rotation_
Definition display.h:863
const display_writer_t & get_writer() const
Definition display.cpp:956
size_t dump_summary(char *buffer, size_t len) const override
Definition gpio.cpp:146
InternalGPIOPin * de_pin_
Definition mipi_rgb.h:92
std::vector< GPIOPin * > enable_pins_
Definition mipi_rgb.h:114
void flush_dirty_()
Send the region touched since the last flush to the panel.
Definition mipi_rgb.cpp:207
esp_lcd_panel_handle_t handle_
Definition mipi_rgb.h:120
int get_height() override
Definition mipi_rgb.cpp:375
InternalGPIOPin * hsync_pin_
Definition mipi_rgb.h:94
void dump_config() override
Definition mipi_rgb.cpp:402
int get_height_internal() override
Definition mipi_rgb.h:67
InternalGPIOPin * data_pins_[16]
Definition mipi_rgb.h:97
void draw_pixel_at(int x, int y, Color color) override
Definition mipi_rgb.cpp:319
void rotate_(int &x, int &y) const
Definition mipi_rgb.h:87
uint16_t to_panel_565_(Color color) const
Pack a colour as the 565 value the panel's byte order expects in memory.
Definition mipi_rgb.h:80
InternalGPIOPin * vsync_pin_
Definition mipi_rgb.h:95
void dump_pins_(uint8_t start, uint8_t end, const char *name, uint8_t offset)
Definition mipi_rgb.cpp:394
void mark_dirty_(int x_start, int y_start, int w, int h)
Definition mipi_rgb.cpp:294
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
Definition mipi_rgb.cpp:223
InternalGPIOPin * pclk_pin_
Definition mipi_rgb.h:93
void fill(Color color) override
Definition mipi_rgb.cpp:344
int get_width_internal() override
Definition mipi_rgb.h:66
void write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset, int x_pad)
Definition mipi_rgb.cpp:256
void write_data_(uint8_t value)
Definition mipi_rgb.cpp:60
void write_init_sequence_()
this relies upon the init sequence being well-formed, which is guaranteed by the Python init code.
Definition mipi_rgb.cpp:75
void write_command_(uint8_t value)
Definition mipi_rgb.cpp:48
const uint8_t * init_sequence_
Definition mipi_rgb.h:146
uint32_t data_rate_
Definition spi.h:436
uint32_t buffer
Definition gif.cpp:50
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:136
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:139
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:138
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:137
const uint8_t SLEEP_OUT
Definition mipi_rgb.h:19
void blit_rotated_565(uint16_t *fb, int fb_width, int fb_height, display::DisplayRotation rotation, const uint16_t *src_origin, int x_start, int y_start, int w, int h, size_t stride)
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
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:498
size_t size_t pos
Definition helpers.h:1333
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:1687
constexpr size_t GPIO_SUMMARY_MAX_LEN
Maximum buffer size for dump_summary output.
Definition gpio_pin.h:14
void HOT delay(uint32_t ms)
Definition hal.cpp:85
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:163
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t seq
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6