ESPHome  2025.2.0
qspi_dbi.h
Go to the documentation of this file.
1 //
2 // Created by Clyde Stubbs on 29/10/2023.
3 //
4 #pragma once
5 
6 #ifdef USE_ESP_IDF
11 
12 #include "esp_lcd_panel_rgb.h"
13 
14 namespace esphome {
15 namespace qspi_dbi {
16 
17 constexpr static const char *const TAG = "display.qspi_dbi";
18 static const uint8_t SW_RESET_CMD = 0x01;
19 static const uint8_t SLEEP_OUT = 0x11;
20 static const uint8_t NORON = 0x13;
21 static const uint8_t INVERT_OFF = 0x20;
22 static const uint8_t INVERT_ON = 0x21;
23 static const uint8_t ALL_ON = 0x23;
24 static const uint8_t WRAM = 0x24;
25 static const uint8_t MIPI = 0x26;
26 static const uint8_t DISPLAY_ON = 0x29;
27 static const uint8_t RASET = 0x2B;
28 static const uint8_t CASET = 0x2A;
29 static const uint8_t WDATA = 0x2C;
30 static const uint8_t TEON = 0x35;
31 static const uint8_t MADCTL_CMD = 0x36;
32 static const uint8_t PIXFMT = 0x3A;
33 static const uint8_t BRIGHTNESS = 0x51;
34 static const uint8_t SWIRE1 = 0x5A;
35 static const uint8_t SWIRE2 = 0x5B;
36 static const uint8_t PAGESEL = 0xFE;
37 
38 static const uint8_t MADCTL_MY = 0x80;
39 static const uint8_t MADCTL_MX = 0x40;
40 static const uint8_t MADCTL_MV = 0x20;
41 static const uint8_t MADCTL_RGB = 0x00;
42 static const uint8_t MADCTL_BGR = 0x08;
43 
44 static const uint8_t DELAY_FLAG = 0xFF;
45 // store a 16 bit value in a buffer, big endian.
46 static inline void put16_be(uint8_t *buf, uint16_t value) {
47  buf[0] = value >> 8;
48  buf[1] = value;
49 }
50 
51 enum Model {
55 };
56 
58  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
59  spi::DATA_RATE_1MHZ> {
60  public:
61  void set_model(const char *model) { this->model_ = model; }
62  void update() override;
63  void setup() override;
65  void set_color_mode(display::ColorOrder color_mode) { this->color_mode_ = color_mode; }
66 
67  void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
68  void set_enable_pin(GPIOPin *enable_pin) { this->enable_pin_ = enable_pin; }
69  void set_dimensions(uint16_t width, uint16_t height) {
70  this->width_ = width;
71  this->height_ = height;
72  }
73  void set_invert_colors(bool invert_colors) {
74  this->invert_colors_ = invert_colors;
75  this->reset_params_();
76  }
77  void set_mirror_x(bool mirror_x) {
78  this->mirror_x_ = mirror_x;
79  this->reset_params_();
80  }
81  void set_mirror_y(bool mirror_y) {
82  this->mirror_y_ = mirror_y;
83  this->reset_params_();
84  }
85  void set_swap_xy(bool swap_xy) {
86  this->swap_xy_ = swap_xy;
87  this->reset_params_();
88  }
89  void set_brightness(uint8_t brightness) {
90  this->brightness_ = brightness;
91  this->reset_params_();
92  }
93  void set_offsets(int16_t offset_x, int16_t offset_y) {
94  this->offset_x_ = offset_x;
95  this->offset_y_ = offset_y;
96  }
97 
98  void set_draw_from_origin(bool draw_from_origin) { this->draw_from_origin_ = draw_from_origin; }
100  void dump_config() override;
101 
102  int get_width_internal() override { return this->width_; }
103  int get_height_internal() override { return this->height_; }
104  bool can_proceed() override { return this->setup_complete_; }
105  void add_init_sequence(const std::vector<uint8_t> &sequence) { this->init_sequences_.push_back(sequence); }
106  void set_draw_rounding(unsigned rounding) { this->draw_rounding_ = rounding; }
107 
108  protected:
109  void check_buffer_() {
110  if (this->buffer_ == nullptr)
111  this->init_internal_(this->width_ * this->height_ * 2);
112  }
113  void write_sequence_(const std::vector<uint8_t> &vec);
114  void draw_absolute_pixel_internal(int x, int y, Color color) override;
115  void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
116  display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override;
117  void write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
118  int x_pad);
137  void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len);
138 
139  void write_command_(uint8_t cmd, uint8_t data) { this->write_command_(cmd, &data, 1); }
140  void write_command_(uint8_t cmd) { this->write_command_(cmd, &cmd, 0); }
141  void reset_params_(bool ready = false);
142  void write_init_sequence_();
143  void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
144 
145  GPIOPin *reset_pin_{nullptr};
146  GPIOPin *enable_pin_{nullptr};
147  uint16_t x_low_{1};
148  uint16_t y_low_{1};
149  uint16_t x_high_{0};
150  uint16_t y_high_{0};
152 
155  size_t width_{};
156  size_t height_{};
157  int16_t offset_x_{0};
158  int16_t offset_y_{0};
159  bool swap_xy_{};
160  bool mirror_x_{};
161  bool mirror_y_{};
162  bool draw_from_origin_{false};
163  unsigned draw_rounding_{2};
164  uint8_t brightness_{0xD0};
165  const char *model_{"Unknown"};
166  std::vector<std::vector<uint8_t>> init_sequences_{};
167 
168  esp_lcd_panel_handle_t handle_{};
169 };
170 
171 } // namespace qspi_dbi
172 } // namespace esphome
173 #endif
int get_width_internal() override
Definition: qspi_dbi.h:102
void set_brightness(uint8_t brightness)
Definition: qspi_dbi.h:89
void set_dimensions(uint16_t width, uint16_t height)
Definition: qspi_dbi.h:69
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition: qspi_dbi.cpp:119
void set_swap_xy(bool swap_xy)
Definition: qspi_dbi.h:85
uint16_t x
Definition: tt21100.cpp:17
std::vector< std::vector< uint8_t > > init_sequences_
Definition: qspi_dbi.h:166
void set_mirror_y(bool mirror_y)
Definition: qspi_dbi.h:81
void set_draw_from_origin(bool draw_from_origin)
Definition: qspi_dbi.h:98
display::ColorOrder color_mode_
Definition: qspi_dbi.h:154
void write_sequence_(const std::vector< uint8_t > &vec)
Definition: qspi_dbi.cpp:185
display::DisplayType get_display_type() override
Definition: qspi_dbi.h:99
void write_command_(uint8_t cmd, uint8_t data)
Definition: qspi_dbi.h:139
uint8_t h
Definition: bl0906.h:209
display::ColorOrder get_color_mode()
Definition: qspi_dbi.h:64
uint16_t y
Definition: tt21100.cpp:18
void set_mirror_x(bool mirror_x)
Definition: qspi_dbi.h:77
void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len)
the RM67162 in quad SPI mode seems to work like this (not in the datasheet, this is deduced from the ...
Definition: qspi_dbi.cpp:178
void init_internal_(uint32_t buffer_length)
void setup() override
Definition: qspi_dbi.cpp:8
void add_init_sequence(const std::vector< uint8_t > &sequence)
Definition: qspi_dbi.h:105
The SPIDevice is what components using the SPI will create.
Definition: spi.h:410
void dump_config() override
Definition: qspi_dbi.cpp:210
void write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset, int x_pad)
Definition: qspi_dbi.cpp:161
void set_color_mode(display::ColorOrder color_mode)
Definition: qspi_dbi.h:65
int get_height_internal() override
Definition: qspi_dbi.h:103
bool can_proceed() override
Definition: qspi_dbi.h:104
void update() override
Definition: qspi_dbi.cpp:29
esp_lcd_panel_handle_t handle_
Definition: qspi_dbi.h:168
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition: qspi_dbi.cpp:58
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: qspi_dbi.cpp:134
std::string size_t len
Definition: helpers.h:301
void reset_params_(bool ready=false)
Definition: qspi_dbi.cpp:93
void set_enable_pin(GPIOPin *enable_pin)
Definition: qspi_dbi.h:68
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
void set_draw_rounding(unsigned rounding)
Definition: qspi_dbi.h:106
void set_model(const char *model)
Definition: qspi_dbi.h:61
void set_reset_pin(GPIOPin *reset_pin)
Definition: qspi_dbi.h:67
stm32_cmd_t * cmd
Definition: stm32flash.h:96
void write_command_(uint8_t cmd)
Definition: qspi_dbi.h:140
void set_offsets(int16_t offset_x, int16_t offset_y)
Definition: qspi_dbi.h:93
void set_invert_colors(bool invert_colors)
Definition: qspi_dbi.h:73