use crate::pixel::{LedPixel, Rgb, Rgb16, Rgb16Order, RgbOrder, Rgbw, RgbwOrder};
mod private {
use super::{Sk6812, Ws2811, Ws2812B, Ws2816};
pub trait ProtocolSealed {}
impl ProtocolSealed for Ws2812B {}
impl ProtocolSealed for Ws2811 {}
impl ProtocolSealed for Sk6812 {}
impl ProtocolSealed for Ws2816 {}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PulseTiming {
pub high_ns: u32,
pub low_ns: u32,
}
pub trait SingleWireProtocol<P>: private::ProtocolSealed
where
P: LedPixel,
{
const NAME: &'static str;
const DEFAULT_COLOR_ORDER: P::Order;
const RESET_NS: u32;
const ZERO: PulseTiming;
const ONE: PulseTiming;
const TIMING_TOLERANCE_NS: u32 = 150;
const ZERO_MIN: PulseTiming = PulseTiming {
high_ns: Self::ZERO.high_ns.saturating_sub(Self::TIMING_TOLERANCE_NS),
low_ns: Self::ZERO.low_ns.saturating_sub(Self::TIMING_TOLERANCE_NS),
};
const ZERO_MAX: PulseTiming = PulseTiming {
high_ns: Self::ZERO.high_ns.saturating_add(Self::TIMING_TOLERANCE_NS),
low_ns: Self::ZERO.low_ns.saturating_add(Self::TIMING_TOLERANCE_NS),
};
const ONE_MIN: PulseTiming = PulseTiming {
high_ns: Self::ONE.high_ns.saturating_sub(Self::TIMING_TOLERANCE_NS),
low_ns: Self::ONE.low_ns.saturating_sub(Self::TIMING_TOLERANCE_NS),
};
const ONE_MAX: PulseTiming = PulseTiming {
high_ns: Self::ONE.high_ns.saturating_add(Self::TIMING_TOLERANCE_NS),
low_ns: Self::ONE.low_ns.saturating_add(Self::TIMING_TOLERANCE_NS),
};
const MIN_BIT_PERIOD_NS: u32 = 0;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Ws2812B;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Ws2811;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Sk6812;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Ws2816;
impl SingleWireProtocol<Rgb> for Ws2812B {
const NAME: &'static str = "WS2812B";
const DEFAULT_COLOR_ORDER: RgbOrder = RgbOrder::Grb;
const RESET_NS: u32 = 50_000;
const ZERO: PulseTiming = PulseTiming {
high_ns: 400,
low_ns: 850,
};
const ONE: PulseTiming = PulseTiming {
high_ns: 800,
low_ns: 450,
};
}
impl SingleWireProtocol<Rgb> for Ws2811 {
const NAME: &'static str = "WS2811";
const DEFAULT_COLOR_ORDER: RgbOrder = RgbOrder::Rgb;
const RESET_NS: u32 = 50_000;
const ZERO: PulseTiming = PulseTiming {
high_ns: 500,
low_ns: 2_000,
};
const ONE: PulseTiming = PulseTiming {
high_ns: 1_200,
low_ns: 1_300,
};
}
impl SingleWireProtocol<Rgbw> for Sk6812 {
const NAME: &'static str = "SK6812";
const DEFAULT_COLOR_ORDER: RgbwOrder = RgbwOrder::Grbw;
const RESET_NS: u32 = 80_000;
const ZERO: PulseTiming = PulseTiming {
high_ns: 300,
low_ns: 900,
};
const ONE: PulseTiming = PulseTiming {
high_ns: 600,
low_ns: 600,
};
}
impl SingleWireProtocol<Rgb16> for Ws2816 {
const NAME: &'static str = "WS2816";
const DEFAULT_COLOR_ORDER: Rgb16Order = Rgb16Order::Grb;
const RESET_NS: u32 = 300_000;
const ZERO: PulseTiming = PulseTiming {
high_ns: 260,
low_ns: 1_000,
};
const ONE: PulseTiming = PulseTiming {
high_ns: 660,
low_ns: 740,
};
const ZERO_MIN: PulseTiming = PulseTiming {
high_ns: 200,
low_ns: 800,
};
const ZERO_MAX: PulseTiming = PulseTiming {
high_ns: 320,
low_ns: 1_200,
};
const ONE_MIN: PulseTiming = PulseTiming {
high_ns: 520,
low_ns: 480,
};
const ONE_MAX: PulseTiming = PulseTiming {
high_ns: 800,
low_ns: 1_000,
};
const MIN_BIT_PERIOD_NS: u32 = 1_250;
}