FastLED 3.10.10
Loading...
Searching...
No Matches
decode_ws2812.cpp.hpp
Go to the documentation of this file.
1
7
8// IWYU pragma: private
9
11
12namespace fl {
13namespace channels {
14namespace rx {
15
16namespace {
17
22int ws2812BitFromHigh(u32 high_ns, const ChipsetTiming4Phase& timing) FL_NO_EXCEPT {
23 const bool one = high_ns >= timing.t1h_min_ns && high_ns <= timing.t1h_max_ns;
24 const bool zero = high_ns >= timing.t0h_min_ns && high_ns <= timing.t0h_max_ns;
25 if (one == zero) {
26 return -1;
27 }
28 return one ? 1 : 0;
29}
30
31} // namespace
32
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;
44 u8 current_byte = 0;
45 u32 error_count = 0;
46
47 // Shifts one bit in; false when a completed byte does not fit `out`.
48 auto push_bit = [&](bool one) FL_NO_EXCEPT -> bool {
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
78 bool is_bit1 = (high_ns >= timing.t1h_min_ns && high_ns <= timing.t1h_max_ns &&
79 low_ns >= timing.t1l_min_ns && low_ns <= timing.t1l_max_ns);
80 bool is_bit0 = (high_ns >= timing.t0h_min_ns && high_ns <= timing.t0h_max_ns &&
81 low_ns >= timing.t0l_min_ns && low_ns <= timing.t0l_max_ns);
82
83 if (!is_bit0 && !is_bit1) {
84 if (low_ns >= reset_ns) {
85 // WS2812 reset pulse -> end of frame. The LOW is the last
86 // bit's LOW run into the reset, so the bit is in the HIGH.
87 const int bit = ws2812BitFromHigh(high_ns, timing);
88 if (bit >= 0 && !push_bit(bit == 1)) {
90 }
91 frame_ended = true;
92 break;
93 }
94 // #3416 RX-HIGH-5: also break on a long HIGH between frames.
95 // The FlexIO RX path observed sustained HIGH-state between
96 // frames (#3066, FlexIO TX shifter latch retention) where
97 // the line stayed high for many milliseconds. Without this
98 // branch a single stray falling edge after the long HIGH
99 // would misclassify a phantom bit cell of arbitrary
100 // duration as a real WS2812 bit.
101 if (high_ns >= reset_ns) {
102 frame_ended = true;
103 break;
104 }
105 if (timing.gap_tolerance_ns > 0 && low_ns <= timing.gap_tolerance_ns) {
106 if (high_ns >= timing.t1h_min_ns && high_ns <= timing.t1h_max_ns) {
107 is_bit1 = true;
108 } else if (high_ns >= timing.t0h_min_ns && high_ns <= timing.t0h_max_ns) {
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 // A capture that stops after a HIGH has no edge to close the last LOW.
126 if (!frame_ended && i + 1 == edge_count && edges[i].high) {
127 const int bit = ws2812BitFromHigh(edges[i].ns, timing);
128 if (bit >= 0 && !push_bit(bit == 1)) {
130 }
131 }
132
133 if (bytes_written > 0 && error_count * 10 > bytes_written * 8) {
135 }
136
137 return fl::result<u32, DecodeError>::success(static_cast<u32>(bytes_written));
138}
139
140} // namespace rx
141} // namespace channels
142} // namespace fl
static expected failure(E err, const char *msg=nullptr) FL_NO_EXCEPT
Create error result.
Definition expected.h:115
static expected success(T value) FL_NO_EXCEPT
Create successful result.
Definition expected.h:108
Shared 4-phase WS2812 edge-pair -> byte decoder (declaration).
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...
fl::result< u32, DecodeError > decodeWs2812Edges(const ChipsetTiming4Phase &timing, fl::span< const EdgeTime > edges, fl::span< u8 > out) FL_NO_EXCEPT
Decode a WS2812 edge-pair stream into bytes.
unsigned char u8
Definition stdint.h:132
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
@ HIGH_ERROR_RATE
Symbol decode error rate too high (>10%)
Definition rx.h:70
@ BUFFER_OVERFLOW
Output buffer overflow.
Definition rx.h:71
@ INVALID_ARGUMENT
Invalid input arguments.
Definition rx.h:72
Base definition for an LED controller.
4-phase RX timing thresholds for chipset detection
Definition rx.h:88
u32 ns
Duration in nanoseconds (31 bits, max ~2.1s)
Definition rx.h:35
u32 high
High/low level (1 bit: 1=high, 0=low)
Definition rx.h:36
Universal edge timing representation (platform-agnostic)
Definition rx.h:34