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