ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
epaper_spi_ssd1677_gray4.cpp
Go to the documentation of this file.
2
3#include "esphome/core/log.h"
4
5namespace esphome::epaper_spi {
6
7ESPHOME_LOG_TAG(TAG, "epaper_spi.ssd1677_gray4");
8
9// Combine two source bytes (4 pixels each, 2 bits per pixel, most significant pixel first) into
10// one plane byte covering the same 8 pixels (1 bit per pixel), choosing high or low bit
11static uint8_t plane_byte(uint8_t first, uint8_t second, bool high_bit) {
12 uint8_t out = 0;
13 for (const uint8_t src : {first, second}) {
14 for (uint8_t shift = 6;; shift -= 2) {
15 const uint8_t level = (src >> shift) & 0x03;
16 out = (uint8_t) ((out << 1) | (high_bit ? (level >> 1) : (level & 1)));
17 if (shift == 0)
18 break;
19 }
20 }
21 return out;
22}
23
25 if (this->get_clipping().is_set()) {
26 // Falls back to the generic per-pixel implementation for the clipped rectangle.
27 EPaperBase::fill(color);
28 return;
29 }
30 const uint8_t level = color_to_gray4(color);
31 this->buffer_.fill((uint8_t) (level | (level << 2) | (level << 4) | (level << 6)));
32 this->mark_all_drawn_();
33}
34
35void HOT EPaperSSD1677Gray4::draw_pixel_at(int x, int y, Color color) {
36 if (!this->rotate_coordinates_(x, y))
37 return;
38 const uint8_t level = color_to_gray4(color);
39 const size_t byte_position = (size_t) y * this->row_width_ + x / 4;
40 const uint8_t shift = (uint8_t) (6 - 2 * (x % 4)); // most significant pixel first
41 const uint8_t original = this->buffer_[byte_position];
42 this->buffer_[byte_position] = (uint8_t) ((original & ~(0x03 << shift)) | (level << shift));
43}
44
45// A partial update reduces each pixel to its high bit: levels 2 and 3 are light, 0 and 1 dark.
46void EPaperSSD1677Gray4::plane_row(size_t y, uint8_t *out) {
47 const size_t src_row = y * this->row_width_;
48 for (size_t i = 0; i != this->plane_row_length_(); i++)
49 out[i] = plane_byte(this->buffer_[src_row + 2 * i], this->buffer_[src_row + 2 * i + 1], true);
50}
51
52// the high bit of every pixel's level goes to the new (bw) plane
53// (0x24), the low bit to the old (red) plane (0x26)
55 if (this->is_partial_push_())
57
58 auto start_time = millis();
59 const bool first_pass = this->send_red_;
60 if (this->current_data_index_ == 0) {
61 if (first_pass) {
62 // With partial updates enabled the window follows the changed area, but the four-level
63 // refresh drives every pixel from both planes, and the reset before it does not keep RAM.
64 this->x_low_ = 0;
65 this->x_high_ = this->width_;
66 this->y_low_ = 0;
67 this->y_high_ = this->height_;
68 this->set_window();
69 }
70 this->command(first_pass ? 0x24 : 0x26);
71 this->current_data_index_ = this->y_low_;
72 }
73 const size_t plane_row_length = (this->x_high_ - this->x_low_) / 8;
74 // Stack-backed for every panel width in practice; only a custom `dimensions:` far wider than any
75 // supported panel would fall back to the heap.
76 SmallBufferWithHeapFallback<128> bytes_to_send_alloc(plane_row_length);
77 uint8_t *bytes_to_send = bytes_to_send_alloc.get();
78 ESP_LOGV(TAG, "Writing %u bytes at line %zu at %ums", plane_row_length, this->current_data_index_,
79 (unsigned) millis());
80 this->start_data_();
81 while (this->current_data_index_ != this->y_high_) {
82 const size_t src_row = this->current_data_index_ * this->row_width_ + this->x_low_ / 4;
83 for (size_t i = 0; i != plane_row_length; i++)
84 bytes_to_send[i] = plane_byte(this->buffer_[src_row + 2 * i], this->buffer_[src_row + 2 * i + 1], first_pass);
85 // What the next partial update compares against: the high bits, as a black-and-white frame.
86 if (first_pass)
87 this->record_sent_(this->current_data_index_ * plane_row_length, bytes_to_send, plane_row_length);
88 // The OTP grayscale waveform treats data as inverted relative to monochrome
89 for (size_t i = 0; i != plane_row_length; i++)
90 bytes_to_send[i] = (uint8_t) ~bytes_to_send[i];
91 ++this->current_data_index_;
92 this->write_array(bytes_to_send, plane_row_length);
93 if (millis() - start_time > MAX_TRANSFER_TIME) {
94 // Let the main loop run and come back next loop
95 this->disable();
96 return false;
97 }
98 }
99
100 this->disable();
101 this->current_data_index_ = 0;
102 if (first_pass) {
103 this->send_red_ = false;
104 return false;
105 }
106 this->send_red_ = true;
107 return true;
108}
109
111 if (this->is_partial_push_()) {
112 ESP_LOGV(TAG, "Black-and-white partial refresh");
113 // The border follows the LUT selected in 0x3C. The model's setting (sent with the init
114 // sequence) picks the LUT that is white under the four-level waveform's inverted data; under
115 // the black-and-white waveform that LUT drives black and the border darkens, so use LUT1.
116 this->cmd_data(0x3C, {0x01});
118 return;
119 }
120 ESP_LOGV(TAG, "Four-level refresh");
121 this->cmd_data(0x1A, {0x67, 0x00}); // force temperature by OTP
122 this->cmd_data(0x22, {0xD7}); // four-level update sequence, panel's OTP waveform
123 this->command(0x20); // master activation
124}
125
126} // namespace esphome::epaper_spi
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:872
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.
void record_sent_(size_t index, const uint8_t *data, size_t length)
Definition epaper_spi.h:141
virtual void refresh_screen(bool partial)=0
Refresh the screen after data transfer.
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:218
void cmd_data(uint8_t command, const uint8_t *ptr, size_t length)
void plane_row(size_t y, uint8_t *out) override
void draw_pixel_at(int x, int y, Color color) override
void fill(uint8_t value) const
Fill the entire buffer with a single byte value.
uint8_t second
std::vector< uint8_t > & out
Definition gif.cpp:49
ESPHOME_LOG_TAG(TAG, "display")
constexpr uint8_t color_to_gray4(Color color)
Map RGB color to one of 4 discrete gray levels (2 bits per pixel)
Definition colorconv.h:38
const void * src
Definition hal.h:68
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6