ESPHome 2026.12.0-dev
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// the high bit of every pixel's level goes to the new (bw) plane
10// (0x24), the low bit to the old (red) plane (0x26)
12 if (this->is_partial_refresh_())
14 // Completing this transfer makes the previous frame whole, so refresh_screen() must not see a partial update
15 this->partial_ = false;
16
17 auto start_time = millis();
18 const bool first_pass = this->send_red_;
19 if (this->current_data_index_ == 0) {
20 if (first_pass) {
21 // With partial updates enabled the window follows the changed area, but the four-level
22 // refresh drives every pixel from both planes, and the reset before it does not keep RAM.
23 this->x_low_ = 0;
24 this->x_high_ = this->width_;
25 this->y_low_ = 0;
26 this->y_high_ = this->height_;
27 this->set_window();
28 }
29 this->command(first_pass ? 0x24 : 0x26);
30 this->current_data_index_ = this->y_low_;
31 }
32 const size_t plane_row_length = (this->x_high_ - this->x_low_) / 8;
33 // Stack-backed for every panel width in practice; only a custom `dimensions:` far wider than any
34 // supported panel would fall back to the heap.
35 SmallBufferWithHeapFallback<128> bytes_to_send_alloc(plane_row_length);
36 uint8_t *bytes_to_send = bytes_to_send_alloc.get();
37 ESP_LOGV(TAG, "Writing %u bytes at line %zu at %ums", plane_row_length, this->current_data_index_,
38 (unsigned) millis());
39 this->start_data_();
40 while (this->current_data_index_ != this->y_high_) {
41 const size_t src_row = this->current_data_index_ * this->row_width_ + this->x_low_ / 4;
42 for (size_t i = 0; i != plane_row_length; i++) {
43 bytes_to_send[i] =
44 gray4_plane_byte(this->buffer_[src_row + 2 * i], this->buffer_[src_row + 2 * i + 1], first_pass);
45 }
46 // What the next partial update compares against: the high bits, as a black-and-white frame.
47 if (first_pass)
48 this->record_sent_(this->current_data_index_ * plane_row_length, bytes_to_send, plane_row_length);
49 // The OTP grayscale waveform treats data as inverted relative to monochrome
50 for (size_t i = 0; i != plane_row_length; i++)
51 bytes_to_send[i] = (uint8_t) ~bytes_to_send[i];
52 ++this->current_data_index_;
53 this->write_array(bytes_to_send, plane_row_length);
54 if (millis() - start_time > MAX_TRANSFER_TIME) {
55 // Let the main loop run and come back next loop
56 this->disable();
57 return false;
58 }
59 }
60
61 this->disable();
62 this->current_data_index_ = 0;
63 if (first_pass) {
64 this->send_red_ = false;
65 return false;
66 }
67 this->send_red_ = true;
68 return true;
69}
70
72 if (this->is_partial_refresh_()) {
73 ESP_LOGV(TAG, "Black-and-white partial refresh");
74 // The border follows the LUT selected in 0x3C. The model's setting (sent with the init
75 // sequence) picks the LUT that is white under the four-level waveform's inverted data; under
76 // the black-and-white waveform that LUT drives black and the border darkens, so use LUT1.
77 this->cmd_data(0x3C, {0x01});
79 return;
80 }
81 ESP_LOGV(TAG, "Four-level refresh");
82 this->cmd_data(0x1A, {0x67, 0x00}); // force temperature by OTP
83 this->cmd_data(0x22, {0xD7}); // four-level update sequence, panel's OTP waveform
84 this->command(0x20); // master activation
85}
86
87} // namespace esphome::epaper_spi
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:1019
void command(uint8_t value)
void record_sent_(size_t index, const uint8_t *data, size_t length)
Definition epaper_spi.h:140
virtual void refresh_screen(bool partial)=0
Refresh the screen after data transfer.
split_buffer::SplitBuffer buffer_
Definition epaper_spi.h:229
void cmd_data(uint8_t command, const uint8_t *ptr, size_t length)
ESPHOME_LOG_TAG(TAG, "display")
uint8_t gray4_plane_byte(uint8_t first, uint8_t second, bool high_bit)
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25