ESPHome 2025.5.0
Loading...
Searching...
No Matches
mipi_spi.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
9
10namespace esphome {
11namespace mipi_spi {
12
13constexpr static const char *const TAG = "display.mipi_spi";
14static const uint8_t SW_RESET_CMD = 0x01;
15static const uint8_t SLEEP_OUT = 0x11;
16static const uint8_t NORON = 0x13;
17static const uint8_t INVERT_OFF = 0x20;
18static const uint8_t INVERT_ON = 0x21;
19static const uint8_t ALL_ON = 0x23;
20static const uint8_t WRAM = 0x24;
21static const uint8_t MIPI = 0x26;
22static const uint8_t DISPLAY_ON = 0x29;
23static const uint8_t RASET = 0x2B;
24static const uint8_t CASET = 0x2A;
25static const uint8_t WDATA = 0x2C;
26static const uint8_t TEON = 0x35;
27static const uint8_t MADCTL_CMD = 0x36;
28static const uint8_t PIXFMT = 0x3A;
29static const uint8_t BRIGHTNESS = 0x51;
30static const uint8_t SWIRE1 = 0x5A;
31static const uint8_t SWIRE2 = 0x5B;
32static const uint8_t PAGESEL = 0xFE;
33
34static const uint8_t MADCTL_MY = 0x80; // Bit 7 Bottom to top
35static const uint8_t MADCTL_MX = 0x40; // Bit 6 Right to left
36static const uint8_t MADCTL_MV = 0x20; // Bit 5 Swap axes
37static const uint8_t MADCTL_RGB = 0x00; // Bit 3 Red-Green-Blue pixel order
38static const uint8_t MADCTL_BGR = 0x08; // Bit 3 Blue-Green-Red pixel order
39static const uint8_t MADCTL_XFLIP = 0x02; // Mirror the display horizontally
40static const uint8_t MADCTL_YFLIP = 0x01; // Mirror the display vertically
41
42static const uint8_t DELAY_FLAG = 0xFF;
43// store a 16 bit value in a buffer, big endian.
44static inline void put16_be(uint8_t *buf, uint16_t value) {
45 buf[0] = value >> 8;
46 buf[1] = value;
47}
48
53
55 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
56 spi::DATA_RATE_1MHZ> {
57 public:
58 MipiSpi(size_t width, size_t height, int16_t offset_width, int16_t offset_height, display::ColorBitness color_depth)
59 : width_(width),
60 height_(height),
61 offset_width_(offset_width),
62 offset_height_(offset_height),
63 color_depth_(color_depth) {}
64 void set_model(const char *model) { this->model_ = model; }
65 void update() override;
66 void setup() override;
70
71 void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
72 void set_enable_pins(std::vector<GPIOPin *> enable_pins) { this->enable_pins_ = std::move(enable_pins); }
73 void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
74 void set_invert_colors(bool invert_colors) {
75 this->invert_colors_ = invert_colors;
76 this->reset_params_();
77 }
78 void set_brightness(uint8_t brightness) {
79 this->brightness_ = brightness;
80 this->reset_params_();
81 }
82
83 void set_draw_from_origin(bool draw_from_origin) { this->draw_from_origin_ = draw_from_origin; }
85 void dump_config() override;
86
87 int get_width_internal() override { return this->width_; }
88 int get_height_internal() override { return this->height_; }
89 bool can_proceed() override { return this->setup_complete_; }
90 void set_init_sequence(const std::vector<uint8_t> &sequence) { this->init_sequence_ = sequence; }
91 void set_draw_rounding(unsigned rounding) { this->draw_rounding_ = rounding; }
92 void set_spi_16(bool spi_16) { this->spi_16_ = spi_16; }
93
94 protected:
96 if (this->is_failed())
97 return false;
98 if (this->buffer_ != nullptr)
99 return true;
100 auto bytes_per_pixel = this->color_depth_ == display::COLOR_BITNESS_565 ? 2 : 1;
101 this->init_internal_(this->width_ * this->height_ * bytes_per_pixel);
102 if (this->buffer_ == nullptr) {
103 this->mark_failed();
104 return false;
105 }
106 this->buffer_bytes_ = this->width_ * this->height_ * bytes_per_pixel;
107 return true;
108 }
109 void fill(Color color) override;
110 void draw_absolute_pixel_internal(int x, int y, Color color) override;
111 void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorOrder order,
112 display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override;
113 void write_18_from_16_bit_(const uint16_t *ptr, size_t w, size_t h, size_t stride);
114 void write_18_from_8_bit_(const uint8_t *ptr, size_t w, size_t h, size_t stride);
115 void write_16_from_8_bit_(const uint8_t *ptr, size_t w, size_t h, size_t stride);
116 void write_to_display_(int x_start, int y_start, int w, int h, const uint8_t *ptr, int x_offset, int y_offset,
117 int x_pad);
136 void write_command_(uint8_t cmd, const uint8_t *bytes, size_t len);
137
138 void write_command_(uint8_t cmd, uint8_t data) { this->write_command_(cmd, &data, 1); }
139 void write_command_(uint8_t cmd) { this->write_command_(cmd, &cmd, 0); }
140 void reset_params_();
142 void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
143
145 std::vector<GPIOPin *> enable_pins_{};
146 GPIOPin *dc_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
154 size_t width_;
155 size_t height_;
158 size_t buffer_bytes_{0};
161 uint8_t bus_width_{};
162 bool spi_16_{};
163 uint8_t madctl_{};
164 bool draw_from_origin_{false};
165 unsigned draw_rounding_{2};
167 const char *model_{"Unknown"};
168 std::vector<uint8_t> init_sequence_{};
169};
170} // namespace mipi_spi
171} // namespace esphome
uint8_t h
Definition bl0906.h:2
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void init_internal_(uint32_t buffer_length)
void set_model(const char *model)
Definition mipi_spi.h:64
void dump_config() override
Definition mipi_spi.cpp:449
void set_init_sequence(const std::vector< uint8_t > &sequence)
Definition mipi_spi.h:90
MipiSpi(size_t width, size_t height, int16_t offset_width, int16_t offset_height, display::ColorBitness color_depth)
Definition mipi_spi.h:58
bool can_proceed() override
Definition mipi_spi.h:89
void set_brightness(uint8_t brightness)
Definition mipi_spi.h:78
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition mipi_spi.h:72
std::vector< uint8_t > init_sequence_
Definition mipi_spi.h:168
void set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition mipi_spi.cpp:229
display::ColorOrder get_color_mode()
Definition mipi_spi.h:67
int get_width_internal() override
Definition mipi_spi.h:87
void set_reset_pin(GPIOPin *reset_pin)
Definition mipi_spi.h:71
void set_dc_pin(GPIOPin *dc_pin)
Definition mipi_spi.h:73
void write_18_from_16_bit_(const uint16_t *ptr, size_t w, size_t h, size_t stride)
Definition mipi_spi.cpp:271
int get_height_internal() override
Definition mipi_spi.h:88
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 mipi_spi.cpp:334
void set_draw_rounding(unsigned rounding)
Definition mipi_spi.h:91
optional< uint8_t > brightness_
Definition mipi_spi.h:166
void set_spi_16(bool spi_16)
Definition mipi_spi.h:92
void draw_absolute_pixel_internal(int x, int y, Color color) override
Definition mipi_spi.cpp:151
display::ColorBitness color_depth_
Definition mipi_spi.h:159
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 mipi_spi.cpp:409
std::vector< GPIOPin * > enable_pins_
Definition mipi_spi.h:145
void write_18_from_8_bit_(const uint8_t *ptr, size_t w, size_t h, size_t stride)
Definition mipi_spi.cpp:293
void write_16_from_8_bit_(const uint8_t *ptr, size_t w, size_t h, size_t stride)
Definition mipi_spi.cpp:314
void write_command_(uint8_t cmd)
Definition mipi_spi.h:139
display::DisplayType get_display_type() override
Definition mipi_spi.h:84
void write_command_(uint8_t cmd, uint8_t data)
Definition mipi_spi.h:138
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 mipi_spi.cpp:244
void set_invert_colors(bool invert_colors)
Definition mipi_spi.h:74
void update() override
Definition mipi_spi.cpp:92
void fill(Color color) override
Definition mipi_spi.cpp:122
void setup() override
Definition mipi_spi.cpp:7
void set_draw_from_origin(bool draw_from_origin)
Definition mipi_spi.h:83
The SPIDevice is what components using the SPI will create.
Definition spi.h:421
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:301
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6