ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
epaper_spi_inkplate2.cpp
Go to the documentation of this file.
1// Reference: https://github.com/SolderedElectronics/Inkplate-Arduino-library (src/boards/Inkplate2)
2
4#include "colorconv.h"
5#include "esphome/core/log.h"
6
7namespace esphome::epaper_spi {
8
9static constexpr const char *const TAG = "epaper_spi.inkplate2";
10
11// Map RGB to the panel's black/white/red via the shared converter.
12enum class Inkplate2Color : uint8_t { BLACK, WHITE, RED };
13
14static Inkplate2Color to_inkplate2_color(Color color) {
16}
17
19 // Power-on (0x04) leads the init sequence, so there is nothing to do here.
20 ESP_LOGV(TAG, "Power on");
21}
22
24 ESP_LOGV(TAG, "Power off");
25 this->cmd_data(0x50, {0xF7}); // VCOM and data interval
26 this->command(0x02); // power off
27}
28
30 ESP_LOGV(TAG, "Refresh screen"); // full refresh only; partial is unused
31 // Send 0x11 then 0x12 back-to-back: 0x11 raises busy until the refresh finishes, so waiting for idle
32 // between them (as the state machine does between states) would add a ~16s stall.
33 this->cmd_data(0x11, {0x00}); // stop data transfer
34 this->command(0x12); // display refresh
35}
36
38 ESP_LOGV(TAG, "Deep sleep");
39 this->cmd_data(0x07, {0xA5});
40}
41
43 if (this->get_clipping().is_set()) {
44 EPaperBase::fill(color); // clipping active: defer to the base per-pixel path
45 return;
46 }
47
48 const size_t half_buffer = this->buffer_length_ / 2;
49
50 // Plane encoding: B/W plane 1=white, 0=black; red plane 0=red, 1=no-red.
51 uint8_t bw_byte;
52 uint8_t red_byte;
53 switch (to_inkplate2_color(color)) {
55 bw_byte = 0x00;
56 red_byte = 0xFF;
57 break;
59 bw_byte = 0xFF;
60 red_byte = 0x00;
61 break;
63 default:
64 bw_byte = 0xFF;
65 red_byte = 0xFF;
66 break;
67 }
68
69 for (size_t i = 0; i < half_buffer; i++)
70 this->buffer_[i] = bw_byte;
71 for (size_t i = half_buffer; i < this->buffer_length_; i++)
72 this->buffer_[i] = red_byte;
73
74 this->x_low_ = 0;
75 this->y_low_ = 0;
76 this->x_high_ = this->width_;
77 this->y_high_ = this->height_;
78}
79
81
82void HOT EPaperInkplate2::draw_pixel_at(int x, int y, Color color) {
83 if (!this->rotate_coordinates_(x, y))
84 return;
85
86 const size_t half_buffer = this->buffer_length_ / 2;
87 const size_t pos = y * this->row_width_ + x / 8;
88 const uint8_t mask = 0x80 >> (x & 0x07); // MSB first; see fill() for plane encoding
89
90 switch (to_inkplate2_color(color)) {
92 this->buffer_[pos] &= ~mask;
93 this->buffer_[pos + half_buffer] |= mask;
94 break;
96 this->buffer_[pos] |= mask;
97 this->buffer_[pos + half_buffer] &= ~mask;
98 break;
100 default:
101 this->buffer_[pos] |= mask;
102 this->buffer_[pos + half_buffer] |= mask;
103 break;
104 }
105}
106
108 uint8_t bytes_to_send[MAX_TRANSFER_SIZE];
109 size_t buf_idx = 0;
110 while (this->current_data_index_ < end) {
111 bytes_to_send[buf_idx++] = this->buffer_[this->current_data_index_++];
112 if (buf_idx == sizeof bytes_to_send) {
113 this->start_data_();
114 this->write_array(bytes_to_send, buf_idx);
115 this->disable();
116 buf_idx = 0;
117 if (millis() - start_time > MAX_TRANSFER_TIME)
118 return false; // yield; resume next loop
119 }
120 }
121 if (buf_idx != 0) {
122 this->start_data_();
123 this->write_array(bytes_to_send, buf_idx);
124 this->disable();
125 }
126 return true;
127}
128
130 const uint32_t start_time = millis();
131 const size_t half_buffer = this->buffer_length_ / 2;
132
133 // Black/white plane (first half) then red plane (second half).
134 if (this->current_data_index_ == 0)
135 this->command(0x10);
137 return false;
138
139 if (this->current_data_index_ == half_buffer)
140 this->command(0x13);
141 if (!this->send_buffer_range_(this->buffer_length_, start_time))
142 return false;
143
144 this->current_data_index_ = 0;
145 return true;
146}
147
148} // namespace esphome::epaper_spi
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
void command(uint8_t value)
void fill(Color color) override
Definition epaper_spi.h:92
bool rotate_coordinates_(int &x, int &y)
Check and rotate coordinates based on the transform flags.
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:177
void cmd_data(uint8_t command, const uint8_t *ptr, size_t length)
void refresh_screen(bool partial) override
void draw_pixel_at(int x, int y, Color color) override
bool send_buffer_range_(size_t end, uint32_t start_time)
constexpr Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
constexpr NATIVE_COLOR color_to_bwr(Color color, NATIVE_COLOR hw_black, NATIVE_COLOR hw_white, NATIVE_COLOR hw_red)
Map RGB color to discrete BWR (black/white/red) 3 color key.
Definition colorconv.h:80
size_t size_t pos
Definition helpers.h:1052
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6