ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
epaper_spi_4bpp.cpp
Go to the documentation of this file.
1#include "epaper_spi_4bpp.h"
2
3#include "esphome/core/log.h"
4
5namespace esphome::epaper_spi {
6
7static constexpr const char *const TAG = "epaper_spi.4bpp";
8
10 // If clipping is active, fall back to base implementation
11 if (this->get_clipping().is_set()) {
12 EPaperBase::fill(color);
13 return;
14 }
15
16 auto pixel_color = this->color_to_native(color);
17
18 // We store 2 pixels per byte
19 this->buffer_.fill(pixel_color + (pixel_color << 4));
20
21 // Whole buffer just changed; mark the entire canvas dirty.
22 this->x_low_ = 0;
23 this->y_low_ = 0;
24 this->x_high_ = this->width_;
25 this->y_high_ = this->height_;
26}
27
29 // clear buffer to white, just like real paper.
30 this->fill(COLOR_ON);
31}
32
33void HOT EPaper4bpp::draw_pixel_at(int x, int y, Color color) {
34 if (!this->rotate_coordinates_(x, y))
35 return;
36 auto pixel_bits = this->color_to_native(color);
37 uint32_t pixel_position = x + y * this->get_width_internal();
38 uint32_t byte_position = pixel_position / 2;
39 auto original = this->buffer_[byte_position];
40 if ((pixel_position & 1) != 0) {
41 this->buffer_[byte_position] = (original & 0xF0) | pixel_bits;
42 } else {
43 this->buffer_[byte_position] = (original & 0x0F) | (pixel_bits << 4);
44 }
45}
46
48 const uint32_t start_time = App.get_loop_component_start_time();
49 const size_t buffer_length = this->buffer_length_;
50 if (this->current_data_index_ == 0) {
52 }
53
54 size_t buf_idx = 0;
55 uint8_t bytes_to_send[MAX_TRANSFER_SIZE];
56 while (this->current_data_index_ != buffer_length) {
57 bytes_to_send[buf_idx++] = this->buffer_[this->current_data_index_++];
58
59 if (buf_idx == sizeof bytes_to_send) {
60 this->start_data_();
61 this->write_array(bytes_to_send, buf_idx);
62 this->disable();
63 ESP_LOGV(TAG, "Wrote %d bytes at %ums", buf_idx, (unsigned) millis());
64 buf_idx = 0;
65
66 if (millis() - start_time > MAX_TRANSFER_TIME) {
67 // Let the main loop run and come back next loop
68 return false;
69 }
70 }
71 }
72 // Finished the entire dataset
73 if (buf_idx != 0) {
74 this->start_data_();
75 this->write_array(bytes_to_send, buf_idx);
76 this->disable();
77 }
78 this->current_data_index_ = 0;
79 return true;
80}
81
82} // namespace esphome::epaper_spi
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
void draw_pixel_at(int x, int y, Color color) override
virtual uint8_t color_to_native(Color color)=0
Map an RGB color to this panel's native 4-bit color key (low nibble).
void fill(Color color) override
static constexpr uint8_t CMD_TRANSFER_DATA
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 fill(uint8_t value) const
Fill the entire buffer with a single byte value.
constexpr Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6