ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
epaper_spi_t133a01.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
5#include "esphome/core/log.h"
6
7namespace esphome::epaper_spi {
8
9ESPHOME_LOG_TAG(TAG, "epaper_spi.t133a01");
10
11// Color indices used in the 4bpp buffer (sprite-side)
12// These MUST match the Arduino GFX TFT_eSPI.h color definitions and
13// the remap_color()/COLOR_GET mapping:
14// 0x0F=BLACK, 0x00=WHITE, 0x02=GREEN, 0x06=RED, 0x0B=YELLOW, 0x0D=BLUE
15static constexpr uint8_t T133A01_BLACK = 0x0F;
16static constexpr uint8_t T133A01_WHITE = 0x00;
17static constexpr uint8_t T133A01_GREEN = 0x02;
18static constexpr uint8_t T133A01_RED = 0x06;
19static constexpr uint8_t T133A01_YELLOW = 0x0B;
20static constexpr uint8_t T133A01_BLUE = 0x0D;
21
22// T133A01 register addresses
23static constexpr uint8_t R00_PSR = 0x00;
24static constexpr uint8_t R01_PWR = 0x01;
25static constexpr uint8_t R02_POF = 0x02;
26static constexpr uint8_t R04_PON = 0x04;
27static constexpr uint8_t R05_BTST_N = 0x05;
28static constexpr uint8_t R06_BTST_P = 0x06;
29static constexpr uint8_t R10_DTM = 0x10;
30static constexpr uint8_t R12_DRF = 0x12;
31static constexpr uint8_t R50_CDI = 0x50;
32static constexpr uint8_t R61_TRES = 0x61;
33static constexpr uint8_t RA5_DCDC = 0xA5;
34static constexpr uint8_t RE0_CCSET = 0xE0;
35static constexpr uint8_t RE3_PWS = 0xE3;
36
43uint8_t EPaperT133A01::remap_color(uint8_t index) {
44 switch (index & 0x0F) {
45 case 0x0F:
46 return 0x00; // Black
47 case 0x00:
48 return 0x01; // White
49 case 0x02:
50 return 0x06; // Green
51 case 0x06:
52 return 0x03; // Red
53 case 0x0B:
54 return 0x02; // Yellow
55 case 0x0D:
56 return 0x05; // Blue
57 default:
58 return 0x01; // White fallback
59 }
60}
61
68 unsigned char max_rgb = std::max({color.r, color.g, color.b});
69 unsigned char min_rgb = std::min({color.r, color.g, color.b});
70
71 // Check for grayscale
72 if ((max_rgb - min_rgb) < 50) {
73 if ((static_cast<int>(color.r) + color.g + color.b) > 382) {
74 return T133A01_WHITE;
75 }
76 return T133A01_BLACK;
77 }
78
79 bool r_on = (color.r > 128);
80 bool g_on = (color.g > 128);
81 bool b_on = (color.b > 128);
82
83 if (r_on && g_on && !b_on)
84 return T133A01_YELLOW;
85 if (r_on && !g_on && !b_on)
86 return T133A01_RED;
87 if (!r_on && g_on && !b_on)
88 return T133A01_GREEN;
89 if (!r_on && !g_on && b_on)
90 return T133A01_BLUE;
91 // Handle mixed colors: map to nearest primary
92 if (!r_on && g_on && b_on)
93 return T133A01_GREEN; // Cyan -> Green
94 if (r_on && !g_on)
95 return T133A01_RED; // Magenta -> Red
96 if (r_on)
97 return T133A01_WHITE;
98 return T133A01_BLACK;
99}
100
102 // Base setup initialises the buffer, the standard pins and the SPI bus.
104
105 // Both chip-selects are driven directly by this driver (the dual-CS
106 // protocol needs CS held HIGH while CS1 receives data, which the SPI
107 // bus cannot do). Start both deselected (HIGH).
108 setup_output_pin(this->cs_pin_, true);
109 setup_output_pin(this->cs1_pin_, true);
110}
111
113 for (auto *enable_pin : this->enable_pins_) {
114 enable_pin->digital_write(true);
115 }
116 if (this->reset_pin_ != nullptr) {
117 if (this->state_ == EPaperState::RESET) {
118 this->reset_pin_->digital_write(false);
119 return false;
120 }
121 this->reset_pin_->digital_write(true);
122 }
123 return true;
124}
125
133bool EPaperT133A01::initialise(bool partial) {
134 // Init sequence mirrors the Arduino GFX library's EPD_INIT() macro
135 // (T133A01_Defines.h). Commands routed to CS only leave CS1 deselected;
136 // commands routed to both controllers assert CS and CS1 together.
137
138 // 0x74 - panel config (CS only)
139 this->write_command_(0x74, {0x00, 0x0C, 0x0C, 0xD9, 0xDD, 0xDD, 0x15, 0x15, 0x55}, true, false);
140 delay(10);
141
142 // 0xF0 - panel config (CS + CS1)
143 this->write_command_(0xF0, {0x49, 0x55, 0x13, 0x5D, 0x05, 0x10}, true, true);
144 delay(10);
145
146 // PSR - Panel Setting Register (CS + CS1)
147 this->write_command_(0x00, {0xDF, 0x69}, true, true);
148 delay(10);
149
150 // DCDC (CS only)
151 this->write_command_(RA5_DCDC, {0x44, 0x54, 0x00}, true, false);
152 delay(10);
153
154 // CDI (CS + CS1)
155 this->write_command_(R50_CDI, {0x37}, true, true);
156 delay(10);
157
158 // 0x60 (CS + CS1)
159 this->write_command_(0x60, {0x03, 0x03}, true, true);
160 delay(10);
161
162 // 0x86 (CS + CS1)
163 this->write_command_(0x86, {0x10}, true, true);
164 delay(10);
165
166 // PWS - Phase Width Setting (CS + CS1)
167 this->write_command_(RE3_PWS, {0x22}, true, true);
168 delay(10);
169
170 // TRES - Resolution Setting (CS + CS1).
171 // With width=1200, height=1600: first word = width = 1200, second word = height/2 = 800.
172 this->write_command_(R61_TRES,
173 {(uint8_t) (this->width_ >> 8), (uint8_t) (this->width_ & 0xFF),
174 (uint8_t) ((this->height_ / 2) >> 8), (uint8_t) ((this->height_ / 2) & 0xFF)},
175 true, true);
176 delay(10);
177
178 // PWR - Power Setting (CS only)
179 this->write_command_(R01_PWR, {0x0F, 0x00, 0x28, 0x2C, 0x28, 0x38}, true, false);
180 delay(10);
181
182 // 0xB6 (CS only)
183 this->write_command_(0xB6, {0x07}, true, false);
184 delay(10);
185
186 // BTST_P (CS only)
187 this->write_command_(R06_BTST_P, {0xE0, 0x20}, true, false);
188 delay(10);
189
190 // 0xB7 (CS only)
191 this->write_command_(0xB7, {0x01}, true, false);
192 delay(10);
193
194 // BTST_N (CS only)
195 this->write_command_(R05_BTST_N, {0xE0, 0x20}, true, false);
196 delay(10);
197
198 // 0xB0 (CS only)
199 this->write_command_(0xB0, {0x01}, true, false);
200 delay(10);
201
202 // 0xB1 (CS only)
203 this->write_command_(0xB1, {0x02}, true, false);
204 delay(10);
205
206 return true;
207}
208
209void EPaperT133A01::write_command_(uint8_t command, const uint8_t *data, size_t length, bool use_cs, bool use_cs1) {
210 ESP_LOGV(TAG, "Command: 0x%02X, Length: %u, CS: %d, CS1: %d", command, (unsigned) length, use_cs, use_cs1);
211 // Chip-selects are active-low: assert the requested controllers.
212 this->cs_pin_->digital_write(!use_cs);
213 this->cs1_pin_->digital_write(!use_cs1);
214 this->dc_pin_->digital_write(false);
215 this->enable();
216 this->write_byte(command);
217 if (length > 0) {
218 this->dc_pin_->digital_write(true);
219 this->write_array(data, length);
220 }
221 this->disable();
222 this->cs_pin_->digital_write(true);
223 this->cs1_pin_->digital_write(true);
224}
225
227 if (this->get_clipping().is_set()) {
228 EPaperBase::fill(color);
229 return;
230 }
231 auto pixel_color = color_to_index(color);
232 this->buffer_.fill(pixel_color + (pixel_color << 4));
233 this->mark_all_drawn_();
234}
235
236void EPaperT133A01::draw_pixel_at(int x, int y, Color color) {
237 if (!this->rotate_coordinates_(x, y))
238 return;
239 auto pixel_bits = color_to_index(color);
240 uint32_t pixel_position = x + y * this->get_width_internal();
241 uint32_t byte_position = pixel_position / 2;
242 auto original = this->buffer_[byte_position];
243 if ((pixel_position & 1) != 0) {
244 this->buffer_[byte_position] = (original & 0xF0) | pixel_bits;
245 } else {
246 this->buffer_[byte_position] = (original & 0x0F) | (pixel_bits << 4);
247 }
248}
249
251 ESP_LOGV(TAG, "Power on");
252 this->write_command_(R04_PON, true, true);
253}
254
256 ESP_LOGV(TAG, "Power off");
257 this->write_command_(R02_POF, {0x00}, true, true);
258}
259
261 ESP_LOGV(TAG, "Refresh screen");
262 // Display Refresh
263 this->write_command_(R12_DRF, {0x01}, true, true);
264}
265
267 ESP_LOGV(TAG, "Deep sleep");
268 this->write_command_(0x07, {0xA5}, true, true);
269}
270
272 const uint32_t start_time = millis();
273 const uint16_t bytes_per_half_row = this->width_ / 4;
274 const uint16_t total_rows = this->height_;
275 const uint16_t bytes_per_row = this->width_ / 2;
276 uint8_t line_data[400] = {};
277
278 size_t half = this->current_data_index_;
279
280 // --- CCSET: select color set before data transfer (CS + CS1) ---
281 if (half == 0) {
282 this->write_command_(RE0_CCSET, {0x01}, true, true);
283 this->wait_for_idle_(true);
284 delay(10);
285 }
286
287 // --- CS phase: left half of each row via CS ---
288 // T133A01 requires CS to stay LOW for the ENTIRE DTM data stream.
289 // Toggling CS between chunks resets the controller's data pointer,
290 // causing only the last chunk to be retained. Keep CS asserted
291 // across timeout boundaries by NOT deselecting on yield.
292 if (half < total_rows) {
293 if (half == 0) {
294 this->cs_pin_->digital_write(false); // select CS
295 this->cs1_pin_->digital_write(true); // deselect CS1
296 this->dc_pin_->digital_write(false);
297 this->enable();
298 this->write_byte(R10_DTM);
299 this->dc_pin_->digital_write(true);
300 }
301
302 while (half < total_rows) {
303 size_t buf_offset = half * bytes_per_row;
304 for (uint16_t col = 0; col < bytes_per_half_row; col++) {
305 uint8_t b = this->buffer_[buf_offset + col];
306 line_data[col] = (remap_color(b >> 4) << 4) | remap_color(b & 0x0F);
307 }
308 this->write_array(line_data, bytes_per_half_row);
309 half++;
310 this->current_data_index_ = half;
311
312 if (millis() - start_time > MAX_TRANSFER_TIME) {
313 break;
314 }
315 }
316 if (half < total_rows) {
317 return false;
318 }
319 ESP_LOGD(TAG, "CS phase done");
320 this->disable();
321 this->cs_pin_->digital_write(true); // deselect CS
322 }
323
324 // --- CS1 phase: right half of each row via CS1 ---
325 // Same continuous-transaction requirement as the CS phase.
326 // CS is held HIGH so only CS1 receives the data.
327 if (half >= total_rows && half < total_rows * 2) {
328 size_t cs1_row = half - total_rows;
329
330 if (cs1_row == 0) {
331 this->cs_pin_->digital_write(true); // deselect CS
332 this->cs1_pin_->digital_write(false); // select CS1
333 this->enable();
334 this->dc_pin_->digital_write(false);
335 this->write_byte(R10_DTM);
336 this->dc_pin_->digital_write(true);
337 }
338
339 while (half < total_rows * 2) {
340 size_t row = half - total_rows;
341 size_t buf_offset = row * bytes_per_row + bytes_per_half_row;
342 for (uint16_t col = 0; col < bytes_per_half_row; col++) {
343 uint8_t b = this->buffer_[buf_offset + col];
344 line_data[col] = (remap_color(b >> 4) << 4) | remap_color(b & 0x0F);
345 }
346 this->write_array(line_data, bytes_per_half_row);
347 half++;
348 this->current_data_index_ = half;
349
350 if (millis() - start_time > MAX_TRANSFER_TIME) {
351 break;
352 }
353 }
354 if (half < total_rows * 2) {
355 return false;
356 }
357 ESP_LOGD(TAG, "CS1 phase done");
358 this->disable();
359 this->cs1_pin_->digital_write(true); // deselect CS1
360 }
361
362 this->current_data_index_ = 0;
363 return true;
364}
365
367 // A data stream is open, with one of the chip selects held low, whenever transfer_data() has started sending rows
368 if (this->current_data_index_ == 0)
369 return;
370 this->disable();
371 this->cs_pin_->digital_write(true);
372 this->cs1_pin_->digital_write(true);
373}
374
377 LOG_PIN(" CS Pin: ", this->cs_pin_);
378 LOG_PIN(" CS1 Pin: ", this->cs1_pin_);
379}
380
381} // namespace esphome::epaper_spi
virtual void digital_write(bool value)=0
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:815
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.
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:229
void wait_for_idle_(bool should_wait)
std::vector< GPIOPin * > enable_pins_
Definition epaper_spi.h:241
static uint8_t remap_color(uint8_t index)
Apply COLOR_GET remap table to translate sprite indices to hardware values.
void refresh_screen(bool partial) override
static uint8_t color_to_index(Color color)
Convert Color to 4-bit T133A01 color index.
bool initialise(bool partial) override
Initialise the T133A01 display.
void draw_pixel_at(int x, int y, Color color) override
void write_command_(uint8_t command, const uint8_t *data, size_t length, bool use_cs, bool use_cs1)
Send a command (and optional data) selecting one or both controllers.
void fill(uint8_t value) const
Fill the entire buffer with a single byte value.
ESPHOME_LOG_TAG(TAG, "display")
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
Definition gpio.cpp:44
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t
uint8_t g
Definition color.h:42
uint8_t b
Definition color.h:46
uint8_t r
Definition color.h:38
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6