1#ifdef USE_ESP32_VARIANT_ESP32S3
5#include "esp_lcd_panel_rgb.h"
10static const uint8_t DELAY_FLAG = 0xFF;
11static constexpr uint8_t MADCTL_MY = 0x80;
12static constexpr uint8_t MADCTL_MX = 0x40;
13static constexpr uint8_t MADCTL_MV = 0x20;
14static constexpr uint8_t MADCTL_ML = 0x10;
15static constexpr uint8_t MADCTL_BGR = 0x08;
16static constexpr uint8_t MADCTL_XFLIP = 0x02;
17static constexpr uint8_t MADCTL_YFLIP = 0x01;
23 pin->digital_write(
true);
47 this->
write(value, 9);
59 this->
write(value | 0x100, 9);
74 while (index != vec.size()) {
75 if (vec.size() - index < 2) {
79 uint8_t cmd = vec[index++];
80 uint8_t
x = vec[index++];
81 if (
x == DELAY_FLAG) {
82 ESP_LOGD(TAG,
"Delay %dms", cmd);
85 uint8_t num_args =
x & 0x7F;
86 if (vec.size() - index < num_args) {
93 const auto *ptr = vec.data() + index;
94 ESP_LOGD(TAG,
"Write command %02X, length %d, byte(s) %s", cmd, num_args,
98 while (num_args-- != 0)
111 LOG_PIN(
" CS Pin: ", this->
cs_);
112 LOG_PIN(
" DC Pin: ", this->
dc_pin_);
114 " SPI Data rate: %uMHz"
118 "\n Color Order: %s",
119 (
unsigned) (this->
data_rate_ / 1000000), YESNO(this->
madctl_ & (MADCTL_XFLIP | MADCTL_MX)),
120 YESNO(this->
madctl_ & (MADCTL_YFLIP | MADCTL_MY | MADCTL_ML)), YESNO(this->
madctl_ & MADCTL_MV),
121 this->
madctl_ & MADCTL_BGR ?
"BGR" :
"RGB");
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++) {
151 config.data_width = data_pin_count;
152 config.disp_gpio_num = -1;
158 config.de_gpio_num = -1;
161 esp_err_t err = esp_lcd_new_rgb_panel(&config, &this->
handle_);
163 err = esp_lcd_panel_reset(this->
handle_);
165 err = esp_lcd_panel_init(this->
handle_);
167 auto msg =
str_sprintf(
"lcd setup failed: %s", esp_err_to_name(err));
170 ESP_LOGCONFIG(TAG,
"MipiRgb setup complete");
175 esp_lcd_rgb_panel_restart(this->
handle_);
186 }
else if (this->
page_ !=
nullptr) {
199 this->x_low_, this->y_low_, this->
width_ - w - this->x_low_);
201 this->x_low_ = this->
width_;
214 Display::draw_pixels_at(x_start, y_start, w,
h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
216 this->
width_ - w - x_start);
224 esp_err_t err = ESP_OK;
225 auto stride = (x_offset + w + x_pad) * 2;
226 ptr += y_offset * stride + x_offset * 2;
228 if (x_offset == 0 && x_pad == 0) {
229 err = esp_lcd_panel_draw_bitmap(this->
handle_, x_start, y_start, x_start + w, y_start +
h, ptr);
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);
240 ESP_LOGE(TAG,
"lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
251 if (this->
buffer_ ==
nullptr) {
252 this->
mark_failed(
"Could not allocate buffer for display!");
283 size_t pos = (
y * this->
width_) + x;
284 uint8_t hi_byte =
static_cast<uint8_t
>(color.
r & 0xF8) | (color.
g >> 5);
285 uint8_t lo_byte =
static_cast<uint8_t
>((color.
g & 0x1C) << 3) | (color.
b >> 3);
286 uint16_t new_color = hi_byte | (lo_byte << 8);
287 if (this->
buffer_[pos] == new_color)
289 this->
buffer_[pos] = new_color;
303 auto *ptr_16 =
reinterpret_cast<uint16_t *
>(this->
buffer_);
304 uint8_t hi_byte =
static_cast<uint8_t
>(color.
r & 0xF8) | (color.
g >> 5);
305 uint8_t lo_byte =
static_cast<uint8_t
>((color.
g & 0x1C) << 3) | (color.
b >> 3);
306 uint16_t new_color = lo_byte | (hi_byte << 8);
307 std::fill_n(ptr_16, this->
width_ * this->
height_, new_color);
334static std::string get_pin_name(
GPIOPin *pin) {
341 for (uint8_t i = start; i !=
end; i++) {
342 ESP_LOGCONFIG(TAG,
" %s pin %d: %s", name, offset++, this->
data_pins_[i]->dump_summary().c_str());
352 "\n Rotation: %d degrees"
353 "\n HSync Pulse Width: %u"
354 "\n HSync Back Porch: %u"
355 "\n HSync Front Porch: %u"
356 "\n VSync Pulse Width: %u"
357 "\n VSync Back Porch: %u"
358 "\n VSync Front Porch: %u"
359 "\n Invert Colors: %s"
360 "\n Pixel Clock: %dMHz"
373 if (this->
madctl_ & MADCTL_BGR) {
virtual void mark_failed()
Mark this component as failed.
virtual std::string dump_summary() const =0
virtual void digital_write(bool value)=0
virtual uint8_t get_pin() const =0
An STL allocator that uses SPI or internal RAM.
void clear()
Clear the entire screen by filling it with OFF pixels.
optional< display_writer_t > writer_
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_
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 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.
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.
std::string str_sprintf(const char *fmt,...)
void IRAM_ATTR HOT delay(uint32_t ms)