1#if defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32P4)
7#include <driver/gpio.h>
8#include <esp_lcd_panel_rgb.h>
14static const uint8_t DELAY_FLAG = 0xFF;
17static constexpr size_t MIPI_RGB_MAX_CMD_LOG_BYTES = 64;
18static constexpr uint8_t MADCTL_MY = 0x80;
19static constexpr uint8_t MADCTL_MX = 0x40;
20static constexpr uint8_t MADCTL_MV = 0x20;
21static constexpr uint8_t MADCTL_ML = 0x10;
22static constexpr uint8_t MADCTL_BGR = 0x08;
23static constexpr uint8_t MADCTL_XFLIP = 0x02;
24static constexpr uint8_t MADCTL_YFLIP = 0x01;
30 pin->digital_write(
true);
54 this->
write(value, 9);
66 this->
write(value | 0x100, 9);
81 while (index != vec.size()) {
82 if (vec.size() - index < 2) {
83 this->
mark_failed(LOG_STR(
"Malformed init sequence"));
86 uint8_t cmd = vec[index++];
87 uint8_t
x = vec[index++];
88 if (
x == DELAY_FLAG) {
89 ESP_LOGD(TAG,
"Delay %dms", cmd);
92 uint8_t num_args =
x & 0x7F;
93 if (vec.size() - index < num_args) {
94 this->
mark_failed(LOG_STR(
"Malformed init sequence"));
100 const auto *ptr = vec.data() + index;
102 ESP_LOGD(TAG,
"Write command %02X, length %d, byte(s) %s", cmd, num_args,
106 while (num_args-- != 0)
119 LOG_PIN(
" CS Pin: ", this->
cs_);
120 LOG_PIN(
" DC Pin: ", this->
dc_pin_);
121 ESP_LOGCONFIG(TAG,
" SPI Data rate: %uMHz", (
unsigned) (this->
data_rate_ / 1000000));
132 esp_lcd_rgb_panel_config_t config{};
133 config.flags.fb_in_psram = 1;
134 config.bounce_buffer_size_px = this->
width_ * 10;
136 config.timings.h_res = this->
width_;
137 config.timings.v_res = this->
height_;
146 config.clk_src = LCD_CLK_SRC_PLL160M;
148 for (
size_t i = 0; i != data_pin_count; i++) {
149 config.data_gpio_nums[i] =
static_cast<gpio_num_t
>(this->
data_pins_[i]->
get_pin());
151 config.data_width = data_pin_count;
152 config.disp_gpio_num = GPIO_NUM_NC;
156 config.hsync_gpio_num = GPIO_NUM_NC;
161 config.vsync_gpio_num = GPIO_NUM_NC;
164 config.de_gpio_num =
static_cast<gpio_num_t
>(this->
de_pin_->
get_pin());
166 config.de_gpio_num = GPIO_NUM_NC;
169 esp_err_t err = esp_lcd_new_rgb_panel(&config, &this->
handle_);
171 err = esp_lcd_panel_reset(this->
handle_);
173 err = esp_lcd_panel_init(this->
handle_);
175 ESP_LOGE(TAG,
"lcd setup failed: %s", esp_err_to_name(err));
178 ESP_LOGCONFIG(TAG,
"MipiRgb setup complete");
183 esp_lcd_rgb_panel_restart(this->
handle_);
194 }
else if (this->
page_ !=
nullptr) {
207 this->x_low_, this->y_low_, this->
width_ - w - this->x_low_);
209 this->x_low_ = this->
width_;
222 Display::draw_pixels_at(x_start, y_start, w,
h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
224 this->
width_ - w - x_start);
232 esp_err_t err = ESP_OK;
233 auto stride = (x_offset + w + x_pad) * 2;
234 ptr += y_offset * stride + x_offset * 2;
236 if (x_offset == 0 && x_pad == 0) {
237 err = esp_lcd_panel_draw_bitmap(this->
handle_, x_start, y_start, x_start + w, y_start +
h, ptr);
240 for (
int y = 0;
y !=
h;
y++) {
241 err = esp_lcd_panel_draw_bitmap(this->
handle_, x_start,
y + y_start, x_start + w,
y + y_start + 1, ptr);
248 ESP_LOGE(TAG,
"lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
259 if (this->
buffer_ ==
nullptr) {
260 this->
mark_failed(LOG_STR(
"Could not allocate buffer for display!"));
293 if (this->
buffer_[pos] == new_color)
312 Display::fill(color);
316 auto *ptr_16 =
reinterpret_cast<uint16_t *
>(this->
buffer_);
318 std::fill_n(ptr_16, this->
width_ * this->
height_, new_color);
322 this->
y_high_ = this->height_ - 1;
349static const char *get_pin_name(
GPIOPin *pin, std::span<char, GPIO_SUMMARY_MAX_LEN> buffer) {
353 return buffer.data();
358 for (uint8_t i = start; i !=
end; i++) {
360 ESP_LOGCONFIG(TAG,
" %s pin %d: %s", name, offset++, pin_summary);
375 "\n Rotation: %d degrees"
376 "\n PCLK Inverted: %s"
377 "\n HSync Pulse Width: %u"
378 "\n HSync Back Porch: %u"
379 "\n HSync Front Porch: %u"
380 "\n VSync Pulse Width: %u"
381 "\n VSync Back Porch: %u"
382 "\n VSync Front Porch: %u"
383 "\n Invert Colors: %s"
384 "\n Pixel Clock: %uMHz"
394 get_pin_name(this->
de_pin_, de_buf), get_pin_name(this->
pclk_pin_, pclk_buf),
void mark_failed()
Mark this component as failed.
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.
virtual uint8_t get_pin() const =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_
const display_writer_t & get_writer() const
InternalGPIOPin * de_pin_
std::vector< GPIOPin * > enable_pins_
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 hsync_pulse_width_
uint16_t hsync_front_porch_
uint16_t vsync_back_porch_
esp_lcd_panel_handle_t handle_
int get_height() override
InternalGPIOPin * hsync_pin_
void dump_config() override
uint16_t vsync_front_porch_
int get_height_internal() override
uint16_t vsync_pulse_width_
InternalGPIOPin * data_pins_[16]
void draw_pixel_at(int x, int y, Color color) override
InternalGPIOPin * vsync_pin_
void dump_pins_(uint8_t start, uint8_t end, const char *name, uint8_t offset)
InternalGPIOPin * pclk_pin_
uint16_t hsync_back_porch_
void fill(Color color) override
int get_width_internal() override
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)
void dump_config() override
void write_data_(uint8_t value)
void write_init_sequence_()
this relies upon the init sequence being well-formed, which is guaranteed by the Python init code.
void write_command_(uint8_t value)
std::vector< uint8_t > init_sequence_
void spi_setup() override
void write_byte(uint8_t data)
void write(uint16_t data, size_t num_bits)
@ DISPLAY_ROTATION_0_DEGREES
@ DISPLAY_ROTATION_270_DEGREES
@ DISPLAY_ROTATION_180_DEGREES
@ DISPLAY_ROTATION_90_DEGREES
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".
constexpr size_t GPIO_SUMMARY_MAX_LEN
Maximum buffer size for dump_summary output.
void HOT delay(uint32_t ms)