ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
epaper_spi_bwr.cpp
Go to the documentation of this file.
1#include "epaper_spi_bwr.h"
2
3#include <algorithm>
4#include <cstring>
5
6#include "colorconv.h"
7
8namespace esphome::epaper_spi {
9
10namespace {
11// Whether a pixel of this color sets its bit in the black/white plane
12bool sets_bw_bit(BwrColor bwr, bool red_sets_black) {
13 return bwr == BwrColor::BWR_COLOR_BLACK || (bwr == BwrColor::BWR_COLOR_RED && red_sets_black);
14}
15} // namespace
16
17void HOT EPaperBWR::draw_pixel_at(int x, int y, Color color) {
18 if (!this->rotate_coordinates_(x, y))
19 return;
20
21 const uint32_t pos = (x / 8) + (y * this->row_width_);
22 const uint8_t bit = 0x80 >> (x & 0x07);
23 const uint32_t red_offset = this->buffer_length_ / 2u;
24
25 const auto bwr =
27
28 assign_bits(this->buffer_[pos], bit, sets_bw_bit(bwr, this->red_sets_black_));
29 assign_bits(this->buffer_[red_offset + pos], bit, bwr == BwrColor::BWR_COLOR_RED);
30}
31
33 if (this->get_clipping().is_set()) {
34 EPaperBase::fill(color);
35 return;
36 }
37
38 const size_t half_buffer = this->buffer_length_ / 2u;
39 const auto bwr =
41
42 this->fill_range_(0, half_buffer, sets_bw_bit(bwr, this->red_sets_black_) ? 0xFF : 0x00);
43 this->fill_range_(half_buffer, this->buffer_length_, bwr == BwrColor::BWR_COLOR_RED ? 0xFF : 0x00);
44
45 this->mark_all_drawn_();
46}
47
48void EPaperBWR::fill_range_(size_t index, size_t end, uint8_t value) {
49 while (index != end) {
50 size_t length;
51 uint8_t *data = this->buffer_.get_span(index, length);
52 length = std::min(length, end - index);
53 memset(data, value, length);
54 index += length;
55 }
56}
57
58bool HOT EPaperBWR::send_buffer_range_(size_t end, uint8_t invert_mask, uint32_t start_time) {
59 uint8_t bytes_to_send[MAX_TRANSFER_SIZE];
60 this->start_data_();
61 while (this->current_data_index_ < end) {
62 const size_t chunk = std::min(MAX_TRANSFER_SIZE, end - this->current_data_index_);
63 // The chunk may cross from one SplitBuffer part to the next, so copy it a span at a time
64 for (size_t filled = 0; filled != chunk;) {
65 size_t length;
66 const uint8_t *data = this->buffer_.get_span(this->current_data_index_ + filled, length);
67 length = std::min(length, chunk - filled);
68 for (size_t i = 0; i != length; i++)
69 bytes_to_send[filled + i] = data[i] ^ invert_mask;
70 filled += length;
71 }
72 this->write_array(bytes_to_send, chunk);
73 this->current_data_index_ += chunk;
74 if (this->current_data_index_ < end && millis() - start_time > MAX_TRANSFER_TIME) {
75 this->disable();
76 return false; // yield; resume next loop
77 }
78 }
79 this->disable();
80 return true;
81}
82
84 const uint32_t start_time = millis();
85 const size_t half_buffer = this->buffer_length_ / 2u;
86
87 // Black/white plane (first half, always inverted) then red plane (second half, inverted if invert_red_).
88 if (this->current_data_index_ == 0)
89 this->command(0x10);
90 if (this->current_data_index_ < half_buffer && !this->send_buffer_range_(half_buffer, 0xFF, start_time))
91 return false;
92
93 if (this->current_data_index_ == half_buffer)
94 this->command(0x13);
95 if (!this->send_buffer_range_(this->buffer_length_, this->invert_red_ ? 0xFF : 0x00, start_time))
96 return false;
97
98 this->current_data_index_ = 0;
99 return true;
100}
101
102} // namespace esphome::epaper_spi
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:826
void draw_pixel_at(int x, int y, Color color) override
void fill(Color color) override
void fill_range_(size_t index, size_t end, uint8_t value)
bool send_buffer_range_(size_t end, uint8_t invert_mask, uint32_t start_time)
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:218
const uint8_t * get_span(size_t index, size_t &length) const
BwrColor
Native color keys for black/white/red panels, for use with color_to_bwr.
Definition colorconv.h:27
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:99
uint32_t value
Definition hal.cpp:41
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150
size_t size_t pos
Definition helpers.h:1333
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
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