ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
epaper_spi_uc8151.cpp
Go to the documentation of this file.
1#include "epaper_spi_uc8151.h"
2
3#include <algorithm>
4
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7
8namespace esphome::epaper_spi {
9
10ESPHOME_LOG_TAG(TAG, "epaper_spi.uc8151");
11
12static constexpr uint8_t CMD_PSR = 0x00;
13static constexpr uint8_t CMD_PWR = 0x01;
14static constexpr uint8_t CMD_BTST = 0x06;
15static constexpr uint8_t CMD_LUT_VCOM = 0x20;
16static constexpr uint8_t CMD_PLL = 0x30;
17static constexpr uint8_t CMD_VDCS = 0x82;
18
19static constexpr uint16_t BUSY_ASSERT_MS = 100;
20
21// Partial-refresh waveform from GxEPD2_290_T5, as used by the waveshare_epaper GDEW029T5 driver.
22// The VCOM table is 44 bytes, the four pixel-transition tables (WW, BW, WB, BB, commands 0x21 to 0x24) 42 bytes.
23static constexpr size_t LUT_VCOM_SIZE = 44;
24static constexpr size_t LUT_SIZE = 42;
25static constexpr size_t LUT_COUNT = 5;
26static constexpr uint8_t LUT_PARTIAL[LUT_COUNT][LUT_VCOM_SIZE] PROGMEM = {
27 {0x00, 0x20, 0x01, 0x00, 0x00, 0x01}, // VCOM
28 {0x00, 0x20, 0x01, 0x00, 0x00, 0x01}, // WW
29 {0x80, 0x20, 0x01, 0x00, 0x00, 0x01}, // BW
30 {0x40, 0x20, 0x01, 0x00, 0x00, 0x01}, // WB
31 {0x00, 0x20, 0x01, 0x00, 0x00, 0x01}, // BB
32};
33
35 // A partial update follows one that left the controller configured, in standby, with DTM1 holding the image
36 if (this->partial_)
37 return true;
38 return EPaperBase::reset();
39}
40
42 // Settings from the waveshare_epaper GDEW029T5 driver for its register waveform
43 this->cmd_data(CMD_PWR, {0x03, 0x00, 0x2B, 0x2B, 0x03}); // VGH/VGL and VDH/VDL
44 this->cmd_data(CMD_BTST, {0x17, 0x17, 0x17}); // booster soft start
45 this->cmd_data(CMD_PSR, {0xBF, 0x0D}); // LUT from register, black/white
46 this->cmd_data(CMD_PLL, {0x3A}); // 100 Hz
47 this->cmd_data(CMD_VDCS, {0x08}); // VCOM DC
48 this->cmd_data(CMD_CDI, {0x17}); // floating border, same data polarity as full refresh
49 uint8_t lut[LUT_VCOM_SIZE];
50 for (size_t i = 0; i != LUT_COUNT; i++) {
51 const size_t size = i == 0 ? LUT_VCOM_SIZE : LUT_SIZE;
52 progmem_memcpy(lut, LUT_PARTIAL[i], size);
53 this->cmd_data(static_cast<uint8_t>(CMD_LUT_VCOM + i), lut, size);
54 }
55}
56
57bool EPaperUC8151::initialise(bool partial) {
58 if (partial) {
60 } else {
61 EPaperBase::initialise(partial); // the model init sequence sets up the OTP waveform
62 }
63 ESP_LOGV(TAG, "Power on");
64 // Power on here, so the state machine busy-waits for it before the data transfer
65 this->command(CMD_POWER_ON);
66 this->next_delay_ = BUSY_ASSERT_MS;
67 return true;
68}
69
70bool HOT EPaperUC8151::write_plane_(uint8_t command) {
71 const uint32_t start_time = millis();
72 const size_t buffer_length = this->buffer_length_;
73 if (this->current_data_index_ == 0)
74 this->command(command);
75 this->start_data_();
76 while (this->current_data_index_ != buffer_length) {
77 size_t length;
78 const uint8_t *data = this->buffer_.get_span(this->current_data_index_, length);
79 length = std::min({length, MAX_TRANSFER_SIZE, buffer_length - this->current_data_index_});
80 this->write_array(data, length);
82 if (this->current_data_index_ != buffer_length && millis() - start_time > MAX_TRANSFER_TIME) {
83 this->disable();
84 return false;
85 }
86 }
87 this->disable();
88 this->current_data_index_ = 0;
89 return true;
90}
91
93 if (this->partial_ && this->current_data_index_ == 0)
94 this->set_partial_window(0, this->width_ - 1, 0, this->height_ - 1);
95 return this->write_plane_(CMD_DTM2);
96}
97
99 // Power-on is sent at the end of initialise() instead, because the data transfer must follow it
100}
101
103 ESP_LOGV(TAG, "Refresh screen (%s)", partial ? LOG_STR_LITERAL("partial") : LOG_STR_LITERAL("full"));
104 this->command(CMD_REFRESH);
105 // The busy line takes a short time to assert after the refresh command
106 this->next_delay_ = BUSY_ASSERT_MS;
107}
108
110 // While partial updates are in use, DTM1 holds the image on the panel for the next partial comparison
111 if (!this->is_using_partial_update_())
112 return true;
113 if (this->current_data_index_ == 0 && this->partial_)
115 if (!this->write_plane_(CMD_DTM1))
116 return false;
117 // A draw during the update means DTM1 may hold pixels the panel never showed, so refresh fully next time
118 if (this->drawn_since_hash_)
119 this->update_count_ = 0;
120 return true;
121}
122
124 ESP_LOGV(TAG, "Power off");
125 this->command(CMD_POWER_OFF);
126}
127
128void EPaperUC8151::set_partial_window(uint16_t x_start, uint16_t x_end, uint16_t y_start, uint16_t y_end) {
129 this->command(CMD_PARTIAL_IN);
130 this->cmd_data(CMD_PARTIAL_WINDOW, {static_cast<uint8_t>(x_start & 0xF8), static_cast<uint8_t>(x_end | 0x07),
131 static_cast<uint8_t>(y_start >> 8), static_cast<uint8_t>(y_start & 0xFF),
132 static_cast<uint8_t>(y_end >> 8), static_cast<uint8_t>(y_end & 0xFF), 0x01});
133}
134
135} // namespace esphome::epaper_spi
void command(uint8_t value)
virtual bool initialise(bool partial)
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:229
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) override
static constexpr uint8_t CMD_PARTIAL_WINDOW
const uint8_t * get_span(size_t index, size_t &length) const
constexpr uint8_t TESTCARD_FONT[3][8] PROGMEM
Definition display.cpp:867
static void int x_start
Definition display.cpp:61
static void int int y_start
Definition display.cpp:61
ESPHOME_LOG_TAG(TAG, "display")
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint16_t size
Definition helpers.cpp:44
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t