ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
epaper_spi_jd79660.cpp
Go to the documentation of this file.
2#include "colorconv.h"
3
4#include "esphome/core/log.h"
5
6namespace esphome::epaper_spi {
7ESPHOME_LOG_TAG(TAG, "epaper_spi.jd79660");
8
10enum JD79660Color : uint8_t {
11 BLACK = 0b00,
12 WHITE = 0b01,
13 YELLOW = 0b10,
14 RED = 0b11,
15};
16
18static JD79660Color HOT color_to_hex(Color color) {
19 return color_to_bwyr(color, JD79660Color::BLACK, JD79660Color::WHITE, JD79660Color::YELLOW, JD79660Color::RED);
20}
21
23 // If clipping is active, fall back to base implementation
24 if (this->get_clipping().is_set()) {
25 EPaperBase::fill(color);
26 return;
27 }
28
29 const auto pixel_color = color_to_hex(color);
30
31 // We store 4 pixels per byte
32 this->buffer_.fill(pixel_color | (pixel_color << 2) | (pixel_color << 4) | (pixel_color << 6));
33 this->mark_all_drawn_();
34}
35
36void HOT EPaperJD79660::draw_pixel_at(int x, int y, Color color) {
37 if (!this->rotate_coordinates_(x, y))
38 return;
39 const auto pixel_bits = color_to_hex(color);
40 const uint32_t pixel_position = x + y * this->get_width_internal();
41 // We store 4 pixels per byte at LSB offsets 6, 4, 2, 0
42 const uint32_t byte_position = pixel_position / 4;
43 const uint32_t bit_offset = 6 - ((pixel_position % 4) * 2);
44 const auto original = this->buffer_[byte_position];
45
46 this->buffer_[byte_position] = (original & (~(0b11 << bit_offset))) | // mask old 2bpp
47 (pixel_bits << bit_offset); // add new 2bpp
48}
49
51 // On entry state RESET set step, next state will be RESET_END
52 if (this->state_ == EPaperState::RESET) {
54 }
55
56 switch (this->step_) {
58 // Step #0: Reset H for some settle time.
59
60 ESP_LOGVV(TAG, "reset #0");
61 this->reset_pin_->digital_write(true);
62
65 return false; // another loop: step #1 below
66
68 // Step #1: Reset L pulse for slightly >1.5ms.
69 // This is actual reset trigger.
70
71 ESP_LOGVV(TAG, "reset #1");
72
73 // As commented on SLEEP_MS_RESET1: Reset pulse must happen within time window.
74 // So do not use FSM loop, and avoid other calls/logs during pulse below.
75 pulse_reset(this->reset_pin_, {.low_ms = SLEEP_MS_RESET1});
76
79 return false; // another loop: step #2 below
80
82 // Step #2: Basically finished. Check sanity, and move FSM to INITIALISE state
83 ESP_LOGVV(TAG, "reset #2");
84
85 if (!this->is_idle_()) {
86 // Expectation: Idle after reset + settle time.
87 // Improperly connected/unexpected hardware?
88 // Error path reproducable e.g. with disconnected VDD/... pins
89 // (optimally while busy_pin configured with local pulldown).
90 // -> Mark failed to avoid followup problems.
91 this->mark_failed(LOG_STR("Busy after reset"));
92 }
93 break; // End state loop below
94
95 default:
96 // Unexpected step = bug?
97 this->mark_failed();
98 }
99
100 this->step_ = FSMState::INIT_STEP0_REGULARINIT; // reset for initialize state
101 return true;
102}
103
104bool EPaperJD79660::initialise(bool partial) {
105 switch (this->step_) {
107 // Step #0: Regular init sequence
108 ESP_LOGVV(TAG, "init #0");
109 if (!EPaperBase::initialise(partial)) { // Call parent impl
110 return false; // If parent should request another loop, do so
111 }
112
113 // Fast init requested + supported?
114 if (partial && (this->fast_update_length_ > 0)) {
116 this->wait_for_idle_(true); // Must wait for idle before fastinit sequence in next loop
117 return false; // another loop: step #1 below
118 }
119
120 break; // End state loop below
121
123 // Step #1: Fast init sequence
124 ESP_LOGVV(TAG, "init #1");
125 this->write_fastinit_();
126 break; // End state loop below
127
128 default:
129 // Unexpected step = bug?
130 this->mark_failed();
131 }
132
133 this->step_ = FSMState::NONE;
134 return true; // Finished: State transition waits for idle
135}
136
138 size_t buf_idx = 0;
139 uint8_t bytes_to_send[MAX_TRANSFER_SIZE];
140 const uint32_t start_time = App.get_loop_component_start_time();
141 const auto buffer_length = this->buffer_length_;
142 while (this->current_data_index_ != buffer_length) {
143 bytes_to_send[buf_idx++] = this->buffer_[this->current_data_index_++];
144
145 if (buf_idx == sizeof bytes_to_send) {
146 this->start_data_();
147 this->write_array(bytes_to_send, buf_idx);
148 this->disable();
149 ESP_LOGVV(TAG, "Wrote %zu bytes at %ums", buf_idx, (unsigned) millis());
150 buf_idx = 0;
151
152 if (millis() - start_time > MAX_TRANSFER_TIME) {
153 // Let the main loop run and come back next loop
154 return false;
155 }
156 }
157 }
158
159 // Finished the entire dataset
160 if (buf_idx != 0) {
161 this->start_data_();
162 this->write_array(bytes_to_send, buf_idx);
163 this->disable();
164 ESP_LOGVV(TAG, "Wrote %zu bytes at %ums", buf_idx, (unsigned) millis());
165 }
166 // Cleanup for next transfer
167 this->current_data_index_ = 0;
168
169 // Finished with all buffer chunks
170 return true;
171}
172
174 // Undocumented register sequence in vendor register range.
175 // Related to Fast Init/Update.
176 // Should likely happen after regular init seq and power on, but before refresh.
177 // Might only work for some models with certain factory MTP.
178 // Please do not change without knowledge to avoid breakage.
179
181}
182
184 // For now always send full frame buffer in chunks.
185 // JD79660 might support partial window transfers. But sample code missing.
186 // And likely minimal impact, solely on SPI transfer time into RAM.
187
188 if (this->current_data_index_ == 0) {
189 this->command(CMD_TRANSFER);
190 }
191
192 return this->transfer_buffer_chunks_();
193}
194
195void EPaperJD79660::refresh_screen([[maybe_unused]] bool partial) {
196 ESP_LOGV(TAG, "Refresh");
197 this->cmd_data(CMD_REFRESH, {(uint8_t) 0x00});
198}
199
201 ESP_LOGV(TAG, "Power off");
202 this->cmd_data(CMD_POWEROFF, {(uint8_t) 0x00});
203}
204
206 ESP_LOGV(TAG, "Deep sleep");
207 // "Deepsleep between update": Ensure EPD sleep to avoid early hardware wearout!
208 this->cmd_data(CMD_DEEPSLEEP, {(uint8_t) 0xA5});
209
210 // Notes:
211 // - VDD: Some boards (Waveshare) with "clever reset logic" would allow switching off
212 // EPD VDD by pulling reset pin low for longer time.
213 // However, a) not all boards have this, b) reliable sequence timing is difficult,
214 // c) saving is not worth it after deepsleep command above.
215 // If needed: Better option is to drive VDD via MOSFET with separate enable pin.
216 //
217 // - Possible safe shutdown:
218 // EPaperBase::on_safe_shutdown() may also trigger deep_sleep() again.
219 // Regularly, in IDLE state, this does not make sense for this "deepsleep between update" model,
220 // but SPI sequence should simply be ignored by sleeping receiver.
221 // But if triggering during lengthy update, this quick SPI sleep sequence may have benefit.
222 // Optimally, EPDs should even be set all white for longer storage.
223 // But full sequence (>15s) not possible w/o app logic.
224}
225
226} // namespace esphome::epaper_spi
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
virtual void digital_write(bool value)=0
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:826
void command(uint8_t value)
void fill(Color color) override
Definition epaper_spi.h:91
bool rotate_coordinates_(int &x, int &y)
Check and rotate coordinates based on the transform flags.
virtual bool initialise(bool partial)
void send_init_sequence_(const uint8_t *sequence, size_t length)
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:218
void cmd_data(uint8_t command, const uint8_t *ptr, size_t length)
void wait_for_idle_(bool should_wait)
static constexpr uint16_t SLEEP_MS_RESET0
Wait time (millisec) for first reset phase: High.
void deep_sleep() override
Deepsleep: Must be used to avoid hardware wearout!
bool initialise(bool partial) override
Initialise (multistep sequence)
static constexpr uint8_t CMD_REFRESH
void write_fastinit_()
Internal: Send fast init sequence via undocumented vendor registers.
@ NONE
Initial/default value: Unused.
bool transfer_data() override
Buffer transfer.
static constexpr uint8_t CMD_POWEROFF
void draw_pixel_at(int x, int y, Color color) override
Draw colored pixel into frame buffer.
static constexpr uint16_t SLEEP_MS_RESET2
Wait time (millisec) for third reset phase: High.
bool transfer_buffer_chunks_()
Internal: Send raw buffer in chunks.
static constexpr uint8_t CMD_DEEPSLEEP
static constexpr uint16_t SLEEP_MS_RESET1
Wait time (millisec) for second reset phase: Low.
void refresh_screen(bool partial) override
Refresh screen.
static constexpr uint8_t CMD_TRANSFER
FSMState step_
Counter for tracking substeps within FSM state.
bool reset() override
Reset (multistep sequence)
void fill(uint8_t value) const
Fill the entire buffer with a single byte value.
ESPHOME_LOG_TAG(TAG, "display")
constexpr NATIVE_COLOR color_to_bwyr(Color color, NATIVE_COLOR hw_black, NATIVE_COLOR hw_white, NATIVE_COLOR hw_yellow, NATIVE_COLOR hw_red)
Map RGB color to discrete BWYR hex 4 color key.
Definition colorconv.h:54
JD79660Color
Pixel color as 2bpp.
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:163
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6