ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
epaper_spi_gray4.h
Go to the documentation of this file.
1#pragma once
2
3#include "colorconv.h"
4#include "epaper_spi.h"
5
6namespace esphome::epaper_spi {
7
8// Combine two source bytes (4 pixels each, 2 bits per pixel, most significant pixel first) into
9// one plane byte covering the same 8 pixels (1 bit per pixel), choosing high or low bit
10inline uint8_t gray4_plane_byte(uint8_t first, uint8_t second, bool high_bit) {
11 uint8_t out = 0;
12 for (const uint8_t src : {first, second}) {
13 for (uint8_t shift = 6;; shift -= 2) {
14 const uint8_t level = (src >> shift) & 0x03;
15 out = (uint8_t) ((out << 1) | (high_bit ? (level >> 1) : (level & 1)));
16 if (shift == 0)
17 break;
18 }
19 }
20 return out;
21}
22
27template<typename Base> class EPaperGray4 : public Base {
28 public:
29 EPaperGray4(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
30 size_t init_sequence_length)
31 : Base(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_GRAYSCALE, 2) {}
32
33 void fill(Color color) override {
34 if (this->get_clipping().is_set()) {
35 // Falls back to the generic per-pixel implementation for the clipped rectangle.
36 Base::fill(color);
37 return;
38 }
39 const uint8_t level = color_to_gray4(color);
40 this->buffer_.fill((uint8_t) (level | (level << 2) | (level << 4) | (level << 6)));
41 this->mark_all_drawn_();
42 }
43
44 void HOT draw_pixel_at(int x, int y, Color color) override {
45 if (!this->rotate_coordinates_(x, y))
46 return;
47 const uint8_t level = color_to_gray4(color);
48 const size_t byte_position = (size_t) y * this->row_width_ + x / 4;
49 const uint8_t shift = (uint8_t) (6 - 2 * (x % 4));
50 const uint8_t original = this->buffer_[byte_position];
51 this->buffer_[byte_position] = (uint8_t) ((original & ~(0x03 << shift)) | (level << shift));
52 }
53
54 protected:
55 // A partial update reduces each pixel to its high bit: levels 2 and 3 are light, 0 and 1 dark.
56 void mono_row(size_t y, uint8_t *out) override {
57 const size_t src_row = y * this->row_width_;
58 for (size_t i = 0; i != this->mono_row_length_(); i++)
59 out[i] = gray4_plane_byte(this->buffer_[src_row + 2 * i], this->buffer_[src_row + 2 * i + 1], true);
60 }
61};
62
63} // namespace esphome::epaper_spi
Adds a framebuffer of four gray levels to a driver: 2 bits per pixel, 4 per byte, most significant pi...
EPaperGray4(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence, size_t init_sequence_length)
void mono_row(size_t y, uint8_t *out) override
void fill(Color color) override
void HOT draw_pixel_at(int x, int y, Color color) override
uint8_t second
std::vector< uint8_t > & out
Definition gif.cpp:49
uint8_t gray4_plane_byte(uint8_t first, uint8_t second, bool high_bit)
constexpr uint8_t color_to_gray4(Color color)
Map RGB color to one of 4 discrete gray levels (2 bits per pixel)
Definition colorconv.h:38
const void * src
Definition hal.h:68
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6