ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
encoder.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <cstdint>
4#include <cstring>
5
6// Brizlabs uses "Data over Power" LED modules that are signaled by "dipping" their 24VDC
7// supply lines per this protocol.
8// We run SPI at 100 kHz, giving 10 us time slots to pull either high (1) or low (0)
9// A 0 bit is encoded as 10, and a 1 bit is encoded as 110
10// Each individual field is sent LSB first, and we pack everything into bytes for a single SPI write.
11//
12// ┌───────────────┬──────┬───────────┐
13// │ Field │ Bits │ Slots │
14// ├───────────────┼──────┼───────────┤
15// │ LED id │ 6 │ 12–18 │
16// ├───────────────┼──────┼───────────┤
17// │ zone │ 4 │ 8–12 │
18// ├───────────────┼──────┼───────────┤
19// │ header (0xFF) │ 8 │ 24 │
20// ├───────────────┼──────┼───────────┤
21// │ R │ 8 │ 16–24 │
22// ├───────────────┼──────┼───────────┤
23// │ G │ 8 │ 16–24 │
24// ├───────────────┼──────┼───────────┤
25// │ B │ 8 │ 16–24 │
26// ├───────────────┼──────┼───────────┤
27// │ trailer │ — │ 8 │
28// └───────────────┴──────┴───────────┘
29//
30// - LED id - a seemingly-random factory ID assigned to each LED module, which technically means every LED is
31// individually addressable. However, 6 bits means there's guaranteed conflicts, and short of manually mapping every
32// module's (zone, ID) there's no real use for them. As such, we always send an LED id of 0, which effectively means
33// "broadcast". LED control is then done by...
34// - zone - Each LED module also has a zone number, which starts at 1 and increments up to 2^4 - 1 (15) as you proceed
35// down the string of lights. Once the max is reached, it loops back to 1. 0 again is "broadcast", so the whole string.
36// - header - fixed, always 0xFF
37// - R, G, B brightness where 0 = off
38// - trailer is always 11110000
39
41
42static constexpr size_t FRAME_BYTES = 17; // Worst case (all bits as 110), longest frame = 42 bits * 3 + 8 = 134 slots
43static_assert(FRAME_BYTES * 8 >= 42 * 3 + 8, "frame buffer too small for the longest frame");
44static_assert(FRAME_BYTES <= 64, "frame exceeds the SPI FIFO; transfers would need DMA");
45
46struct Writer {
47 uint8_t *out;
48 size_t n = 0;
49 void slot(uint8_t level) {
50 if (level)
51 this->out[this->n >> 3] |= 0x80 >> (this->n & 7);
52 this->n++;
53 }
54 void field(uint32_t v, int bits) {
55 for (int b = 0; b < bits; b++) {
56 this->slot(1);
57 if ((v >> b) & 1)
58 this->slot(1);
59 this->slot(0);
60 }
61 }
62};
63
64// Fills out[0..FRAME_BYTES) and returns the number of bits to transmit.
65inline size_t encode(uint8_t *out, uint8_t id, uint8_t zone, uint8_t hdr, uint8_t r, uint8_t g, uint8_t b) {
66 memset(out, 0, FRAME_BYTES);
67 Writer w{out};
68 w.field(id, 6);
69 w.field(zone, 4);
70 w.field(hdr, 8);
71 w.field(r, 8);
72 w.field(g, 8);
73 w.field(b, 8);
74 for (int k = 0; k < 4; k++)
75 w.slot(1);
76 for (int k = 0; k < 4; k++)
77 w.slot(0);
78 return w.n;
79}
80
81} // namespace esphome::brizlabs_led_string
std::vector< uint8_t > & out
Definition gif.cpp:49
size_t encode(uint8_t *out, uint8_t id, uint8_t zone, uint8_t hdr, uint8_t r, uint8_t g, uint8_t b)
Definition encoder.h:65
static void uint32_t
void field(uint32_t v, int bits)
Definition encoder.h:54