1#ifdef USE_ESP32_VARIANT_ESP32P4
10static constexpr size_t MIPI_DSI_MAX_CMD_LOG_BYTES = 64;
12static bool notify_refresh_ready(esp_lcd_panel_handle_t panel, esp_lcd_dpi_panel_event_data_t *edata,
void *user_ctx) {
13 auto *sem =
static_cast<SemaphoreHandle_t *
>(user_ctx);
14 BaseType_t need_yield = pdFALSE;
15 xSemaphoreGiveFromISR(sem, &need_yield);
16 return (need_yield == pdTRUE);
20 ESP_LOGE(TAG,
"%s: %s", LOG_STR_ARG(
message), esp_err_to_name(err));
25 ESP_LOGCONFIG(TAG,
"Running Setup");
30 pin->digital_write(
true);
35 esp_lcd_dsi_bus_config_t bus_config = {
39 .phy_clk_src = MIPI_DSI_PHY_CLK_SRC_DEFAULT,
42 auto err = esp_lcd_new_dsi_bus(&bus_config, &this->
bus_handle_);
44 this->
smark_failed(LOG_STR(
"lcd_new_dsi_bus failed"), err);
47 esp_lcd_dbi_io_config_t dbi_config = {
54 this->
smark_failed(LOG_STR(
"new_panel_io_dbi failed"), err);
57 auto pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565;
59 pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB888;
61 esp_lcd_dpi_panel_config_t dpi_config = {.virtual_channel = 0,
62 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
64 .pixel_format = pixel_format,
82 this->
smark_failed(LOG_STR(
"esp_lcd_new_panel_dpi failed"), err);
96 auto when =
millis() + 120;
97 err = esp_lcd_panel_init(this->
handle_);
104 while (index != vec.size()) {
105 if (vec.size() - index < 2) {
106 this->
mark_failed(LOG_STR(
"Malformed init sequence"));
109 uint8_t cmd = vec[index++];
110 uint8_t
x = vec[index++];
112 ESP_LOGD(TAG,
"Delay %dms", cmd);
115 uint8_t num_args =
x & 0x7F;
116 if (vec.size() - index < num_args) {
117 this->
mark_failed(LOG_STR(
"Malformed init sequence"));
127 const auto *ptr = vec.data() + index;
128#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
131 ESP_LOGVV(TAG,
"Command %02X, length %d, byte(s) %s", cmd, num_args,
133 err = esp_lcd_panel_io_tx_param(this->
io_handle_, cmd, ptr, num_args);
135 this->
smark_failed(LOG_STR(
"lcd_panel_io_tx_param failed"), err);
143 this->
io_lock_ = xSemaphoreCreateBinary();
144 esp_lcd_dpi_panel_event_callbacks_t cbs = {
145 .on_color_trans_done = notify_refresh_ready,
148 err = (esp_lcd_dpi_panel_register_event_callbacks(this->
handle_, &cbs, this->
io_lock_));
150 this->
smark_failed(LOG_STR(
"Failed to register callbacks"), err);
154 ESP_LOGCONFIG(TAG,
"MIPI DSI setup complete");
163 }
else if (this->
page_ !=
nullptr) {
176 this->
width_ - w - this->x_low_);
178 this->x_low_ = this->
width_;
186 if (w <= 0 ||
h <= 0)
191 display::Display::draw_pixels_at(x_start, y_start, w,
h, ptr, order, bitness, big_endian, x_offset, y_offset,
199 esp_err_t err = ESP_OK;
201 auto stride = (x_offset + w + x_pad) * bytes_per_pixel;
202 ptr += y_offset * stride + x_offset * bytes_per_pixel;
204 if (x_offset == 0 && x_pad == 0) {
205 err = esp_lcd_panel_draw_bitmap(this->
handle_, x_start, y_start, x_start + w, y_start +
h, ptr);
206 xSemaphoreTake(this->
io_lock_, portMAX_DELAY);
210 for (
int y = 0;
y !=
h;
y++) {
211 err = esp_lcd_panel_draw_bitmap(this->
handle_, x_start,
y + y_start, x_start + w,
y + y_start + 1, ptr);
215 xSemaphoreTake(this->
io_lock_, portMAX_DELAY);
219 ESP_LOGE(TAG,
"lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
231 if (this->
buffer_ ==
nullptr) {
232 this->
mark_failed(LOG_STR(
"Could not allocate buffer for display!"));
264 size_t pos = (
y * this->
width_) + x;
267 auto *ptr_16 =
reinterpret_cast<uint16_t *
>(this->
buffer_);
268 uint8_t hi_byte =
static_cast<uint8_t
>(color.
r & 0xF8) | (color.
g >> 5);
269 uint8_t lo_byte =
static_cast<uint8_t
>((color.
g & 0x1C) << 3) | (color.
b >> 3);
270 uint16_t new_color = lo_byte | (hi_byte << 8);
271 if (ptr_16[pos] == new_color)
273 ptr_16[pos] = new_color;
279 this->
buffer_[pos * 3 + 1] = color.
g;
280 this->
buffer_[pos * 3 + 2] = color.
r;
283 this->
buffer_[pos * 3 + 1] = color.
g;
284 this->
buffer_[pos * 3 + 2] = color.
b;
306 Display::fill(color);
312 auto *ptr_16 =
reinterpret_cast<uint16_t *
>(this->
buffer_);
313 uint8_t hi_byte =
static_cast<uint8_t
>(color.
r & 0xF8) | (color.
g >> 5);
314 uint8_t lo_byte =
static_cast<uint8_t
>((color.
g & 0x1C) << 3) | (color.
b >> 3);
315 uint16_t new_color = lo_byte | (hi_byte << 8);
316 std::fill_n(ptr_16, this->
width_ * this->
height_, new_color);
364static const uint8_t PIXEL_MODES[] = {0, 16, 18, 24};
375 "\n Rotation: %d degrees"
377 "\n Lane Bit Rate: %uMbps"
378 "\n HSync Pulse Width: %u"
379 "\n HSync Back Porch: %u"
380 "\n HSync Front Porch: %u"
381 "\n VSync Pulse Width: %u"
382 "\n VSync Back Porch: %u"
383 "\n VSync Front Porch: %u"
384 "\n Buffer Color Depth: %d bit"
385 "\n Display Pixel Mode: %d bit"
387 "\n Invert Colors: %s"
388 "\n Pixel Clock: %dMHz",
virtual void mark_failed()
Mark this component as failed.
virtual void digital_write(bool value)=0
An STL allocator that uses SPI or internal RAM.
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
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
uint16_t vsync_front_porch_
void fill(Color color) override
uint16_t hsync_front_porch_
uint16_t hsync_back_porch_
display::ColorOrder color_mode_
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
void dump_config() override
std::vector< GPIOPin * > enable_pins_
void smark_failed(const LogString *message, esp_err_t err)
uint16_t vsync_pulse_width_
display::ColorBitness color_depth_
uint16_t hsync_pulse_width_
SemaphoreHandle_t io_lock_
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)
esp_lcd_panel_io_handle_t io_handle_
int get_height() override
void draw_pixel_at(int x, int y, Color color) override
uint16_t vsync_back_porch_
int get_width_internal() override
esp_lcd_dsi_bus_handle_t bus_handle_
std::vector< uint8_t > init_sequence_
int get_height_internal() override
esp_lcd_panel_handle_t handle_
@ DISPLAY_ROTATION_0_DEGREES
@ DISPLAY_ROTATION_270_DEGREES
@ DISPLAY_ROTATION_180_DEGREES
@ DISPLAY_ROTATION_90_DEGREES
const uint8_t MADCTL_YFLIP
const uint8_t MADCTL_XFLIP
const uint8_t SW_RESET_CMD
Providing packet encoding functions for exchanging data with a remote host.
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
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 IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()