ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
epaper_spi_uc8179.cpp
Go to the documentation of this file.
1#include "epaper_spi_uc8179.h"
2
3#include <algorithm>
4
5#include "esphome/core/log.h"
6
7namespace esphome::epaper_spi {
8
9ESPHOME_LOG_TAG(TAG, "epaper_spi.uc8179");
10
11bool EPaperUC8179::initialise(bool partial) {
12 EPaperBase::initialise(partial); // send the model init sequence
13 ESP_LOGV(TAG, "Power on");
14 // POWER ON must precede the waveform/mode registers and the data transfer
15 // (the original driver powers on and busy-waits before writing them).
16 // The state machine busy-waits before entering TRANSFER_DATA.
17 this->command(CMD_POWER_ON);
18 // Give the busy line time to assert before the state machine polls it
19 this->next_delay_ = 100;
20 return true;
21}
22
23// Set up the refresh mode. Must be called after power-on has completed.
25 if (!this->is_using_partial_update_()) {
26 return; // plain full refresh uses the mode set by the init sequence
27 }
28 // Fast and partial refresh use flipped data polarity and a floating border
29 this->cmd_data(CMD_CDI, {0xA9, 0x07});
30 // Force the waveform via the temperature registers: 0x5A selects the fast
31 // full-refresh waveform, 0x6E the partial-refresh waveform
32 this->cmd_data(CMD_CCSET, {0x02});
33 if (this->partial_) {
34 this->cmd_data(CMD_TSSET, {0x6E});
35 // Partial mode, with the window set to the full screen
36 this->set_partial_window_(0, this->width_ - 1, 0, this->height_ - 1);
37 } else {
38 this->cmd_data(CMD_TSSET, {0x5A});
39 this->command(CMD_PARTIAL_OUT); // exit partial mode
40 }
41}
42
44 const uint32_t start_time = millis();
45 const size_t buffer_length = this->buffer_length_;
46 if (this->current_data_index_ == 0) {
47 this->set_refresh_mode_();
48 }
49 // Fast full refresh sends the previous-image plane as well, so that every pixel transitions
50 const bool two_pass = this->is_using_partial_update_() && !this->partial_;
51 // Plain full refresh sends inverted data (buffer is 1=white, the wire wants 0=white);
52 // in fast/partial mode the data polarity is flipped via the VCOM/data-interval
53 // register instead, so the new-image plane is sent unmodified
54 const bool invert_new_data = !this->is_using_partial_update_();
55
56 uint8_t bytes_to_send[MAX_TRANSFER_SIZE];
57
58 // Phase 1 (fast full refresh only): previous image via 0x10 (DTM1), inverse of the new image
59 if (two_pass && this->current_data_index_ < buffer_length) {
60 if (this->current_data_index_ == 0) {
61 this->command(CMD_DTM1); // DATA START TRANSMISSION 1 (previous image)
62 }
63 this->start_data_();
64 while (this->current_data_index_ < buffer_length) {
65 const size_t bytes_to_copy = std::min(MAX_TRANSFER_SIZE, buffer_length - this->current_data_index_);
66 for (size_t i = 0; i < bytes_to_copy; i++) {
67 bytes_to_send[i] = ~this->buffer_[this->current_data_index_ + i];
68 }
69 this->write_array(bytes_to_send, bytes_to_copy);
70 this->current_data_index_ += bytes_to_copy;
71 if (millis() - start_time > MAX_TRANSFER_TIME) {
72 this->disable();
73 return false;
74 }
75 }
76 this->disable();
77 }
78
79 // Phase 2: new image via 0x13 (DTM2)
80 const size_t offset = two_pass ? buffer_length : 0;
81 const size_t total = offset + buffer_length;
82 if (this->current_data_index_ < total) {
83 if (this->current_data_index_ == offset) {
84 this->command(CMD_DTM2); // DATA START TRANSMISSION 2 (new image)
85 }
86 this->start_data_();
87 while (this->current_data_index_ < total) {
88 const size_t bytes_to_copy = std::min(MAX_TRANSFER_SIZE, total - this->current_data_index_);
89 const size_t data_idx = this->current_data_index_ - offset;
90 for (size_t i = 0; i < bytes_to_copy; i++) {
91 const uint8_t byte = this->buffer_[data_idx + i];
92 bytes_to_send[i] = invert_new_data ? ~byte : byte;
93 }
94 this->write_array(bytes_to_send, bytes_to_copy);
95 this->current_data_index_ += bytes_to_copy;
96 if (millis() - start_time > MAX_TRANSFER_TIME) {
97 this->disable();
98 return false;
99 }
100 }
101 this->disable();
102 }
103
104 this->current_data_index_ = 0;
105 return true;
106}
107
109 // Power-on is sent at the end of initialise() instead, because the
110 // waveform/mode registers and the data transfer must follow it
111}
112
113void EPaperUC8179::refresh_screen(bool /*partial*/) {
114 ESP_LOGV(TAG, "Refresh");
115 this->command(CMD_REFRESH); // DISPLAY REFRESH
116 // Delay the next busy poll: the busy line takes a short time to assert after
117 // the refresh command, and polling too early would read it as already idle
118 this->next_delay_ = 100;
119}
120
122 ESP_LOGV(TAG, "Power off");
123 this->command(CMD_POWER_OFF); // POWER OFF
124}
125
126} // namespace esphome::epaper_spi
void command(uint8_t value)
virtual bool initialise(bool partial)
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:218
void cmd_data(uint8_t command, const uint8_t *ptr, size_t length)
bool initialise(bool partial) override
void refresh_screen(bool partial) override
void set_partial_window_(uint16_t x_start, uint16_t x_end, uint16_t y_start, uint16_t y_end)
ESPHOME_LOG_TAG(TAG, "display")
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t