#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/46717278")]
#![deny(missing_docs)]
#![no_std]
use core::{fmt::Debug, marker::PhantomData};
pub use color_order::ColorOrder;
use esp_hal::{
Async, Blocking, DriverMode,
clock::Clocks,
gpio::{Level, interconnect::PeripheralOutput},
rmt::{Channel, Error as RmtError, PulseCode, Tx, TxChannelConfig, TxChannelCreator},
};
use num_traits::Unsigned;
use smart_leds_trait::{
CctWhite, RGB, RGB8, RGBCCT, RGBW, SmartLedsWrite, SmartLedsWriteAsync, White,
};
pub trait Timing {
const TIME_0_LOW: u16;
const TIME_0_HIGH: u16;
const TIME_1_LOW: u16;
const TIME_1_HIGH: u16;
}
const SK68XX_CODE_PERIOD: u16 = 1200;
pub enum Sk68xxTiming {}
impl Timing for Sk68xxTiming {
const TIME_0_HIGH: u16 = 320;
const TIME_0_LOW: u16 = SK68XX_CODE_PERIOD - Self::TIME_0_HIGH;
const TIME_1_HIGH: u16 = 640;
const TIME_1_LOW: u16 = SK68XX_CODE_PERIOD - Self::TIME_1_HIGH;
}
pub enum Ws2812bTiming {}
impl Timing for Ws2812bTiming {
const TIME_0_HIGH: u16 = 400;
const TIME_0_LOW: u16 = 800;
const TIME_1_HIGH: u16 = 850;
const TIME_1_LOW: u16 = 450;
}
pub enum Ws2812Timing {}
impl Timing for Ws2812Timing {
const TIME_0_HIGH: u16 = 350;
const TIME_0_LOW: u16 = 700;
const TIME_1_HIGH: u16 = 800;
const TIME_1_LOW: u16 = 600;
}
pub enum Ws2811LowSpeedTiming {}
impl Timing for Ws2811LowSpeedTiming {
const TIME_0_HIGH: u16 = 500;
const TIME_0_LOW: u16 = 2000;
const TIME_1_HIGH: u16 = 1200;
const TIME_1_LOW: u16 = 1300;
}
pub enum Ws2811Timing {}
impl Timing for Ws2811Timing {
const TIME_0_HIGH: u16 = Ws2811LowSpeedTiming::TIME_0_HIGH / 2;
const TIME_0_LOW: u16 = Ws2811LowSpeedTiming::TIME_0_LOW / 2;
const TIME_1_HIGH: u16 = Ws2811LowSpeedTiming::TIME_1_HIGH / 2;
const TIME_1_LOW: u16 = Ws2811LowSpeedTiming::TIME_1_LOW / 2;
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum AdapterError {
BufferSizeExceeded,
TransmissionError(RmtError),
}
impl From<RmtError> for AdapterError {
fn from(value: RmtError) -> Self {
Self::TransmissionError(value)
}
}
pub trait Color {
const CHANNELS: u8;
type ChannelType: Unsigned + Into<usize>;
}
impl<T> Color for RGB<T>
where
T: Unsigned + Into<usize>,
{
const CHANNELS: u8 = 3;
type ChannelType = T;
}
impl<T> Color for RGBW<T>
where
T: Unsigned + Into<usize>,
{
const CHANNELS: u8 = 4;
type ChannelType = T;
}
impl<T> Color for RGBCCT<T>
where
T: Unsigned + Into<usize>,
{
const CHANNELS: u8 = 5;
type ChannelType = T;
}
impl<T> Color for White<T>
where
T: Unsigned + Into<usize>,
{
const CHANNELS: u8 = 1;
type ChannelType = T;
}
impl<T> Color for CctWhite<T>
where
T: Unsigned + Into<usize>,
{
const CHANNELS: u8 = 2;
type ChannelType = T;
}
pub const fn buffer_size<C: Color>(led_count: usize) -> usize {
led_count * (size_of::<C::ChannelType>() * 8) * C::CHANNELS as usize + 1
}
pub mod color_order {
use num_traits::Unsigned;
use smart_leds_trait::{RGB, RGBW, White};
use crate::Color;
pub trait ColorOrder<C: Color> {
fn get_channel_data(color: &C, channel: u8) -> C::ChannelType;
}
macro_rules! color_order_rgb {
($name:ident => $first:ident, $second:ident, $third:ident) => {
#[doc = concat!("[`ColorOrder`] ", stringify!($name), ".")]
pub enum $name {}
impl<T> ColorOrder<RGB<T>> for $name
where
T: Copy + Unsigned + Into<usize>,
{
fn get_channel_data(color: &RGB<T>, channel: u8) -> T {
match channel {
0 => color.$first,
1 => color.$second,
2 => color.$third,
_ => unreachable!(),
}
}
}
};
}
color_order_rgb!(Rgb => r, g, b);
color_order_rgb!(Rbg => r, b, g);
color_order_rgb!(Grb => g, r, b);
color_order_rgb!(Gbr => g, b, r);
color_order_rgb!(Brg => b, r, g);
color_order_rgb!(Bgr => b, g, r);
pub enum Rgbw {}
impl<T> ColorOrder<RGBW<T>> for Rgbw
where
T: Copy + Unsigned + Into<usize>,
{
fn get_channel_data(color: &RGBW<T>, channel: u8) -> T {
match channel {
0 => color.r,
1 => color.g,
2 => color.b,
3 => color.a.0,
_ => unreachable!(),
}
}
}
pub enum SingleChannel {}
impl<T> ColorOrder<White<T>> for SingleChannel
where
T: Copy + Unsigned + Into<usize>,
{
fn get_channel_data(color: &White<T>, _channel: u8) -> T {
color.0
}
}
}
pub struct RmtSmartLeds<'d, const BUFFER_SIZE: usize, Mode, C, Order, Timing>
where
Mode: DriverMode,
C: Color,
Order: ColorOrder<C>,
Timing: crate::Timing,
{
channel: Option<Channel<'d, Mode, Tx>>,
rmt_buffer: [PulseCode; BUFFER_SIZE],
pulses: (PulseCode, PulseCode),
_order: PhantomData<Order>,
_timing: PhantomData<Timing>,
_color: PhantomData<C>,
}
pub type Rgb8RmtSmartLeds<'d, const BUFFER_SIZE: usize, Mode, Order, Timing> =
RmtSmartLeds<'d, BUFFER_SIZE, Mode, RGB8, Order, Timing>;
pub type Ws2812SmartLeds<'d, const BUFFER_SIZE: usize, Mode> =
Rgb8RmtSmartLeds<'d, BUFFER_SIZE, Mode, color_order::Grb, Ws2812Timing>;
pub type Sk68xxRgbwSmartLeds<'d, const BUFFER_SIZE: usize, Mode> =
RmtSmartLeds<'d, BUFFER_SIZE, Mode, RGBW<u8>, color_order::Rgbw, Sk68xxTiming>;
pub type WhiteSmartLeds<'d, const BUFFER_SIZE: usize, Mode, Timing> =
RmtSmartLeds<'d, BUFFER_SIZE, Mode, White<u8>, color_order::SingleChannel, Timing>;
impl<'d, const BUFFER_SIZE: usize, Mode, C, Order, Timing>
RmtSmartLeds<'d, BUFFER_SIZE, Mode, C, Order, Timing>
where
Mode: DriverMode,
C: Color,
Order: ColorOrder<C>,
Timing: crate::Timing,
{
pub fn new<Ch, P>(channel: Ch, pin: P) -> Result<Self, RmtError>
where
Ch: TxChannelCreator<'d, Mode>,
P: PeripheralOutput<'d>,
{
Self::new_with_memsize(channel, pin, 1)
}
pub fn new_with_memsize<Ch, P>(channel: Ch, pin: P, memsize: u8) -> Result<Self, RmtError>
where
Ch: TxChannelCreator<'d, Mode>,
P: PeripheralOutput<'d>,
{
let config = TxChannelConfig::default()
.with_clk_divider(1)
.with_idle_output_level(Level::Low)
.with_memsize(memsize)
.with_carrier_modulation(false)
.with_idle_output(true);
let channel = channel.configure_tx(pin, config)?;
let clocks = Clocks::get();
let src_clock = clocks.apb_clock.as_hz() / 1_000_000;
let zero_pulse = PulseCode::new(
Level::High,
((Timing::TIME_0_HIGH as u32 * src_clock) / 1000) as u16,
Level::Low,
((Timing::TIME_0_LOW as u32 * src_clock) / 1000) as u16,
);
let mut rmt_buffer = [zero_pulse; _];
rmt_buffer[BUFFER_SIZE - 1] = PulseCode::end_marker();
Ok(Self {
channel: Some(channel),
rmt_buffer,
pulses: (
zero_pulse,
PulseCode::new(
Level::High,
((Timing::TIME_1_HIGH as u32 * src_clock) / 1000) as u16,
Level::Low,
((Timing::TIME_1_LOW as u32 * src_clock) / 1000) as u16,
),
),
_order: PhantomData,
_timing: PhantomData,
_color: PhantomData,
})
}
fn create_rmt_data(
&mut self,
iterator: impl IntoIterator<Item = impl Into<C>>,
) -> Result<(), AdapterError> {
let mut seq_iter = self.rmt_buffer.iter_mut();
for item in iterator {
convert_colors_to_pulse::<_, Order>(&item.into(), &mut seq_iter, self.pulses)?;
}
*seq_iter.next().ok_or(AdapterError::BufferSizeExceeded)? = PulseCode::end_marker();
Ok(())
}
#[allow(unused)]
pub(crate) fn write_pixel_data(
&mut self,
index: usize,
color: impl Into<C>,
) -> Result<(), AdapterError> {
let buffer_start_index = index * C::CHANNELS as usize * (size_of::<C::ChannelType>() * 8);
let mut buffer_iter = self
.rmt_buffer
.get_mut(buffer_start_index..)
.ok_or(AdapterError::BufferSizeExceeded)?
.iter_mut();
convert_colors_to_pulse::<_, Order>(&color.into(), &mut buffer_iter, self.pulses)
}
}
impl<'d, const BUFFER_SIZE: usize, C, Order, Timing>
RmtSmartLeds<'d, BUFFER_SIZE, Blocking, C, Order, Timing>
where
C: Color,
Order: ColorOrder<C>,
Timing: crate::Timing,
{
pub fn flush(&mut self) -> Result<(), AdapterError> {
let channel = self.channel.take().unwrap();
match channel.transmit(&self.rmt_buffer)?.wait() {
Ok(chan) => {
self.channel = Some(chan);
Ok(())
}
Err((e, chan)) => {
self.channel = Some(chan);
Err(AdapterError::TransmissionError(e))
}
}
}
}
impl<'d, const BUFFER_SIZE: usize, C, Order, Timing> SmartLedsWrite
for RmtSmartLeds<'d, BUFFER_SIZE, Blocking, C, Order, Timing>
where
C: Color,
Order: ColorOrder<C>,
Timing: crate::Timing,
{
type Error = AdapterError;
type Color = C;
fn write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error>
where
T: IntoIterator<Item = I>,
I: Into<Self::Color>,
{
self.create_rmt_data(iterator)?;
self.flush()
}
}
impl<'d, const BUFFER_SIZE: usize, C, Order, Timing> SmartLedsWriteAsync
for RmtSmartLeds<'d, BUFFER_SIZE, Async, C, Order, Timing>
where
C: Color,
Order: ColorOrder<C>,
Timing: crate::Timing,
{
type Error = AdapterError;
type Color = C;
fn write<T, I>(&mut self, iterator: T) -> impl Future<Output = Result<(), Self::Error>>
where
T: IntoIterator<Item = I>,
I: Into<Self::Color>,
{
let res = self.create_rmt_data(iterator);
async move {
res?;
self.channel
.as_mut()
.unwrap()
.transmit(&self.rmt_buffer)
.await?;
Ok(())
}
}
}
fn convert_colors_to_pulse<'a, C, Order>(
value: &C,
mut_iter: &mut impl Iterator<Item = &'a mut PulseCode>,
pulses: (PulseCode, PulseCode),
) -> Result<(), AdapterError>
where
C: Color,
Order: ColorOrder<C>,
{
for channel in 0..C::CHANNELS {
convert_channel_to_pulses(Order::get_channel_data(&value, channel), mut_iter, pulses)?;
}
Ok(())
}
fn convert_channel_to_pulses<'a, N>(
channel_value: N,
mut_iter: &mut impl Iterator<Item = &'a mut PulseCode>,
pulses: (PulseCode, PulseCode),
) -> Result<(), AdapterError>
where
N: Unsigned + Into<usize>,
{
let channel_value: usize = channel_value.into();
for index in (0..size_of::<N>() * 8).rev() {
let position = 1 << index;
*mut_iter.next().ok_or(AdapterError::BufferSizeExceeded)? = match channel_value & position {
0 => pulses.0,
_ => pulses.1,
}
}
Ok(())
}