ESPHome  2024.10.1
st7701s.h
Go to the documentation of this file.
1 //
2 // Created by Clyde Stubbs on 29/10/2023.
3 //
4 #pragma once
5 
6 // only applicable on ESP32-S3
7 #ifdef USE_ESP32_VARIANT_ESP32S3
11 #include "esp_lcd_panel_ops.h"
12 
13 #include "esp_lcd_panel_rgb.h"
14 
15 namespace esphome {
16 namespace st7701s {
17 
18 constexpr static const char *const TAG = "display.st7701s";
19 const uint8_t SW_RESET_CMD = 0x01;
20 const uint8_t SLEEP_OUT = 0x11;
21 const uint8_t SDIR_CMD = 0xC7;
22 const uint8_t MADCTL_CMD = 0x36;
23 const uint8_t INVERT_OFF = 0x20;
24 const uint8_t INVERT_ON = 0x21;
25 const uint8_t DISPLAY_ON = 0x29;
26 const uint8_t CMD2_BKSEL = 0xFF;
27 const uint8_t CMD2_BK0[5] = {0x77, 0x01, 0x00, 0x00, 0x10};
28 const uint8_t ST7701S_DELAY_FLAG = 0xFF;
29 
30 class ST7701S : public display::Display,
31  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
32  spi::DATA_RATE_1MHZ> {
33  public:
34  void update() override { this->do_update_(); }
35  void setup() override;
36  void complete_setup_();
37  void loop() override;
38  void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
39  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override;
40 
42  void set_color_mode(display::ColorOrder color_mode) { this->color_mode_ = color_mode; }
43  void set_invert_colors(bool invert_colors) { this->invert_colors_ = invert_colors; }
44 
45  void add_data_pin(InternalGPIOPin *data_pin, size_t index) { this->data_pins_[index] = data_pin; };
46  void set_de_pin(InternalGPIOPin *de_pin) { this->de_pin_ = de_pin; }
47  void set_pclk_pin(InternalGPIOPin *pclk_pin) { this->pclk_pin_ = pclk_pin; }
48  void set_vsync_pin(InternalGPIOPin *vsync_pin) { this->vsync_pin_ = vsync_pin; }
49  void set_hsync_pin(InternalGPIOPin *hsync_pin) { this->hsync_pin_ = hsync_pin; }
50  void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
51  void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
52  void set_width(uint16_t width) { this->width_ = width; }
53  void set_pclk_frequency(uint32_t pclk_frequency) { this->pclk_frequency_ = pclk_frequency; }
54  void set_pclk_inverted(bool inverted) { this->pclk_inverted_ = inverted; }
55  void set_dimensions(uint16_t width, uint16_t height) {
56  this->width_ = width;
57  this->height_ = height;
58  }
59  int get_width() override { return this->width_; }
60  int get_height() override { return this->height_; }
61  void set_hsync_back_porch(uint16_t hsync_back_porch) { this->hsync_back_porch_ = hsync_back_porch; }
62  void set_hsync_front_porch(uint16_t hsync_front_porch) { this->hsync_front_porch_ = hsync_front_porch; }
63  void set_hsync_pulse_width(uint16_t hsync_pulse_width) { this->hsync_pulse_width_ = hsync_pulse_width; }
64  void set_vsync_pulse_width(uint16_t vsync_pulse_width) { this->vsync_pulse_width_ = vsync_pulse_width; }
65  void set_vsync_back_porch(uint16_t vsync_back_porch) { this->vsync_back_porch_ = vsync_back_porch; }
66  void set_vsync_front_porch(uint16_t vsync_front_porch) { this->vsync_front_porch_ = vsync_front_porch; }
67  void set_init_sequence(const std::vector<uint8_t> &init_sequence) { this->init_sequence_ = init_sequence; }
68  void set_mirror_x(bool mirror_x) { this->mirror_x_ = mirror_x; }
69  void set_mirror_y(bool mirror_y) { this->mirror_y_ = mirror_y; }
70  void set_offsets(int16_t offset_x, int16_t offset_y) {
71  this->offset_x_ = offset_x;
72  this->offset_y_ = offset_y;
73  }
75  int get_width_internal() override { return this->width_; }
76  int get_height_internal() override { return this->height_; }
77  void dump_config() override;
78  void draw_pixel_at(int x, int y, Color color) override;
79 
80  // this will be horribly slow.
81  protected:
82  void write_command_(uint8_t value);
83  void write_data_(uint8_t value);
84  void write_sequence_(uint8_t cmd, size_t len, const uint8_t *bytes);
85  void write_init_sequence_();
86 
91  GPIOPin *reset_pin_{nullptr};
92  GPIOPin *dc_pin_{nullptr};
94  uint16_t hsync_pulse_width_ = 10;
95  uint16_t hsync_back_porch_ = 10;
96  uint16_t hsync_front_porch_ = 20;
97  uint16_t vsync_pulse_width_ = 10;
98  uint16_t vsync_back_porch_ = 10;
99  uint16_t vsync_front_porch_ = 10;
100  std::vector<uint8_t> init_sequence_;
101  uint32_t pclk_frequency_ = 16 * 1000 * 1000;
102  bool pclk_inverted_{true};
103 
106  size_t width_{};
107  size_t height_{};
108  int16_t offset_x_{0};
109  int16_t offset_y_{0};
110  bool mirror_x_{};
111  bool mirror_y_{};
112 
113  esp_lcd_panel_handle_t handle_{};
114 };
115 
116 } // namespace st7701s
117 } // namespace esphome
118 #endif
const uint8_t INVERT_ON
Definition: st7701s.h:24
void set_init_sequence(const std::vector< uint8_t > &init_sequence)
Definition: st7701s.h:67
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
void set_mirror_y(bool mirror_y)
Definition: st7701s.h:69
void set_pclk_pin(InternalGPIOPin *pclk_pin)
Definition: st7701s.h:47
InternalGPIOPin * pclk_pin_
Definition: st7701s.h:88
void set_dc_pin(GPIOPin *dc_pin)
Definition: st7701s.h:50
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
void set_hsync_pin(InternalGPIOPin *hsync_pin)
Definition: st7701s.h:49
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 set_pclk_inverted(bool inverted)
Definition: st7701s.h:54
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
int get_width_internal() override
Definition: st7701s.h:75
const uint8_t CMD2_BK0[5]
Definition: st7701s.h:27
void write_command_(uint8_t value)
Definition: st7701s.cpp:115
void update() override
Definition: st7701s.h:34
void set_hsync_back_porch(uint16_t hsync_back_porch)
Definition: st7701s.h:61
uint16_t vsync_back_porch_
Definition: st7701s.h:98
void set_vsync_back_porch(uint16_t vsync_back_porch)
Definition: st7701s.h:65
uint8_t h
Definition: bl0906.h:209
uint16_t vsync_pulse_width_
Definition: st7701s.h:97
void set_pclk_frequency(uint32_t pclk_frequency)
Definition: st7701s.h:53
InternalGPIOPin * de_pin_
Definition: st7701s.h:87
void set_vsync_pulse_width(uint16_t vsync_pulse_width)
Definition: st7701s.h:64
uint16_t y
Definition: tt21100.cpp:18
const uint8_t ST7701S_DELAY_FLAG
Definition: st7701s.h:28
void loop() override
Definition: st7701s.cpp:50
The SPIDevice is what components using the SPI will create.
Definition: spi.h:391
void dump_config() override
Definition: st7701s.cpp:182
void set_offsets(int16_t offset_x, int16_t offset_y)
Definition: st7701s.h:70
const char *const TAG
Definition: spi.cpp:8
const uint8_t DISPLAY_ON
Definition: st7701s.h:25
display::ColorOrder color_mode_
Definition: st7701s.h:105
void set_mirror_x(bool mirror_x)
Definition: st7701s.h:68
int get_height_internal() override
Definition: st7701s.h:76
void set_invert_colors(bool invert_colors)
Definition: st7701s.h:43
void set_dimensions(uint16_t width, uint16_t height)
Definition: st7701s.h:55
const uint8_t SDIR_CMD
Definition: st7701s.h:21
display::ColorOrder get_color_mode()
Definition: st7701s.h:41
void set_color_mode(display::ColorOrder color_mode)
Definition: st7701s.h:42
void set_vsync_pin(InternalGPIOPin *vsync_pin)
Definition: st7701s.h:48
void set_reset_pin(GPIOPin *reset_pin)
Definition: st7701s.h:51
void set_width(uint16_t width)
Definition: st7701s.h:52
std::vector< uint8_t > init_sequence_
Definition: st7701s.h:100
void add_data_pin(InternalGPIOPin *data_pin, size_t index)
Definition: st7701s.h:45
void setup() override
Definition: st7701s.cpp:8
std::string size_t len
Definition: helpers.h:292
void set_hsync_front_porch(uint16_t hsync_front_porch)
Definition: st7701s.h:62
int get_height() override
Definition: st7701s.h:60
uint16_t hsync_front_porch_
Definition: st7701s.h:96
void set_de_pin(InternalGPIOPin *de_pin)
Definition: st7701s.h:46
display::DisplayType get_display_type() override
Definition: st7701s.h:74
void set_vsync_front_porch(uint16_t vsync_front_porch)
Definition: st7701s.h:66
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
int get_width() override
Definition: st7701s.h:59
esp_lcd_panel_handle_t handle_
Definition: st7701s.h:113
void set_hsync_pulse_width(uint16_t hsync_pulse_width)
Definition: st7701s.h:63
stm32_cmd_t * cmd
Definition: stm32flash.h:96
const uint8_t CMD2_BKSEL
Definition: st7701s.h:26