ESPHome 2026.1.5
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) { this->transform_ = transform; }
55 void set_full_update_every(uint8_t full_update_every) { this->full_update_every_ = full_update_every; }
56 void dump_config() override;
57
58 void command(uint8_t value);
59 void cmd_data(uint8_t command, const uint8_t *ptr, size_t length);
60
61 // variant with in-place initializer list
62 void cmd_data(uint8_t command, std::initializer_list<uint8_t> data) {
63 this->cmd_data(command, data.begin(), data.size());
64 }
65
66 void update() override;
67 void loop() override;
68
69 void setup() override;
70
71 void on_safe_shutdown() override;
72
73 DisplayType get_display_type() override { return this->display_type_; };
74
75 // Default implementations for monochrome displays
76 static uint8_t color_to_bit(Color color) {
77 // It's always a shade of gray. Map to BLACK or WHITE.
78 // We split the luminance at a suitable point
79 if ((static_cast<int>(color.r) + color.g + color.b) > 512) {
80 return 1;
81 }
82 return 0;
83 }
84 void fill(Color color) override {
85 // If clipping is active, fall back to base implementation
86 if (this->get_clipping().is_set()) {
87 Display::fill(color);
88 return;
89 }
90
91 auto pixel_color = color_to_bit(color) ? 0xFF : 0x00;
92
93 // We store 8 pixels per byte
94 this->buffer_.fill(pixel_color);
95 this->x_high_ = this->width_;
96 this->y_high_ = this->height_;
97 this->x_low_ = 0;
98 this->y_low_ = 0;
99 }
100
101 void clear() override {
102 // clear buffer to white, just like real paper.
103 this->fill(COLOR_ON);
104 }
105
106 protected:
107 int get_height_internal() override { return this->height_; };
108 int get_width_internal() override { return this->width_; };
109 int get_width() override { return this->transform_ & SWAP_XY ? this->height_ : this->width_; }
110 int get_height() override { return this->transform_ & SWAP_XY ? this->width_ : this->height_; }
111 void draw_pixel_at(int x, int y, Color color) override;
112 void process_state_();
113
114 const char *epaper_state_to_string_();
115 bool is_idle_() const;
116 void setup_pins_() const;
117 virtual bool reset();
118 virtual void initialise(bool partial);
119 void wait_for_idle_(bool should_wait);
120 bool init_buffer_(size_t buffer_length);
121 bool rotate_coordinates_(int &x, int &y);
122
130 virtual bool transfer_data() = 0;
134 virtual void refresh_screen(bool partial) = 0;
135
139 virtual void power_on() = 0;
143 virtual void power_off() = 0;
144
148 virtual void deep_sleep() = 0;
149
150 void set_state_(EPaperState state, uint16_t delay = 0);
151
152 void start_data_();
153
154 // properties initialised in the constructor
155 const char *name_;
156 uint16_t width_;
157 uint16_t row_width_; // width of a row in bytes
158 uint16_t height_;
159 const uint8_t *init_sequence_;
162
164 size_t current_data_index_{}; // used by data transfer to track progress
170 uint32_t delay_until_{}; // timestamp until which to delay processing
171 uint16_t next_delay_{}; // milliseconds to delay before next state
172 uint8_t transform_{};
173 uint8_t update_count_{};
174 // these values represent the bounds of the updated buffer. Note that x_high and y_high
175 // point to the pixel past the last one updated, i.e. may range up to width/height.
176 uint16_t x_low_{}, y_low_{}, x_high_{}, y_high_{};
177
178#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
181#endif
182#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
184#endif
185
186 // properties with specific initialisers go last
188 uint32_t reset_duration_{10};
190};
191
192} // namespace esphome::epaper_spi
virtual void fill(Color color)
Fill the entire screen with the given color.
Definition display.cpp:15
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:764
virtual void power_off()=0
Power the display off.
void command(uint8_t value)
void set_transform(uint8_t transform)
Definition epaper_spi.h:54
void fill(Color color) override
Definition epaper_spi.h:84
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.
void set_dc_pin(GPIOPin *dc_pin)
Definition epaper_spi.h:49
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:165
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:62
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:55
DisplayType get_display_type() override
Definition epaper_spi.h:73
virtual void initialise(bool partial)
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:76
The SPIDevice is what components using the SPI will create.
Definition spi.h:427
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:0
const Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
Definition display.h:303
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26
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