ESPHome  2024.5.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  esph_log_config(TAG, "Setting up RPI_DPI_RGB");
10  esp_lcd_rgb_panel_config_t config{};
11  config.flags.fb_in_psram = 1;
12  config.timings.h_res = this->width_;
13  config.timings.v_res = this->height_;
14  config.timings.hsync_pulse_width = this->hsync_pulse_width_;
15  config.timings.hsync_back_porch = this->hsync_back_porch_;
16  config.timings.hsync_front_porch = this->hsync_front_porch_;
17  config.timings.vsync_pulse_width = this->vsync_pulse_width_;
18  config.timings.vsync_back_porch = this->vsync_back_porch_;
19  config.timings.vsync_front_porch = this->vsync_front_porch_;
20  config.timings.flags.pclk_active_neg = this->pclk_inverted_;
21  config.timings.pclk_hz = this->pclk_frequency_;
22  config.clk_src = LCD_CLK_SRC_PLL160M;
23  config.sram_trans_align = 64;
24  config.psram_trans_align = 64;
25  size_t data_pin_count = sizeof(this->data_pins_) / sizeof(this->data_pins_[0]);
26  for (size_t i = 0; i != data_pin_count; i++) {
27  config.data_gpio_nums[i] = this->data_pins_[i]->get_pin();
28  }
29  config.data_width = data_pin_count;
30  config.disp_gpio_num = -1;
31  config.hsync_gpio_num = this->hsync_pin_->get_pin();
32  config.vsync_gpio_num = this->vsync_pin_->get_pin();
33  config.de_gpio_num = this->de_pin_->get_pin();
34  config.pclk_gpio_num = this->pclk_pin_->get_pin();
35  esp_err_t err = esp_lcd_new_rgb_panel(&config, &this->handle_);
36  if (err != ESP_OK) {
37  esph_log_e(TAG, "lcd_new_rgb_panel failed: %s", esp_err_to_name(err));
38  }
39  ESP_ERROR_CHECK(esp_lcd_panel_reset(this->handle_));
40  ESP_ERROR_CHECK(esp_lcd_panel_init(this->handle_));
41  esph_log_config(TAG, "RPI_DPI_RGB setup complete");
42 }
43 
44 void RpiDpiRgb::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
45  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
46  if (w <= 0 || h <= 0)
47  return;
48  // if color mapping is required, pass the buck.
49  // note that endianness is not considered here - it is assumed to match!
50  if (bitness != display::COLOR_BITNESS_565) {
51  return display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
52  x_pad);
53  }
54  x_start += this->offset_x_;
55  y_start += this->offset_y_;
56  esp_err_t err;
57  // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
58  if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
59  // 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
60  err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
61  } else {
62  // draw line by line
63  auto stride = x_offset + w + x_pad;
64  for (int y = 0; y != h; y++) {
65  err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1,
66  ptr + ((y + y_offset) * stride + x_offset) * 2);
67  if (err != ESP_OK)
68  break;
69  }
70  }
71  if (err != ESP_OK)
72  esph_log_e(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
73 }
74 
75 void RpiDpiRgb::draw_pixel_at(int x, int y, Color color) {
76  if (!this->get_clipping().inside(x, y))
77  return; // NOLINT
78 
79  switch (this->rotation_) {
81  break;
83  std::swap(x, y);
84  x = this->width_ - x - 1;
85  break;
87  x = this->width_ - x - 1;
88  y = this->height_ - y - 1;
89  break;
91  std::swap(x, y);
92  y = this->height_ - y - 1;
93  break;
94  }
96 
97  this->draw_pixels_at(x, y, 1, 1, (const uint8_t *) &pixel, display::COLOR_ORDER_RGB, display::COLOR_BITNESS_565, true,
98  0, 0, 0);
99  App.feed_wdt();
100 }
101 
103  ESP_LOGCONFIG("", "RPI_DPI_RGB LCD");
104  ESP_LOGCONFIG(TAG, " Height: %u", this->height_);
105  ESP_LOGCONFIG(TAG, " Width: %u", this->width_);
106  LOG_PIN(" DE Pin: ", this->de_pin_);
107  LOG_PIN(" Reset Pin: ", this->reset_pin_);
108  size_t data_pin_count = sizeof(this->data_pins_) / sizeof(this->data_pins_[0]);
109  for (size_t i = 0; i != data_pin_count; i++)
110  ESP_LOGCONFIG(TAG, " Data pin %d: %s", i, (this->data_pins_[i])->dump_summary().c_str());
111 }
112 
113 } // namespace rpi_dpi_rgb
114 } // namespace esphome
115 
116 #endif // USE_ESP32_VARIANT_ESP32S3
InternalGPIOPin * data_pins_[16]
Definition: rpi_dpi_rgb.h:70
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:44
uint16_t x
Definition: tt21100.cpp:17
Rect get_clipping() const
Get the current the clipping rectangle.
Definition: display.cpp:567
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:239
Application App
Global storage of Application pointer - only one Application can exist.
void swap(optional< T > &x, optional< T > &y)
Definition: optional.h:210
DisplayRotation rotation_
Definition: display.h:658
InternalGPIOPin * pclk_pin_
Definition: rpi_dpi_rgb.h:66
InternalGPIOPin * vsync_pin_
Definition: rpi_dpi_rgb.h:68
uint8_t h
Definition: bl0939.h:21
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
InternalGPIOPin * hsync_pin_
Definition: rpi_dpi_rgb.h:67
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:87
void draw_pixel_at(int x, int y, Color color) override
Definition: rpi_dpi_rgb.cpp:75