ESPHome 2026.5.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_reset_duration(uint32_t reset_duration) { this->reset_duration_ = reset_duration; }
54 void set_transform(uint8_t transform) {
55 this->transform_ = transform;
57 }
58 void set_rotation(DisplayRotation rotation) override {
59 Display::set_rotation(rotation);
61 }
62 void set_full_update_every(uint8_t full_update_every) { this->full_update_every_ = full_update_every; }
63 void dump_config() override;
64
65 void command(uint8_t value);
66 void cmd_data(uint8_t command, const uint8_t *ptr, size_t length);
67
68 // variant with in-place initializer list
69 void cmd_data(uint8_t command, std::initializer_list<uint8_t> data) {
70 this->cmd_data(command, data.begin(), data.size());
71 }
72
73 void update() override;
74 void loop() override;
75
76 void setup() override;
77
78 void on_safe_shutdown() override;
79
80 DisplayType get_display_type() override { return this->display_type_; };
81
82 // Default implementations for monochrome displays
83 static uint8_t color_to_bit(Color color) {
84 // It's always a shade of gray. Map to BLACK or WHITE.
85 // We split the luminance at a suitable point
86 if ((color.r + color.g + color.b) >= 382) {
87 return 1;
88 }
89 return 0;
90 }
91 void fill(Color color) override {
92 // If clipping is active, fall back to base implementation
93 if (this->get_clipping().is_set()) {
94 Display::fill(color);
95 return;
96 }
97
98 auto pixel_color = color_to_bit(color) ? 0xFF : 0x00;
99
100 // We store 8 pixels per byte
101 this->buffer_.fill(pixel_color);
102 this->x_high_ = this->width_;
103 this->y_high_ = this->height_;
104 this->x_low_ = 0;
105 this->y_low_ = 0;
106 }
107
108 void clear() override {
109 // clear buffer to white, just like real paper.
110 this->fill(COLOR_ON);
111 }
112
113 int get_width() override { return this->effective_transform_ & SWAP_XY ? this->height_ : this->width_; }
114 int get_height() override { return this->effective_transform_ & SWAP_XY ? this->width_ : this->height_; }
115 void draw_pixel_at(int x, int y, Color color) override;
116
117 protected:
118 int get_height_internal() override { return this->height_; };
119 int get_width_internal() override { return this->width_; };
120 bool is_using_partial_update_() const { return this->full_update_every_ > 1; }
121 void process_state_();
122
123 const char *epaper_state_to_string_();
124 bool is_idle_() const;
125 void setup_pins_() const;
126 virtual bool reset();
127 virtual bool initialise(bool partial);
128 void send_init_sequence_(const uint8_t *sequence, size_t length);
129 void wait_for_idle_(bool should_wait);
130 bool init_buffer_(size_t buffer_length);
132 bool rotate_coordinates_(int &x, int &y);
133
141 virtual bool transfer_data() = 0;
145 virtual void refresh_screen(bool partial) = 0;
146
150 virtual void power_on() = 0;
154 virtual void power_off() = 0;
155
159 virtual void deep_sleep() = 0;
160
161 void set_state_(EPaperState state, uint16_t delay = 0);
162
163 void start_data_();
164
165 // properties initialised in the constructor
166 const char *name_;
167 uint16_t width_;
168 uint16_t row_width_; // width of a row in bytes
169 uint16_t height_;
170 const uint8_t *init_sequence_;
173
175 size_t current_data_index_{}; // used by data transfer to track progress
181 uint32_t delay_until_{}; // timestamp until which to delay processing
182 uint16_t next_delay_{}; // milliseconds to delay before next state
183 uint8_t transform_{};
185 uint8_t update_count_{};
186 // these values represent the bounds of the updated buffer. Note that x_high and y_high
187 // point to the pixel past the last one updated, i.e. may range up to width/height.
188 uint16_t x_low_{}, y_low_{}, x_high_{}, y_high_{};
189
190#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
193#endif
194#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
196#endif
197
198 // properties with specific initialisers go last
202};
203
204} // 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:58
void set_transform(uint8_t transform)
Definition epaper_spi.h:54
void fill(Color color) override
Definition epaper_spi.h:91
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:176
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:69
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:53
void set_full_update_every(uint8_t full_update_every)
Definition epaper_spi.h:62
DisplayType get_display_type() override
Definition epaper_spi.h:80
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:83
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 core.cpp:28
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