Decode a WS2812 edge-pair stream into bytes.
36 {
37 const size_t edge_count = edges.
size();
38 if (edge_count == 0) {
40 }
41
42 size_t bytes_written = 0;
43 size_t bit_index = 0;
45 u32 error_count = 0;
46
47
49 current_byte =
static_cast<u8>((current_byte << 1) | (one ? 1u : 0u));
50 if (++bit_index < 8) {
51 return true;
52 }
53 if (bytes_written >= out.
size()) {
54 return false;
55 }
56 out[bytes_written++] = current_byte;
57 current_byte = 0;
58 bit_index = 0;
59 return true;
60 };
61 const u32 reset_ns =
static_cast<u32
>(timing.
reset_min_us) * 1000u;
62 bool frame_ended = false;
63
64 size_t i = 0;
65 while (i + 1 < edge_count) {
66 const EdgeTime high_edge = edges[i];
67 const EdgeTime low_edge = edges[i + 1];
68
69 if (!high_edge.high || low_edge.high) {
70 error_count++;
71 i++;
72 continue;
73 }
74
75 const u32 high_ns = high_edge.ns;
76 const u32 low_ns = low_edge.ns;
77
82
83 if (!is_bit0 && !is_bit1) {
84 if (low_ns >= reset_ns) {
85
86
88 if (bit >= 0 && !push_bit(bit == 1)) {
90 }
91 frame_ended = true;
92 break;
93 }
94
95
96
97
98
99
100
101 if (high_ns >= reset_ns) {
102 frame_ended = true;
103 break;
104 }
107 is_bit1 = true;
109 is_bit0 = true;
110 }
111 }
112 if (!is_bit0 && !is_bit1) {
113 error_count++;
114 i += 2;
115 continue;
116 }
117 }
118
119 if (!push_bit(is_bit1)) {
121 }
122 i += 2;
123 }
124
125
126 if (!frame_ended && i + 1 == edge_count && edges[i].high) {
128 if (bit >= 0 && !push_bit(bit == 1)) {
130 }
131 }
132
133 if (bytes_written > 0 && error_count * 10 > bytes_written * 8) {
135 }
136
138}
static expected failure(E err, const char *msg=nullptr) FL_NO_EXCEPT
Create error result.
static expected success(T value) FL_NO_EXCEPT
Create successful result.
constexpr fl::size size() const FL_NO_EXCEPT
int ws2812BitFromHigh(u32 high_ns, const ChipsetTiming4Phase &timing) FL_NO_EXCEPT
Bit from a HIGH alone, for the frame's last bit: its LOW runs into the reset, so it is either a reset...
u8 u8 fract8 blur_amount FL_NO_EXCEPT
@ HIGH_ERROR_RATE
Symbol decode error rate too high (>10%)
@ BUFFER_OVERFLOW
Output buffer overflow.
@ INVALID_ARGUMENT
Invalid input arguments.
u32 t1h_min_ns
Bit 1 high time minimum (e.g., 650ns)
u32 reset_min_us
Reset pulse minimum duration (e.g., 50us)
u32 gap_tolerance_ns
Maximum gap duration to tolerate (0 = no gap tolerance, treat as error)
u32 t0l_min_ns
Bit 0 low time minimum (e.g., 700ns)
u32 t1l_min_ns
Bit 1 low time minimum (e.g., 300ns)
u32 t1h_max_ns
Bit 1 high time maximum (e.g., 950ns)
u32 t0h_min_ns
Bit 0 high time minimum (e.g., 250ns)
u32 t0h_max_ns
Bit 0 high time maximum (e.g., 550ns)
u32 t1l_max_ns
Bit 1 low time maximum (e.g., 600ns)
u32 t0l_max_ns
Bit 0 low time maximum (e.g., 1000ns)