ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
epaper_spi.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace esphome::epaper_spi {
9using namespace display;
10
11enum class EPaperState : uint8_t {
12 IDLE, // not doing anything
13 UPDATE, // update the buffer
14 RESET, // drive reset low (active)
15 RESET_END, // drive reset high (inactive)
16
17 SHOULD_WAIT, // states higher than this should wait for the display to be not busy
18 INITIALISE, // send the init sequence
19 TRANSFER_DATA, // transfer data to the display
20 POWER_ON, // power on the display
21 REFRESH_SCREEN, // send refresh command
22 POWER_OFF, // power off the display
23 DEEP_SLEEP, // deep sleep the display
24};
25
26static constexpr uint8_t NONE = 0;
27static constexpr uint8_t MIRROR_X = 1;
28static constexpr uint8_t MIRROR_Y = 2;
29static constexpr uint8_t SWAP_XY = 4;
30
31static constexpr uint32_t MAX_TRANSFER_TIME = 10; // Transfer in 10ms blocks to allow the loop to run
32static constexpr size_t MAX_TRANSFER_SIZE = 128;
33static constexpr uint8_t DELAY_FLAG = 0xFF;
34
35class EPaperBase : public Display,
36 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
37 spi::DATA_RATE_2MHZ> {
38 public:
39 EPaperBase(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence = nullptr,
40 size_t init_sequence_length = 0, DisplayType display_type = DISPLAY_TYPE_BINARY)
41 : name_(name),
42 width_(width),
43 height_(height),
44 init_sequence_(init_sequence),
45 init_sequence_length_(init_sequence_length),
46 display_type_(display_type) {
47 this->row_width_ = (this->width_ + 7) / 8; // width of a row in bytes
48 }
49 void set_dc_pin(GPIOPin *dc_pin) { dc_pin_ = dc_pin; }
50 float get_setup_priority() const override;
52 void set_busy_pin(GPIOPin *busy) { this->busy_pin_ = busy; }
53 void set_enable_pins(std::vector<GPIOPin *> enable_pins) { this->enable_pins_ = std::move(enable_pins); }
54 void set_reset_duration(uint32_t reset_duration) { this->reset_duration_ = reset_duration; }
55 void set_transform(uint8_t transform) {
56 this->transform_ = transform;
58 }
59 void set_rotation(DisplayRotation rotation) override {
60 Display::set_rotation(rotation);
62 }
63 void set_full_update_every(uint8_t full_update_every) { this->full_update_every_ = full_update_every; }
64 void dump_config() override;
65
66 void command(uint8_t value);
67 void cmd_data(uint8_t command, const uint8_t *ptr, size_t length);
68
69 // variant with in-place initializer list
70 void cmd_data(uint8_t command, std::initializer_list<uint8_t> data) {
71 this->cmd_data(command, data.begin(), data.size());
72 }
73
74 void update() override;
75 void loop() override;
76
77 void setup() override;
78
79 void on_safe_shutdown() override;
80
81 DisplayType get_display_type() override { return this->display_type_; };
82
83 // Default implementations for monochrome displays
84 static uint8_t color_to_bit(Color color) {
85 // It's always a shade of gray. Map to BLACK or WHITE.
86 // We split the luminance at a suitable point
87 if ((color.r + color.g + color.b) >= 382) {
88 return 1;
89 }
90 return 0;
91 }
92 void fill(Color color) override {
93 // If clipping is active, fall back to base implementation
94 if (this->get_clipping().is_set()) {
95 Display::fill(color);
96 return;
97 }
98
99 auto pixel_color = color_to_bit(color) ? 0xFF : 0x00;
100
101 // We store 8 pixels per byte
102 this->buffer_.fill(pixel_color);
103 this->x_high_ = this->width_;
104 this->y_high_ = this->height_;
105 this->x_low_ = 0;
106 this->y_low_ = 0;
107 }
108
109 void clear() override {
110 // clear buffer to white, just like real paper.
111 this->fill(COLOR_ON);
112 }
113
114 int get_width() override { return this->effective_transform_ & SWAP_XY ? this->height_ : this->width_; }
115 int get_height() override { return this->effective_transform_ & SWAP_XY ? this->width_ : this->height_; }
116 void draw_pixel_at(int x, int y, Color color) override;
117
118 protected:
119 int get_height_internal() override { return this->height_; };
120 int get_width_internal() override { return this->width_; };
121 bool is_using_partial_update_() const { return this->full_update_every_ > 1; }
122 void process_state_();
123
124 const char *epaper_state_to_string_();
125 bool is_idle_() const;
126 void setup_pins_() const;
127 virtual bool reset();
128 virtual bool initialise(bool partial);
129 void send_init_sequence_(const uint8_t *sequence, size_t length);
130 void wait_for_idle_(bool should_wait);
131 bool init_buffer_(size_t buffer_length);
133 bool rotate_coordinates_(int &x, int &y);
134
142 virtual bool transfer_data() = 0;
146 virtual void refresh_screen(bool partial) = 0;
147
151 virtual void power_on() = 0;
155 virtual void power_off() = 0;
156
160 virtual void deep_sleep() = 0;
161
162 void set_state_(EPaperState state, uint16_t delay = 0);
163
164 void start_data_();
165
166 // properties initialised in the constructor
167 const char *name_;
168 uint16_t width_;
169 uint16_t row_width_; // width of a row in bytes
170 uint16_t height_;
171 const uint8_t *init_sequence_;
174
176 size_t current_data_index_{}; // used by data transfer to track progress
181 std::vector<GPIOPin *> enable_pins_{};
183 uint32_t delay_until_{}; // timestamp until which to delay processing
184 uint16_t next_delay_{}; // milliseconds to delay before next state
185 uint8_t transform_{};
187 uint8_t update_count_{};
188 // these values represent the bounds of the updated buffer. Note that x_high and y_high
189 // point to the pixel past the last one updated, i.e. may range up to width/height.
190 uint16_t x_low_{}, y_low_{}, x_high_{}, y_high_{};
191
192#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
195#endif
196#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
198#endif
199
200 // properties with specific initialisers go last
204};
205
206} // namespace esphome::epaper_spi
virtual void fill(Color color)
Fill the entire screen with the given color.
Definition display.cpp:14
virtual void set_rotation(DisplayRotation rotation)
Internal method to set the display rotation with.
Definition display.cpp:16
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
virtual void power_off()=0
Power the display off.
void command(uint8_t value)
void set_rotation(DisplayRotation rotation) override
Definition epaper_spi.h:59
void set_enable_pins(std::vector< GPIOPin * > enable_pins)
Definition epaper_spi.h:53
void set_transform(uint8_t transform)
Definition epaper_spi.h:55
void fill(Color color) override
Definition epaper_spi.h:92
void process_state_()
Process the state machine.
virtual void deep_sleep()=0
Place the display into deep sleep.
void draw_pixel_at(int x, int y, Color color) override
Default implementation for monochrome displays where 8 pixels are packed to a byte.
bool rotate_coordinates_(int &x, int &y)
Check and rotate coordinates based on the transform flags.
void loop() override
Called during the loop task.
virtual void refresh_screen(bool partial)=0
Refresh the screen after data transfer.
virtual bool initialise(bool partial)
void set_dc_pin(GPIOPin *dc_pin)
Definition epaper_spi.h:49
void send_init_sequence_(const uint8_t *sequence, size_t length)
void set_reset_pin(GPIOPin *reset)
Definition epaper_spi.h:51
virtual bool transfer_data()=0
Methods that must be implemented by concrete classes to control the display.
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:177
void cmd_data(uint8_t command, const uint8_t *ptr, size_t length)
void cmd_data(uint8_t command, std::initializer_list< uint8_t > data)
Definition epaper_spi.h:70
void set_state_(EPaperState state, uint16_t delay=0)
const char * epaper_state_to_string_()
EPaperBase(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence=nullptr, size_t init_sequence_length=0, DisplayType display_type=DISPLAY_TYPE_BINARY)
Definition epaper_spi.h:39
virtual void power_on()=0
Power the display on.
void set_reset_duration(uint32_t reset_duration)
Definition epaper_spi.h:54
void set_full_update_every(uint8_t full_update_every)
Definition epaper_spi.h:63
DisplayType get_display_type() override
Definition epaper_spi.h:81
void wait_for_idle_(bool should_wait)
float get_setup_priority() const override
bool init_buffer_(size_t buffer_length)
void set_busy_pin(GPIOPin *busy)
Definition epaper_spi.h:52
static uint8_t color_to_bit(Color color)
Definition epaper_spi.h:84
std::vector< GPIOPin * > enable_pins_
Definition epaper_spi.h:181
The SPIDevice is what components using the SPI will create.
Definition spi.h:429
A SplitBuffer allocates a large memory buffer potentially as multiple smaller buffers to facilitate a...
void fill(uint8_t value) const
Fill the entire buffer with a single byte value.
bool state
Definition fan.h:2
constexpr Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint8_t g
Definition color.h:34
uint8_t b
Definition color.h:38
uint8_t r
Definition color.h:30
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6