ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
epaper_spi_mono.cpp
Go to the documentation of this file.
1#include "epaper_spi_mono.h"
2
3#include <algorithm>
4
5#include "esphome/core/log.h"
6
7namespace esphome::epaper_spi {
8static constexpr const char *const TAG = "epaper_spi.mono";
9
10void EPaperMono::refresh_screen(bool partial) {
11 ESP_LOGV(TAG, "Refresh screen");
12 this->cmd_data(0x22, {partial ? (uint8_t) 0xFF : (uint8_t) 0xF7});
13 this->command(0x20);
14}
15
17 // Deep sleep loses RAM so cannot be used with partial update
18 if (!this->is_using_partial_update_()) {
19 ESP_LOGV(TAG, "Deep sleep");
20 this->cmd_data(0x10, {0x03}); // deep sleep
21 }
22}
23
25 if (EPaperBase::reset()) {
26 this->command(0x12);
27 return true;
28 }
29 return false;
30}
31
33 // if not using partial update, the display will go into deep sleep, so must rewrite entire
34 // buffer since the display RAM will not retain contents
35 if (!this->is_using_partial_update_()) {
36 this->x_low_ = 0;
37 this->x_high_ = this->width_;
38 this->y_low_ = 0;
39 this->y_high_ = this->height_;
40 }
41 // round x-coordinates to byte boundaries
42 this->x_low_ &= ~7;
43 this->x_high_ += 7;
44 this->x_high_ &= ~7;
45 this->cmd_data(0x44, {(uint8_t) this->x_low_, (uint8_t) (this->x_low_ / 256), (uint8_t) (this->x_high_ - 1),
46 (uint8_t) ((this->x_high_ - 1) / 256)});
47 this->cmd_data(0x4E, {(uint8_t) this->x_low_, (uint8_t) (this->x_low_ / 256)});
48 this->cmd_data(0x45, {(uint8_t) this->y_low_, (uint8_t) (this->y_low_ / 256), (uint8_t) (this->y_high_ - 1),
49 (uint8_t) ((this->y_high_ - 1) / 256)});
50 this->cmd_data(0x4F, {(uint8_t) this->y_low_, (uint8_t) (this->y_low_ / 256)});
51}
52
54 auto start_time = millis();
55 if (this->current_data_index_ == 0) {
56 // round to byte boundaries
57 this->set_window();
58 // for monochrome, we still need to clear the red data buffer at least once to prevent it
59 // causing dirty pixels after partial refresh.
60 this->command(this->send_red_ ? 0x26 : 0x24);
61 this->current_data_index_ = this->y_low_; // actually current line
62 }
63 size_t row_length = (this->x_high_ - this->x_low_) / 8;
64 FixedVector<uint8_t> bytes_to_send{};
65 bytes_to_send.init(row_length);
66 ESP_LOGV(TAG, "Writing %u bytes at line %zu at %ums", row_length, this->current_data_index_, (unsigned) millis());
67 this->start_data_();
68 while (this->current_data_index_ != this->y_high_) {
69 size_t data_idx = this->current_data_index_ * this->row_width_ + this->x_low_ / 8;
70 for (size_t i = 0; i != row_length; i++) {
71 bytes_to_send[i] = this->send_red_ ? 0 : this->buffer_[data_idx++];
72 }
73 ++this->current_data_index_;
74 this->write_array(&bytes_to_send.front(), row_length); // NOLINT
75 if (millis() - start_time > MAX_TRANSFER_TIME) {
76 // Let the main loop run and come back next loop
77 this->disable();
78 return false;
79 }
80 }
81
82 this->disable();
83 this->current_data_index_ = 0;
84 if (this->send_red_) {
85 this->send_red_ = false;
86 return false;
87 }
88 return true;
89}
90
91} // namespace esphome::epaper_spi
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:534
void init(size_t n)
Definition helpers.h:624
void command(uint8_t value)
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
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28