37 const size_t edge_count = edges.size();
38 if (edge_count == 0) {
42 size_t bytes_written = 0;
49 current_byte =
static_cast<u8>((current_byte << 1) | (one ? 1u : 0u));
50 if (++bit_index < 8) {
53 if (bytes_written >= out.size()) {
56 out[bytes_written++] = current_byte;
61 const u32 reset_ns =
static_cast<u32
>(timing.reset_min_us) * 1000u;
62 bool frame_ended =
false;
65 while (i + 1 < edge_count) {
67 const EdgeTime low_edge = edges[i + 1];
69 if (!high_edge.
high || low_edge.
high) {
75 const u32 high_ns = high_edge.
ns;
76 const u32 low_ns = low_edge.
ns;
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);
83 if (!is_bit0 && !is_bit1) {
84 if (low_ns >= reset_ns) {
87 const int bit = ws2812BitFromHigh(high_ns, timing);
88 if (bit >= 0 && !push_bit(bit == 1)) {
101 if (high_ns >= reset_ns) {
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) {
108 }
else if (high_ns >= timing.t0h_min_ns && high_ns <= timing.t0h_max_ns) {
112 if (!is_bit0 && !is_bit1) {
119 if (!push_bit(is_bit1)) {
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)) {
133 if (bytes_written > 0 && error_count * 10 > bytes_written * 8) {