ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pixoo.cpp
Go to the documentation of this file.
1#include "pixoo.h"
3
4#include "esphome/core/log.h"
6
7#include <cmath>
8#include <cstring>
9#include <utility>
10
11namespace esphome::pixoo {
12
13ESPHOME_LOG_TAG(TAG, "pixoo");
14
15// Divoom LED-board packet protocol.
16static constexpr uint8_t PACKET_HEAD = 0xAA;
17static constexpr uint8_t PACKET_TAIL = 0xBB;
18static constexpr uint8_t CMD_DATA = 0x00;
19static constexpr uint8_t CMD_LIGHT = 0x01;
20static constexpr uint8_t CMD_UNUSED = 0x21;
21static constexpr uint8_t CMD_SET_RGB_IOUT = 0x22;
22static constexpr size_t PACKET_HEADER_LEN = 4; // head + len(2) + cmd
23static constexpr uint8_t DEFAULT_IOUT = 75; // per-channel LED current / white balance default
24
25// Pack a `0xAA len cmd data 0xBB` packet into buf; returns the packet length.
26static inline size_t build_packet(uint8_t *buf, uint8_t cmd, const uint8_t *data, uint16_t len) {
27 buf[0] = PACKET_HEAD;
28 put_le16(buf + 1, len);
29 buf[3] = cmd;
30 if (data != nullptr && len > 0)
31 std::memcpy(buf + PACKET_HEADER_LEN, data, len);
32 buf[PACKET_HEADER_LEN + len] = PACKET_TAIL;
33 return len + PACKET_STATIC_LEN;
34}
35
36// Fill `total` bytes at buf with a single UNUSED padding packet.
37static inline void pad_unused(uint8_t *buf, size_t total) {
38 buf[0] = PACKET_HEAD;
39 put_le16(buf + 1, total - PACKET_STATIC_LEN);
40 buf[3] = CMD_UNUSED;
41 buf[total - 1] = PACKET_TAIL;
42}
43
45
47 const size_t image_bytes = data_size(this->model_);
48 if (!this->setup_check(this->buffer_.init(image_bytes), LOG_STR(ESP_LOG_MSG_ALLOC_FAIL)))
49 return;
50
51 // Pre-build the constant DATA-packet framing; only the RGB888 payload changes per frame.
52 this->frame_buffer_[0] = PACKET_HEAD;
53 put_le16(this->frame_buffer_ + 1, image_bytes);
54 this->frame_buffer_[3] = CMD_DATA;
55 this->frame_buffer_[PACKET_HEADER_LEN + image_bytes] = PACKET_TAIL;
56 pad_unused(this->frame_buffer_ + image_bytes + PACKET_STATIC_LEN, DMA_CHUNK);
57
58 this->spi_setup();
59
60 this->buffer_.fill(0x00);
61
62 // Set the per-channel LED current. Brightness is controlled separately via the light platform.
63 const uint8_t iout[3] = {DEFAULT_IOUT, DEFAULT_IOUT, DEFAULT_IOUT};
64 this->send_command_(CMD_SET_RGB_IOUT, iout, 3);
65
66 // Frames are pushed synchronously inside update(), so there is no loop() work to do and the
67 // component is idle between updates. Marking it done (LOOP_DONE) lets LVGL's
68 // update_when_display_idle option treat the panel as idle and drive frames on demand.
69 this->disable_loop();
70}
71
72void Pixoo::send_command_(uint8_t cmd, const uint8_t *data, uint16_t len) {
73 std::memset(this->cmd_buffer_, 0, DMA_CHUNK);
74 const size_t used = build_packet(this->cmd_buffer_, cmd, data, len);
75 if (DMA_CHUNK - used >= PACKET_STATIC_LEN)
76 pad_unused(this->cmd_buffer_ + used, DMA_CHUNK - used);
77 this->enable();
78 this->write_array(this->cmd_buffer_, DMA_CHUNK);
79 this->disable();
80}
81
82void Pixoo::set_panel_brightness(float brightness) {
83 const uint8_t pct = static_cast<uint8_t>(lroundf(clamp(brightness, 0.0f, 1.0f) * 100.0f));
84 this->send_command_(CMD_LIGHT, &pct, 1);
85}
86
88 this->do_update_();
89 const size_t image_bytes = data_size(this->model_);
90 for (size_t i = 0; i < image_bytes; i++)
91 this->frame_buffer_[PACKET_HEADER_LEN + i] = this->buffer_[i];
92 this->enable();
93 this->write_array(this->frame_buffer_, frame_size(this->model_));
94 this->disable();
95}
96
97void Pixoo::set_pixel_(uint32_t index, Color color) {
98 const size_t off = static_cast<size_t>(index) * 3;
99 this->buffer_[off] = color.r;
100 this->buffer_[off + 1] = color.g;
101 this->buffer_[off + 2] = color.b;
102}
103
104void HOT Pixoo::draw_pixel_at(int x, int y, Color color) {
105 if (this->is_point_clipped(x, y))
106 return;
107 const int side = static_cast<int>(this->model_);
108 switch (this->rotation_) {
110 break;
112 std::swap(x, y);
113 x = side - x - 1;
114 break;
116 x = side - x - 1;
117 y = side - y - 1;
118 break;
120 std::swap(x, y);
121 y = side - y - 1;
122 break;
123 }
124 if (x < 0 || x >= side || y < 0 || y >= side)
125 return;
126 this->set_pixel_(static_cast<uint32_t>(y) * side + x, color);
127}
128
129// flatten keeps the RGB565 decode inlined in the pixel loop, as in Display's own per pixel loop.
130void __attribute__((flatten)) Pixoo::draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr,
131 display::ColorBitness bitness, bool big_endian, int x_offset,
132 int y_offset, int x_pad) {
133 // Fast path for the common LVGL/image blit: RGB565, no rotation, no active clipping.
134 // Anything else defers to the base implementation, which decodes per pixel and routes through
135 // draw_pixel_at() so rotation, clipping and other color formats stay correct.
137 this->is_clipping()) {
139 return;
140 }
141 const int side = static_cast<int>(this->model_);
142 const size_t line_stride = static_cast<size_t>(x_offset) + w + x_pad;
143 for (int y = 0; y != h; y++) {
144 const int dst_y = y_start + y;
145 if (dst_y < 0 || dst_y >= side)
146 continue;
147 size_t source_idx = (static_cast<size_t>(y_offset) + y) * line_stride + x_offset;
148 for (int x = 0; x != w; x++, source_idx++) {
149 const int dst_x = x_start + x;
150 if (dst_x < 0 || dst_x >= side)
151 continue;
152 this->set_pixel_(static_cast<uint32_t>(dst_y) * side + dst_x,
156 }
157 }
158}
159
160void Pixoo::fill(Color color) {
161 if (this->is_clipping()) {
163 return;
164 }
165 const size_t image_bytes = data_size(this->model_);
166 for (size_t i = 0; i < image_bytes; i += 3) {
167 this->buffer_[i] = color.r;
168 this->buffer_[i + 1] = color.g;
169 this->buffer_[i + 2] = color.b;
170 }
171}
172
174 LOG_DISPLAY("", "Divoom Pixoo", this);
175 ESP_LOGCONFIG(TAG, " Model: %ux%u", (unsigned) this->model_, (unsigned) this->model_);
176 LOG_UPDATE_INTERVAL(this);
177}
178
179} // namespace esphome::pixoo
uint8_t h
Definition bl0906.h:2
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void disable_loop()
Disable this component's loop.
static Color to_color(uint32_t colorcode, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
Unpacks a color of the given bitness, red in the most significant bits.
static ESPHOME_ALWAYS_INLINE uint32_t read_packed(const uint8_t *ptr, size_t index, bool big_endian)
Reads one packed pixel of the given bitness from a source buffer into the value to_color() takes.
int int int const uint8_t ColorOrder ColorBitness bitness
Definition display.h:394
bool is_clipping() const
Definition display.h:807
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
Definition display.h:394
virtual void fill(Color color)
Fill the entire screen with the given color.
Definition display.cpp:16
int int int const uint8_t ColorOrder ColorBitness bool int int int x_pad
Definition display.h:394
int int int const uint8_t * ptr
Definition display.h:393
bool ESPHOME_ALWAYS_INLINE is_point_clipped(int x, int y) const
Whether (x, y) falls outside the active clipping rectangle.
Definition display.h:812
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Definition display.h:394
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
Definition display.h:394
DisplayRotation rotation_
Definition display.h:865
virtual void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Draws a block for draw_pixels_at(); the parameters are the same.
Definition display.cpp:118
uint8_t cmd_buffer_[DMA_CHUNK]
Definition pixoo.h:68
void send_command_(uint8_t cmd, const uint8_t *data, uint16_t len)
Definition pixoo.cpp:72
void fill(Color color) override
Definition pixoo.cpp:160
static constexpr size_t frame_size(PixooModel model)
Definition pixoo.h:35
void setup() override
Definition pixoo.cpp:46
split_buffer::SplitBuffer buffer_
Definition pixoo.h:66
PixooModel model_
Definition pixoo.h:64
void set_panel_brightness(float brightness)
Definition pixoo.cpp:82
void set_pixel_(uint32_t index, Color color)
Definition pixoo.cpp:97
void draw_pixel_at(int x, int y, Color color) override
Definition pixoo.cpp:104
static constexpr size_t DMA_CHUNK
Definition pixoo.h:62
float get_setup_priority() const override
Definition pixoo.cpp:44
uint8_t * frame_buffer_
Definition pixoo.h:67
void update() override
Definition pixoo.cpp:87
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
void dump_config() override
Definition pixoo.cpp:173
static constexpr size_t data_size(PixooModel model)
Definition pixoo.h:32
bool init(size_t total_length, size_t max_buffer_size=SIZE_MAX)
void fill(uint8_t value) const
Fill the entire buffer with a single byte value.
struct @67::@68 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
@ DISPLAY_ROTATION_0_DEGREES
Definition display.h:137
@ DISPLAY_ROTATION_270_DEGREES
Definition display.h:140
@ DISPLAY_ROTATION_180_DEGREES
Definition display.h:139
@ DISPLAY_ROTATION_90_DEGREES
Definition display.h:138
ESPHOME_LOG_TAG(TAG, "pixoo")
constexpr float PROCESSOR
For components that use data from sensors like displays.
Definition component.h:48
const void size_t len
Definition hal.h:68
constexpr uint8_t * put_le16(uint8_t *dst, uint16_t value)
Definition helpers.h:1314
static void uint32_t
uint8_t g
Definition color.h:42
uint8_t b
Definition color.h:46
uint8_t r
Definition color.h:38
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6