ESPHome 2026.11.0b6
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
7ESPHOME_LOG_TAG(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 this->mark_all_drawn_();
22}
23
24void HOT EPaper4bpp::draw_pixel_at(int x, int y, Color color) {
25 if (!this->rotate_coordinates_(x, y))
26 return;
27 auto pixel_bits = this->color_to_native(color);
28 uint32_t pixel_position = x + y * this->get_width_internal();
29 uint32_t byte_position = pixel_position / 2;
30 auto original = this->buffer_[byte_position];
31 if ((pixel_position & 1) != 0) {
32 this->buffer_[byte_position] = (original & 0xF0) | pixel_bits;
33 } else {
34 this->buffer_[byte_position] = (original & 0x0F) | (pixel_bits << 4);
35 }
36}
37
39 const uint32_t start_time = App.get_loop_component_start_time();
40 const size_t buffer_length = this->buffer_length_;
41 if (this->current_data_index_ == 0) {
43 }
44
45 size_t buf_idx = 0;
46 uint8_t bytes_to_send[MAX_TRANSFER_SIZE];
47 while (this->current_data_index_ != buffer_length) {
48 bytes_to_send[buf_idx++] = this->buffer_[this->current_data_index_++];
49
50 if (buf_idx == sizeof bytes_to_send) {
51 this->start_data_();
52 this->write_array(bytes_to_send, buf_idx);
53 this->disable();
54 ESP_LOGV(TAG, "Wrote %d bytes at %ums", buf_idx, (unsigned) millis());
55 buf_idx = 0;
56
57 if (millis() - start_time > MAX_TRANSFER_TIME) {
58 // Let the main loop run and come back next loop
59 return false;
60 }
61 }
62 }
63 // Finished the entire dataset
64 if (buf_idx != 0) {
65 this->start_data_();
66 this->write_array(bytes_to_send, buf_idx);
67 this->disable();
68 }
69 this->current_data_index_ = 0;
70 return true;
71}
72
73} // 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:826
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: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
void fill(uint8_t value) const
Fill the entire buffer with a single byte value.
ESPHOME_LOG_TAG(TAG, "display")
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
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