ESPHome  2024.11.0
rpi_dpi_rgb.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32_VARIANT_ESP32S3
2 #include "rpi_dpi_rgb.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace rpi_dpi_rgb {
7 
9  ESP_LOGCONFIG(TAG, "Setting up RPI_DPI_RGB");
10  this->reset_display_();
11  esp_lcd_rgb_panel_config_t config{};
12  config.flags.fb_in_psram = 1;
13 #if ESP_IDF_VERSION_MAJOR >= 5
14  config.bounce_buffer_size_px = this->width_ * 10;
15  config.num_fbs = 1;
16 #endif // ESP_IDF_VERSION_MAJOR
17  config.timings.h_res = this->width_;
18  config.timings.v_res = this->height_;
19  config.timings.hsync_pulse_width = this->hsync_pulse_width_;
20  config.timings.hsync_back_porch = this->hsync_back_porch_;
21  config.timings.hsync_front_porch = this->hsync_front_porch_;
22  config.timings.vsync_pulse_width = this->vsync_pulse_width_;
23  config.timings.vsync_back_porch = this->vsync_back_porch_;
24  config.timings.vsync_front_porch = this->vsync_front_porch_;
25  config.timings.flags.pclk_active_neg = this->pclk_inverted_;
26  config.timings.pclk_hz = this->pclk_frequency_;
27  config.clk_src = LCD_CLK_SRC_PLL160M;
28  config.psram_trans_align = 64;
29  size_t data_pin_count = sizeof(this->data_pins_) / sizeof(this->data_pins_[0]);
30  for (size_t i = 0; i != data_pin_count; i++) {
31  config.data_gpio_nums[i] = this->data_pins_[i]->get_pin();
32  }
33  config.data_width = data_pin_count;
34  config.disp_gpio_num = -1;
35  config.hsync_gpio_num = this->hsync_pin_->get_pin();
36  config.vsync_gpio_num = this->vsync_pin_->get_pin();
37  config.de_gpio_num = this->de_pin_->get_pin();
38  config.pclk_gpio_num = this->pclk_pin_->get_pin();
39  esp_err_t err = esp_lcd_new_rgb_panel(&config, &this->handle_);
40  if (err != ESP_OK) {
41  ESP_LOGE(TAG, "lcd_new_rgb_panel failed: %s", esp_err_to_name(err));
42  this->mark_failed();
43  return;
44  }
45  ESP_ERROR_CHECK(esp_lcd_panel_reset(this->handle_));
46  ESP_ERROR_CHECK(esp_lcd_panel_init(this->handle_));
47  ESP_LOGCONFIG(TAG, "RPI_DPI_RGB setup complete");
48 }
50 #if ESP_IDF_VERSION_MAJOR >= 5
51  if (this->handle_ != nullptr)
52  esp_lcd_rgb_panel_restart(this->handle_);
53 #endif // ESP_IDF_VERSION_MAJOR
54 }
55 
56 void RpiDpiRgb::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
57  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
58  if (w <= 0 || h <= 0)
59  return;
60  // if color mapping is required, pass the buck.
61  // note that endianness is not considered here - it is assumed to match!
62  if (bitness != display::COLOR_BITNESS_565) {
63  return display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
64  x_pad);
65  }
66  x_start += this->offset_x_;
67  y_start += this->offset_y_;
68  esp_err_t err = ESP_OK;
69  // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
70  if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
71  // we could deal here with a non-zero y_offset, but if x_offset is zero, y_offset probably will be so don't bother
72  err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
73  } else {
74  // draw line by line
75  auto stride = x_offset + w + x_pad;
76  for (int y = 0; y != h; y++) {
77  err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1,
78  ptr + ((y + y_offset) * stride + x_offset) * 2);
79  if (err != ESP_OK)
80  break;
81  }
82  }
83  if (err != ESP_OK)
84  ESP_LOGE(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
85 }
86 
88  switch (this->rotation_) {
91  return this->get_height_internal();
92  default:
93  return this->get_width_internal();
94  }
95 }
96 
98  switch (this->rotation_) {
101  return this->get_width_internal();
102  default:
103  return this->get_height_internal();
104  }
105 }
106 
107 void RpiDpiRgb::draw_pixel_at(int x, int y, Color color) {
108  if (!this->get_clipping().inside(x, y))
109  return; // NOLINT
110 
111  switch (this->rotation_) {
113  break;
115  std::swap(x, y);
116  x = this->width_ - x - 1;
117  break;
119  x = this->width_ - x - 1;
120  y = this->height_ - y - 1;
121  break;
123  std::swap(x, y);
124  y = this->height_ - y - 1;
125  break;
126  }
128 
129  this->draw_pixels_at(x, y, 1, 1, (const uint8_t *) &pixel, display::COLOR_ORDER_RGB, display::COLOR_BITNESS_565, true,
130  0, 0, 0);
131  App.feed_wdt();
132 }
133 
135  ESP_LOGCONFIG("", "RPI_DPI_RGB LCD");
136  ESP_LOGCONFIG(TAG, " Height: %u", this->height_);
137  ESP_LOGCONFIG(TAG, " Width: %u", this->width_);
138  LOG_PIN(" DE Pin: ", this->de_pin_);
139  LOG_PIN(" Enable Pin: ", this->enable_pin_);
140  LOG_PIN(" Reset Pin: ", this->reset_pin_);
141  size_t data_pin_count = sizeof(this->data_pins_) / sizeof(this->data_pins_[0]);
142  for (size_t i = 0; i != data_pin_count; i++)
143  ESP_LOGCONFIG(TAG, " Data pin %d: %s", i, (this->data_pins_[i])->dump_summary().c_str());
144 }
145 
147  if (this->reset_pin_ != nullptr) {
148  this->reset_pin_->setup();
149  this->reset_pin_->digital_write(false);
150  if (this->enable_pin_ != nullptr) {
151  this->enable_pin_->setup();
152  this->enable_pin_->digital_write(false);
153  }
154  delay(1);
155  this->reset_pin_->digital_write(true);
156  if (this->enable_pin_ != nullptr) {
157  delay(11);
158  this->enable_pin_->digital_write(true);
159  }
160  }
161 }
162 
163 } // namespace rpi_dpi_rgb
164 } // namespace esphome
165 
166 #endif // USE_ESP32_VARIANT_ESP32S3
virtual void digital_write(bool value)=0
InternalGPIOPin * data_pins_[16]
Definition: rpi_dpi_rgb.h:75
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
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
Definition: rpi_dpi_rgb.cpp:56
uint16_t x
Definition: tt21100.cpp:17
int get_height_internal() override
Definition: rpi_dpi_rgb.h:67
virtual void setup()=0
uint8_t h
Definition: bl0906.h:209
Rect get_clipping() const
Get the current the clipping rectangle.
Definition: display.cpp:709
uint16_t y
Definition: tt21100.cpp:18
virtual uint8_t get_pin() const =0
constexpr14 T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order...
Definition: helpers.h:240
Application App
Global storage of Application pointer - only one Application can exist.
int get_width_internal() override
Definition: rpi_dpi_rgb.h:66
void swap(optional< T > &x, optional< T > &y)
Definition: optional.h:209
DisplayRotation rotation_
Definition: display.h:666
InternalGPIOPin * pclk_pin_
Definition: rpi_dpi_rgb.h:70
InternalGPIOPin * vsync_pin_
Definition: rpi_dpi_rgb.h:72
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
InternalGPIOPin * hsync_pin_
Definition: rpi_dpi_rgb.h:71
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&#39;s buffer...
Definition: display.cpp:54
esp_lcd_panel_handle_t handle_
Definition: rpi_dpi_rgb.h:92
void draw_pixel_at(int x, int y, Color color) override
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26