ESPHome  2024.5.0
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 
29 class ST7701S : public display::Display,
30  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
31  spi::DATA_RATE_1MHZ> {
32  public:
33  void update() override { this->do_update_(); }
34  void setup() override;
35  void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
36  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override;
37 
39  void set_color_mode(display::ColorOrder color_mode) { this->color_mode_ = color_mode; }
40  void set_invert_colors(bool invert_colors) { this->invert_colors_ = invert_colors; }
41 
42  void add_data_pin(InternalGPIOPin *data_pin, size_t index) { this->data_pins_[index] = data_pin; };
43  void set_de_pin(InternalGPIOPin *de_pin) { this->de_pin_ = de_pin; }
44  void set_pclk_pin(InternalGPIOPin *pclk_pin) { this->pclk_pin_ = pclk_pin; }
45  void set_vsync_pin(InternalGPIOPin *vsync_pin) { this->vsync_pin_ = vsync_pin; }
46  void set_hsync_pin(InternalGPIOPin *hsync_pin) { this->hsync_pin_ = hsync_pin; }
47  void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
48  void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
49  void set_width(uint16_t width) { this->width_ = width; }
50  void set_pclk_frequency(uint32_t pclk_frequency) { this->pclk_frequency_ = pclk_frequency; }
51  void set_pclk_inverted(bool inverted) { this->pclk_inverted_ = inverted; }
52  void set_dimensions(uint16_t width, uint16_t height) {
53  this->width_ = width;
54  this->height_ = height;
55  }
56  int get_width() override { return this->width_; }
57  int get_height() override { return this->height_; }
58  void set_hsync_back_porch(uint16_t hsync_back_porch) { this->hsync_back_porch_ = hsync_back_porch; }
59  void set_hsync_front_porch(uint16_t hsync_front_porch) { this->hsync_front_porch_ = hsync_front_porch; }
60  void set_hsync_pulse_width(uint16_t hsync_pulse_width) { this->hsync_pulse_width_ = hsync_pulse_width; }
61  void set_vsync_pulse_width(uint16_t vsync_pulse_width) { this->vsync_pulse_width_ = vsync_pulse_width; }
62  void set_vsync_back_porch(uint16_t vsync_back_porch) { this->vsync_back_porch_ = vsync_back_porch; }
63  void set_vsync_front_porch(uint16_t vsync_front_porch) { this->vsync_front_porch_ = vsync_front_porch; }
64  void set_init_sequence(const std::vector<uint8_t> &init_sequence) { this->init_sequence_ = init_sequence; }
65  void set_mirror_x(bool mirror_x) { this->mirror_x_ = mirror_x; }
66  void set_mirror_y(bool mirror_y) { this->mirror_y_ = mirror_y; }
67  void set_offsets(int16_t offset_x, int16_t offset_y) {
68  this->offset_x_ = offset_x;
69  this->offset_y_ = offset_y;
70  }
72  int get_width_internal() override { return this->width_; }
73  int get_height_internal() override { return this->height_; }
74  void dump_config() override;
75  void draw_pixel_at(int x, int y, Color color) override;
76 
77  // this will be horribly slow.
78  protected:
79  void write_command_(uint8_t value);
80  void write_data_(uint8_t value);
81  void write_sequence_(uint8_t cmd, size_t len, const uint8_t *bytes);
82  void write_init_sequence_();
83 
88  GPIOPin *reset_pin_{nullptr};
89  GPIOPin *dc_pin_{nullptr};
91  uint16_t hsync_pulse_width_ = 10;
92  uint16_t hsync_back_porch_ = 10;
93  uint16_t hsync_front_porch_ = 20;
94  uint16_t vsync_pulse_width_ = 10;
95  uint16_t vsync_back_porch_ = 10;
96  uint16_t vsync_front_porch_ = 10;
97  std::vector<uint8_t> init_sequence_;
98  uint32_t pclk_frequency_ = 16 * 1000 * 1000;
99  bool pclk_inverted_{true};
100 
103  size_t width_{};
104  size_t height_{};
105  int16_t offset_x_{0};
106  int16_t offset_y_{0};
107  bool mirror_x_{};
108  bool mirror_y_{};
109 
110  esp_lcd_panel_handle_t handle_{};
111 };
112 
113 } // namespace st7701s
114 } // namespace esphome
115 #endif
const uint8_t INVERT_ON
Definition: st7701s.h:24
void set_init_sequence(const std::vector< uint8_t > &init_sequence)
Definition: st7701s.h:64
InternalGPIOPin * hsync_pin_
Definition: st7701s.h:86
uint16_t hsync_pulse_width_
Definition: st7701s.h:91
uint16_t hsync_back_porch_
Definition: st7701s.h:92
void set_mirror_y(bool mirror_y)
Definition: st7701s.h:66
void set_pclk_pin(InternalGPIOPin *pclk_pin)
Definition: st7701s.h:44
InternalGPIOPin * pclk_pin_
Definition: st7701s.h:85
void set_dc_pin(GPIOPin *dc_pin)
Definition: st7701s.h:47
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:46
void set_hsync_pin(InternalGPIOPin *hsync_pin)
Definition: st7701s.h:46
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:131
uint16_t vsync_front_porch_
Definition: st7701s.h:96
void set_pclk_inverted(bool inverted)
Definition: st7701s.h:51
void write_data_(uint8_t value)
Definition: st7701s.cpp:116
InternalGPIOPin * vsync_pin_
Definition: st7701s.h:87
uint16_t x
Definition: tt21100.cpp:17
InternalGPIOPin * data_pins_[16]
Definition: st7701s.h:90
int get_width_internal() override
Definition: st7701s.h:72
const uint8_t CMD2_BK0[5]
Definition: st7701s.h:27
void write_command_(uint8_t value)
Definition: st7701s.cpp:104
void update() override
Definition: st7701s.h:33
void set_hsync_back_porch(uint16_t hsync_back_porch)
Definition: st7701s.h:58
uint16_t vsync_back_porch_
Definition: st7701s.h:95
void set_vsync_back_porch(uint16_t vsync_back_porch)
Definition: st7701s.h:62
uint16_t vsync_pulse_width_
Definition: st7701s.h:94
void set_pclk_frequency(uint32_t pclk_frequency)
Definition: st7701s.h:50
InternalGPIOPin * de_pin_
Definition: st7701s.h:84
void set_vsync_pulse_width(uint16_t vsync_pulse_width)
Definition: st7701s.h:61
uint16_t y
Definition: tt21100.cpp:18
The SPIDevice is what components using the SPI will create.
Definition: spi.h:408
void dump_config() override
Definition: st7701s.cpp:164
void set_offsets(int16_t offset_x, int16_t offset_y)
Definition: st7701s.h:67
uint32_t pclk_frequency_
Definition: st7701s.h:98
const char *const TAG
Definition: spi.cpp:8
const uint8_t DISPLAY_ON
Definition: st7701s.h:25
display::ColorOrder color_mode_
Definition: st7701s.h:102
void set_mirror_x(bool mirror_x)
Definition: st7701s.h:65
int get_height_internal() override
Definition: st7701s.h:73
void set_invert_colors(bool invert_colors)
Definition: st7701s.h:40
void set_dimensions(uint16_t width, uint16_t height)
Definition: st7701s.h:52
const uint8_t SDIR_CMD
Definition: st7701s.h:21
display::ColorOrder get_color_mode()
Definition: st7701s.h:38
void set_color_mode(display::ColorOrder color_mode)
Definition: st7701s.h:39
void set_vsync_pin(InternalGPIOPin *vsync_pin)
Definition: st7701s.h:45
void set_reset_pin(GPIOPin *reset_pin)
Definition: st7701s.h:48
void set_width(uint16_t width)
Definition: st7701s.h:49
std::vector< uint8_t > init_sequence_
Definition: st7701s.h:97
void add_data_pin(InternalGPIOPin *data_pin, size_t index)
Definition: st7701s.h:42
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:59
uint8_t h
Definition: bl0939.h:21
int get_height() override
Definition: st7701s.h:57
uint16_t hsync_front_porch_
Definition: st7701s.h:93
void set_de_pin(InternalGPIOPin *de_pin)
Definition: st7701s.h:43
display::DisplayType get_display_type() override
Definition: st7701s.h:71
void set_vsync_front_porch(uint16_t vsync_front_porch)
Definition: st7701s.h:63
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:77
int get_width() override
Definition: st7701s.h:56
esp_lcd_panel_handle_t handle_
Definition: st7701s.h:110
void set_hsync_pulse_width(uint16_t hsync_pulse_width)
Definition: st7701s.h:60
stm32_cmd_t * cmd
Definition: stm32flash.h:96
const uint8_t CMD2_BKSEL
Definition: st7701s.h:26