ESPHome  2024.10.2
st7701s.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32_VARIANT_ESP32S3
2 #include "st7701s.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace st7701s {
7 
8 void ST7701S::setup() {
9  esph_log_config(TAG, "Setting up ST7701S");
10  this->spi_setup();
11  this->write_init_sequence_();
12 
13  esp_lcd_rgb_panel_config_t config{};
14  config.flags.fb_in_psram = 1;
15 #if ESP_IDF_VERSION_MAJOR >= 5
16  config.bounce_buffer_size_px = this->width_ * 10;
17  config.num_fbs = 1;
18 #endif // ESP_IDF_VERSION_MAJOR
19  config.timings.h_res = this->width_;
20  config.timings.v_res = this->height_;
21  config.timings.hsync_pulse_width = this->hsync_pulse_width_;
22  config.timings.hsync_back_porch = this->hsync_back_porch_;
23  config.timings.hsync_front_porch = this->hsync_front_porch_;
24  config.timings.vsync_pulse_width = this->vsync_pulse_width_;
25  config.timings.vsync_back_porch = this->vsync_back_porch_;
26  config.timings.vsync_front_porch = this->vsync_front_porch_;
27  config.timings.flags.pclk_active_neg = this->pclk_inverted_;
28  config.timings.pclk_hz = this->pclk_frequency_;
29  config.clk_src = LCD_CLK_SRC_PLL160M;
30  config.psram_trans_align = 64;
31  size_t data_pin_count = sizeof(this->data_pins_) / sizeof(this->data_pins_[0]);
32  for (size_t i = 0; i != data_pin_count; i++) {
33  config.data_gpio_nums[i] = this->data_pins_[i]->get_pin();
34  }
35  config.data_width = data_pin_count;
36  config.disp_gpio_num = -1;
37  config.hsync_gpio_num = this->hsync_pin_->get_pin();
38  config.vsync_gpio_num = this->vsync_pin_->get_pin();
39  config.de_gpio_num = this->de_pin_->get_pin();
40  config.pclk_gpio_num = this->pclk_pin_->get_pin();
41  esp_err_t err = esp_lcd_new_rgb_panel(&config, &this->handle_);
42  ESP_ERROR_CHECK(esp_lcd_panel_reset(this->handle_));
43  ESP_ERROR_CHECK(esp_lcd_panel_init(this->handle_));
44  if (err != ESP_OK) {
45  esph_log_e(TAG, "lcd_new_rgb_panel failed: %s", esp_err_to_name(err));
46  }
47  esph_log_config(TAG, "ST7701S setup complete");
48 }
49 
50 void ST7701S::loop() {
51 #if ESP_IDF_VERSION_MAJOR >= 5
52  if (this->handle_ != nullptr)
53  esp_lcd_rgb_panel_restart(this->handle_);
54 #endif // ESP_IDF_VERSION_MAJOR
55 }
56 
57 void ST7701S::draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
58  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
59  if (w <= 0 || h <= 0)
60  return;
61  // if color mapping is required, pass the buck.
62  // note that endianness is not considered here - it is assumed to match!
63  if (bitness != display::COLOR_BITNESS_565) {
64  return display::Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset,
65  x_pad);
66  }
67  x_start += this->offset_x_;
68  y_start += this->offset_y_;
69  esp_err_t err;
70  // x_ and y_offset are offsets into the source buffer, unrelated to our own offsets into the display.
71  if (x_offset == 0 && x_pad == 0 && y_offset == 0) {
72  // 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
73  err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y_start, x_start + w, y_start + h, ptr);
74  } else {
75  // draw line by line
76  auto stride = x_offset + w + x_pad;
77  for (int y = 0; y != h; y++) {
78  err = esp_lcd_panel_draw_bitmap(this->handle_, x_start, y + y_start, x_start + w, y + y_start + 1,
79  ptr + ((y + y_offset) * stride + x_offset) * 2);
80  if (err != ESP_OK)
81  break;
82  }
83  }
84  if (err != ESP_OK)
85  esph_log_e(TAG, "lcd_lcd_panel_draw_bitmap failed: %s", esp_err_to_name(err));
86 }
87 
88 void ST7701S::draw_pixel_at(int x, int y, Color color) {
89  if (!this->get_clipping().inside(x, y))
90  return; // NOLINT
91 
92  switch (this->rotation_) {
94  break;
96  std::swap(x, y);
97  x = this->width_ - x - 1;
98  break;
100  x = this->width_ - x - 1;
101  y = this->height_ - y - 1;
102  break;
104  std::swap(x, y);
105  y = this->height_ - y - 1;
106  break;
107  }
109 
110  this->draw_pixels_at(x, y, 1, 1, (const uint8_t *) &pixel, display::COLOR_ORDER_RGB, display::COLOR_BITNESS_565, true,
111  0, 0, 0);
112  App.feed_wdt();
113 }
114 
115 void ST7701S::write_command_(uint8_t value) {
116  this->enable();
117  if (this->dc_pin_ == nullptr) {
118  this->write(value, 9);
119  } else {
120  this->dc_pin_->digital_write(false);
121  this->write_byte(value);
122  this->dc_pin_->digital_write(true);
123  }
124  this->disable();
125 }
126 
127 void ST7701S::write_data_(uint8_t value) {
128  this->enable();
129  if (this->dc_pin_ == nullptr) {
130  this->write(value | 0x100, 9);
131  } else {
132  this->dc_pin_->digital_write(true);
133  this->write_byte(value);
134  }
135  this->disable();
136 }
137 
142 void ST7701S::write_sequence_(uint8_t cmd, size_t len, const uint8_t *bytes) {
143  this->write_command_(cmd);
144  while (len-- != 0)
145  this->write_data_(*bytes++);
146 }
147 
149  for (size_t i = 0; i != this->init_sequence_.size();) {
150  uint8_t cmd = this->init_sequence_[i++];
151  size_t len = this->init_sequence_[i++];
152  if (len == ST7701S_DELAY_FLAG) {
153  ESP_LOGV(TAG, "Delay %dms", cmd);
154  delay(cmd);
155  } else {
156  this->write_sequence_(cmd, len, &this->init_sequence_[i]);
157  i += len;
158  ESP_LOGV(TAG, "Command %X, %d bytes", cmd, len);
159  if (cmd == SW_RESET_CMD)
160  delay(6);
161  }
162  }
163  // st7701 does not appear to support axis swapping
164  this->write_sequence_(CMD2_BKSEL, sizeof(CMD2_BK0), CMD2_BK0);
165  this->write_command_(SDIR_CMD); // this is in the BK0 command set
166  this->write_data_(this->mirror_x_ ? 0x04 : 0x00);
167  uint8_t val = this->color_mode_ == display::COLOR_ORDER_BGR ? 0x08 : 0x00;
168  if (this->mirror_y_)
169  val |= 0x10;
170  this->write_command_(MADCTL_CMD);
171  this->write_data_(val);
172  ESP_LOGD(TAG, "write MADCTL %X", val);
174  // can't avoid this inline delay due to the need to complete setup before anything else tries to draw.
175  delay(120); // NOLINT
176  this->write_command_(SLEEP_OUT);
177  this->write_command_(DISPLAY_ON);
178  this->spi_teardown(); // SPI not needed after this
179  delay(10);
180 }
181 
183  ESP_LOGCONFIG("", "ST7701S RGB LCD");
184  ESP_LOGCONFIG(TAG, " Height: %u", this->height_);
185  ESP_LOGCONFIG(TAG, " Width: %u", this->width_);
186  LOG_PIN(" CS Pin: ", this->cs_);
187  LOG_PIN(" DC Pin: ", this->dc_pin_);
188  LOG_PIN(" DE Pin: ", this->de_pin_);
189  LOG_PIN(" Reset Pin: ", this->reset_pin_);
190  size_t data_pin_count = sizeof(this->data_pins_) / sizeof(this->data_pins_[0]);
191  for (size_t i = 0; i != data_pin_count; i++)
192  ESP_LOGCONFIG(TAG, " Data pin %d: %s", i, (this->data_pins_[i])->dump_summary().c_str());
193  ESP_LOGCONFIG(TAG, " SPI Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000));
194 }
195 
196 } // namespace st7701s
197 } // namespace esphome
198 #endif // USE_ESP32_VARIANT_ESP32S3
virtual void digital_write(bool value)=0
const uint8_t INVERT_ON
Definition: st7701s.h:24
InternalGPIOPin * hsync_pin_
Definition: st7701s.h:89
uint16_t hsync_pulse_width_
Definition: st7701s.h:94
uint16_t hsync_back_porch_
Definition: st7701s.h:95
InternalGPIOPin * pclk_pin_
Definition: st7701s.h:88
const uint8_t INVERT_OFF
Definition: st7701s.h:23
const uint8_t MADCTL_CMD
Definition: st7701s.h:22
const uint8_t SLEEP_OUT
Definition: st7701s.h:20
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: st7701s.cpp:57
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
void write_sequence_(uint8_t cmd, size_t len, const uint8_t *bytes)
this relies upon the init sequence being well-formed, which is guaranteed by the Python init code...
Definition: st7701s.cpp:142
uint16_t vsync_front_porch_
Definition: st7701s.h:99
void write_data_(uint8_t value)
Definition: st7701s.cpp:127
InternalGPIOPin * vsync_pin_
Definition: st7701s.h:90
uint16_t x
Definition: tt21100.cpp:17
InternalGPIOPin * data_pins_[16]
Definition: st7701s.h:93
const uint8_t CMD2_BK0[5]
Definition: st7701s.h:27
void write_command_(uint8_t value)
Definition: st7701s.cpp:115
uint16_t vsync_back_porch_
Definition: st7701s.h:98
mopeka_std_values val[4]
GPIOPin * cs_
Definition: spi.h:378
uint8_t h
Definition: bl0906.h:209
Rect get_clipping() const
Get the current the clipping rectangle.
Definition: display.cpp:567
uint16_t vsync_pulse_width_
Definition: st7701s.h:97
InternalGPIOPin * de_pin_
Definition: st7701s.h:87
uint16_t y
Definition: tt21100.cpp:18
const uint8_t ST7701S_DELAY_FLAG
Definition: st7701s.h:28
void loop() override
Definition: st7701s.cpp:50
void dump_config() override
Definition: st7701s.cpp:182
virtual uint8_t get_pin() const =0
const uint8_t DISPLAY_ON
Definition: st7701s.h:25
display::ColorOrder color_mode_
Definition: st7701s.h:105
void write(uint16_t data, size_t num_bits)
Write a single data item, up to 32 bits.
Definition: spi.h:423
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.
const uint8_t SDIR_CMD
Definition: st7701s.h:21
uint32_t data_rate_
Definition: spi.h:376
void swap(optional< T > &x, optional< T > &y)
Definition: optional.h:209
DisplayRotation rotation_
Definition: display.h:659
std::vector< uint8_t > init_sequence_
Definition: st7701s.h:100
void setup() override
Definition: st7701s.cpp:8
std::string size_t len
Definition: helpers.h:292
uint16_t hsync_front_porch_
Definition: st7701s.h:96
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const uint8_t SW_RESET_CMD
Definition: st7701s.h:19
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
void draw_pixel_at(int x, int y, Color color) override
Definition: st7701s.cpp:88
esp_lcd_panel_handle_t handle_
Definition: st7701s.h:113
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
stm32_cmd_t * cmd
Definition: stm32flash.h:96
const uint8_t CMD2_BKSEL
Definition: st7701s.h:26
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26