1#ifdef USE_ESP32_VARIANT_ESP32P4
9static constexpr size_t MIPI_DSI_MAX_CMD_LOG_BYTES = 64;
11static bool notify_refresh_ready(esp_lcd_panel_handle_t panel, esp_lcd_dpi_panel_event_data_t *edata,
void *user_ctx) {
12 SemaphoreHandle_t sem =
static_cast<SemaphoreHandle_t
>(user_ctx);
13 BaseType_t need_yield = pdFALSE;
14 xSemaphoreGiveFromISR(sem, &need_yield);
15 return (need_yield == pdTRUE);
19 ESP_LOGE(TAG,
"%s: %s", LOG_STR_ARG(
message), esp_err_to_name(err));
24 ESP_LOGCONFIG(TAG,
"Running Setup");
29 pin->digital_write(
true);
34 esp_lcd_dsi_bus_config_t bus_config = {
38 .phy_clk_src = MIPI_DSI_PHY_CLK_SRC_DEFAULT,
41 auto err = esp_lcd_new_dsi_bus(&bus_config, &this->
bus_handle_);
43 this->
smark_failed(LOG_STR(
"lcd_new_dsi_bus failed"), err);
46 esp_lcd_dbi_io_config_t dbi_config = {
53 this->
smark_failed(LOG_STR(
"new_panel_io_dbi failed"), err);
57#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
58 auto color_format = LCD_COLOR_FMT_RGB565;
60 color_format = LCD_COLOR_FMT_RGB888;
62 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
63 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
65 .in_color_format = color_format,
67 auto pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565;
69 pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB888;
71 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
72 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
74 .pixel_format = pixel_format,
89#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
96 this->
smark_failed(LOG_STR(
"esp_lcd_new_panel_dpi failed"), err);
99#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
100 err = esp_lcd_dpi_panel_enable_dma2d(this->
handle_);
102 this->
smark_failed(LOG_STR(
"esp_lcd_dpi_panel_enable_dma2d failed"), err);
117 auto when =
millis() + 120;
118 err = esp_lcd_panel_init(this->
handle_);
120 this->
smark_failed(LOG_STR(
"esp_lcd_init failed"), err);
125 while (index != vec.size()) {
126 if (vec.size() - index < 2) {
127 this->
mark_failed(LOG_STR(
"Malformed init sequence"));
130 uint8_t cmd = vec[index++];
131 uint8_t
x = vec[index++];
133 ESP_LOGD(TAG,
"Delay %dms", cmd);
136 uint8_t num_args =
x & 0x7F;
137 if (vec.size() - index < num_args) {
138 this->
mark_failed(LOG_STR(
"Malformed init sequence"));
148 const auto *ptr = vec.data() + index;
149#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
152 ESP_LOGVV(TAG,
"Command %02X, length %d, byte(s) %s", cmd, num_args,
154 err = esp_lcd_panel_io_tx_param(this->
io_handle_, cmd, ptr, num_args);
156 this->
smark_failed(LOG_STR(
"lcd_panel_io_tx_param failed"), err);
164 this->
io_lock_ = xSemaphoreCreateBinary();
165 esp_lcd_dpi_panel_event_callbacks_t cbs = {
166 .on_color_trans_done = notify_refresh_ready,
169 err = (esp_lcd_dpi_panel_register_event_callbacks(this->
handle_, &cbs, this->
io_lock_));
171 this->
smark_failed(LOG_STR(
"Failed to register callbacks"), err);
175 ESP_LOGCONFIG(TAG,
"MIPI DSI setup complete");
184 }
else if (this->
page_ !=
nullptr) {
197 this->
width_ - w - this->x_low_);
199 this->x_low_ = this->
width_;
207 if (w <= 0 ||
h <= 0)
212 display::Display::draw_pixels_at(x_start, y_start, w,
h, ptr, order, bitness, big_endian, x_offset, y_offset,
221 esp_err_t err = ESP_OK;
223 auto stride = (x_offset + w + x_pad) * bytes_per_pixel;
224 ptr += y_offset * stride + x_offset * bytes_per_pixel;
226 if (x_offset == 0 && x_pad == 0) {
227 err = esp_lcd_panel_draw_bitmap(this->
handle_, x_start, y_start, x_start + w, y_start +
h, ptr);
228 xSemaphoreTake(this->
io_lock_, portMAX_DELAY);
232 for (
int y = 0;
y !=
h;
y++) {
233 err = esp_lcd_panel_draw_bitmap(this->
handle_, x_start,
y + y_start, x_start + w,
y + y_start + 1, ptr);
237 xSemaphoreTake(this->
io_lock_, portMAX_DELAY);
241 ESP_LOGE(TAG,
"lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
251 RAMAllocator<uint8_t> allocator;
253 if (this->
buffer_ ==
nullptr) {
254 this->
mark_failed(LOG_STR(
"Could not allocate buffer for display!"));
288 auto *ptr_16 =
reinterpret_cast<uint16_t *
>(this->
buffer_);
289 uint8_t hi_byte =
static_cast<uint8_t
>(color.r & 0xF8) | (color.g >> 5);
290 uint8_t lo_byte =
static_cast<uint8_t
>((color.g & 0x1C) << 3) | (color.b >> 3);
291 uint16_t new_color = lo_byte | (hi_byte << 8);
292 if (ptr_16[
pos] == new_color)
294 ptr_16[
pos] = new_color;
299 this->
buffer_[pos * 3] = color.b;
300 this->
buffer_[pos * 3 + 1] = color.g;
301 this->
buffer_[pos * 3 + 2] = color.r;
303 this->
buffer_[pos * 3] = color.r;
304 this->
buffer_[pos * 3 + 1] = color.g;
305 this->
buffer_[pos * 3 + 2] = color.b;
327 Display::fill(color);
333 auto *ptr_16 =
reinterpret_cast<uint16_t *
>(this->
buffer_);
334 uint8_t hi_byte =
static_cast<uint8_t
>(color.r & 0xF8) | (color.g >> 5);
335 uint8_t lo_byte =
static_cast<uint8_t
>((color.g & 0x1C) << 3) | (color.b >> 3);
336 uint16_t new_color = lo_byte | (hi_byte << 8);
337 std::fill_n(ptr_16, this->
width_ * this->
height_, new_color);
344 this->
buffer_[i * 3 + 0] = color.b;
345 this->
buffer_[i * 3 + 1] = color.g;
346 this->
buffer_[i * 3 + 2] = color.r;
350 this->
buffer_[i * 3 + 0] = color.r;
351 this->
buffer_[i * 3 + 1] = color.g;
352 this->
buffer_[i * 3 + 2] = color.b;
385static const uint8_t PIXEL_MODES[] = {0, 16, 18, 24};
393 "\n Rotation: %d degrees"
395 "\n Lane Bit Rate: %.0fMbps"
396 "\n HSync Pulse Width: %u"
397 "\n HSync Back Porch: %u"
398 "\n HSync Front Porch: %u"
399 "\n VSync Pulse Width: %u"
400 "\n VSync Back Porch: %u"
401 "\n VSync Front Porch: %u"
402 "\n Buffer Color Depth: %d bit"
403 "\n Display Pixel Mode: %d bit"
404 "\n Invert Colors: %s"
405 "\n Pixel Clock: %.1fMHz",
void mark_failed()
Mark this component as failed.
virtual void digital_write(bool value)=0
virtual void clear()
Clear the entire screen by filling it with OFF pixels.
Rect get_clipping() const
Get the current the clipping rectangle.
DisplayRotation rotation_
virtual void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorOrder order, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Given an array of pixels encoded in the nominated format, draw these into the display's buffer.
const display_writer_t & get_writer() const
display::ColorOrder color_mode_
display::ColorBitness color_depth_
uint16_t hsync_front_porch_
esp_lcd_panel_handle_t handle_
std::vector< uint8_t > init_sequence_
int get_height_internal() override
uint16_t hsync_pulse_width_
esp_lcd_panel_io_handle_t io_handle_
uint16_t vsync_front_porch_
int get_width_internal() override
void draw_pixel_at(int x, int y, Color color) override
std::vector< GPIOPin * > enable_pins_
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)
SemaphoreHandle_t io_lock_
uint16_t vsync_back_porch_
void smark_failed(const LogString *message, esp_err_t err)
esp_lcd_dsi_bus_handle_t bus_handle_
void fill(Color color) override
int get_height() override
void dump_config() override
uint16_t hsync_back_porch_
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
uint16_t vsync_pulse_width_
const LogString * message
@ DISPLAY_ROTATION_0_DEGREES
@ DISPLAY_ROTATION_270_DEGREES
@ DISPLAY_ROTATION_180_DEGREES
@ DISPLAY_ROTATION_90_DEGREES
const uint8_t SW_RESET_CMD
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).
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".
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()