1use crate::pixel::{LedPixel, Rgb, Rgb16, Rgb16Order, RgbOrder, Rgbw, RgbwOrder};
2
3mod private {
6 use super::{Sk6812, Ws2811, Ws2812B, Ws2816};
7
8 pub trait ProtocolSealed {}
9
10 impl ProtocolSealed for Ws2812B {}
11 impl ProtocolSealed for Ws2811 {}
12 impl ProtocolSealed for Sk6812 {}
13 impl ProtocolSealed for Ws2816 {}
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct PulseTiming {
19 pub high_ns: u32,
21 pub low_ns: u32,
23}
24
25pub trait SingleWireProtocol<P>: private::ProtocolSealed
40where
41 P: LedPixel,
42{
43 const NAME: &'static str;
45 const DEFAULT_COLOR_ORDER: P::Order;
47 const RESET_NS: u32;
49 const ZERO: PulseTiming;
51 const ONE: PulseTiming;
53
54 const TIMING_TOLERANCE_NS: u32 = 150;
59
60 const ZERO_MIN: PulseTiming = PulseTiming {
62 high_ns: Self::ZERO.high_ns.saturating_sub(Self::TIMING_TOLERANCE_NS),
63 low_ns: Self::ZERO.low_ns.saturating_sub(Self::TIMING_TOLERANCE_NS),
64 };
65 const ZERO_MAX: PulseTiming = PulseTiming {
67 high_ns: Self::ZERO.high_ns.saturating_add(Self::TIMING_TOLERANCE_NS),
68 low_ns: Self::ZERO.low_ns.saturating_add(Self::TIMING_TOLERANCE_NS),
69 };
70 const ONE_MIN: PulseTiming = PulseTiming {
72 high_ns: Self::ONE.high_ns.saturating_sub(Self::TIMING_TOLERANCE_NS),
73 low_ns: Self::ONE.low_ns.saturating_sub(Self::TIMING_TOLERANCE_NS),
74 };
75 const ONE_MAX: PulseTiming = PulseTiming {
77 high_ns: Self::ONE.high_ns.saturating_add(Self::TIMING_TOLERANCE_NS),
78 low_ns: Self::ONE.low_ns.saturating_add(Self::TIMING_TOLERANCE_NS),
79 };
80 const MIN_BIT_PERIOD_NS: u32 = 0;
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
86pub struct Ws2812B;
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
90pub struct Ws2811;
91
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
94pub struct Sk6812;
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
102pub struct Ws2816;
103
104impl SingleWireProtocol<Rgb> for Ws2812B {
105 const NAME: &'static str = "WS2812B";
106 const DEFAULT_COLOR_ORDER: RgbOrder = RgbOrder::Grb;
107 const RESET_NS: u32 = 50_000;
108 const ZERO: PulseTiming = PulseTiming {
109 high_ns: 400,
110 low_ns: 850,
111 };
112 const ONE: PulseTiming = PulseTiming {
113 high_ns: 800,
114 low_ns: 450,
115 };
116}
117
118impl SingleWireProtocol<Rgb> for Ws2811 {
119 const NAME: &'static str = "WS2811";
120 const DEFAULT_COLOR_ORDER: RgbOrder = RgbOrder::Rgb;
121 const RESET_NS: u32 = 50_000;
122 const ZERO: PulseTiming = PulseTiming {
125 high_ns: 500,
126 low_ns: 2_000,
127 };
128 const ONE: PulseTiming = PulseTiming {
129 high_ns: 1_200,
130 low_ns: 1_300,
131 };
132}
133
134impl SingleWireProtocol<Rgbw> for Sk6812 {
135 const NAME: &'static str = "SK6812";
136 const DEFAULT_COLOR_ORDER: RgbwOrder = RgbwOrder::Grbw;
137 const RESET_NS: u32 = 80_000;
138 const ZERO: PulseTiming = PulseTiming {
139 high_ns: 300,
140 low_ns: 900,
141 };
142 const ONE: PulseTiming = PulseTiming {
143 high_ns: 600,
144 low_ns: 600,
145 };
146}
147
148impl SingleWireProtocol<Rgb16> for Ws2816 {
149 const NAME: &'static str = "WS2816";
150 const DEFAULT_COLOR_ORDER: Rgb16Order = Rgb16Order::Grb;
151 const RESET_NS: u32 = 300_000;
152 const ZERO: PulseTiming = PulseTiming {
154 high_ns: 260,
155 low_ns: 1_000,
156 };
157 const ONE: PulseTiming = PulseTiming {
158 high_ns: 660,
159 low_ns: 740,
160 };
161 const ZERO_MIN: PulseTiming = PulseTiming {
162 high_ns: 200,
163 low_ns: 800,
164 };
165 const ZERO_MAX: PulseTiming = PulseTiming {
166 high_ns: 320,
167 low_ns: 1_200,
168 };
169 const ONE_MIN: PulseTiming = PulseTiming {
170 high_ns: 520,
171 low_ns: 480,
172 };
173 const ONE_MAX: PulseTiming = PulseTiming {
174 high_ns: 800,
175 low_ns: 1_000,
176 };
177 const MIN_BIT_PERIOD_NS: u32 = 1_250;
178}