#![doc = document_features::document_features!()]
#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/46717278")]
#![deny(missing_docs)]
#![no_std]
use core::{fmt::Debug, slice::IterMut};
use esp_hal::{
clock::Clocks,
gpio::OutputPin,
peripheral::Peripheral,
rmt::{Error as RmtError, PulseCode, TxChannel, TxChannelConfig, TxChannelCreator},
};
use smart_leds_trait::{SmartLedsWrite, RGB8};
const SK68XX_CODE_PERIOD: u32 = 1250; const SK68XX_T0H_NS: u32 = 400; const SK68XX_T0L_NS: u32 = SK68XX_CODE_PERIOD - SK68XX_T0H_NS;
const SK68XX_T1H_NS: u32 = 850; const SK68XX_T1L_NS: u32 = SK68XX_CODE_PERIOD - SK68XX_T1H_NS;
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum LedAdapterError {
BufferSizeExceeded,
TransmissionError(RmtError),
}
impl From<RmtError> for LedAdapterError {
fn from(e: RmtError) -> Self {
LedAdapterError::TransmissionError(e)
}
}
#[macro_export]
macro_rules! smartLedBuffer {
( $buffer_size: literal ) => {
[0u32; $buffer_size * 24 + 1]
};
}
pub struct SmartLedsAdapter<TX, const BUFFER_SIZE: usize>
where
TX: TxChannel,
{
channel: Option<TX>,
rmt_buffer: [u32; BUFFER_SIZE],
pulses: (u32, u32),
}
impl<'d, TX, const BUFFER_SIZE: usize> SmartLedsAdapter<TX, BUFFER_SIZE>
where
TX: TxChannel,
{
pub fn new<C, O>(
channel: C,
pin: impl Peripheral<P = O> + 'd,
rmt_buffer: [u32; BUFFER_SIZE],
) -> SmartLedsAdapter<TX, BUFFER_SIZE>
where
O: OutputPin + 'd,
C: TxChannelCreator<'d, TX, O>,
{
let config = TxChannelConfig {
clk_divider: 1,
idle_output_level: false,
carrier_modulation: false,
idle_output: true,
..TxChannelConfig::default()
};
let channel = channel.configure(pin, config).unwrap();
let clocks = Clocks::get();
let src_clock = clocks.apb_clock.to_MHz();
Self {
channel: Some(channel),
rmt_buffer,
pulses: (
PulseCode::new (
true,
((SK68XX_T0H_NS * src_clock) / 1000) as u16,
false,
((SK68XX_T0L_NS * src_clock) / 1000) as u16,
),
PulseCode::new (
true,
((SK68XX_T1H_NS * src_clock) / 1000) as u16,
false,
((SK68XX_T1L_NS * src_clock) / 1000) as u16,
),
),
}
}
fn convert_rgb_to_pulse(
value: RGB8,
mut_iter: &mut IterMut<u32>,
pulses: (u32, u32),
) -> Result<(), LedAdapterError> {
Self::convert_rgb_channel_to_pulses(value.g, mut_iter, pulses)?;
Self::convert_rgb_channel_to_pulses(value.r, mut_iter, pulses)?;
Self::convert_rgb_channel_to_pulses(value.b, mut_iter, pulses)?;
Ok(())
}
fn convert_rgb_channel_to_pulses(
channel_value: u8,
mut_iter: &mut IterMut<u32>,
pulses: (u32, u32),
) -> Result<(), LedAdapterError> {
for position in [128, 64, 32, 16, 8, 4, 2, 1] {
*mut_iter.next().ok_or(LedAdapterError::BufferSizeExceeded)? =
match channel_value & position {
0 => pulses.0,
_ => pulses.1,
}
}
Ok(())
}
}
impl<TX, const BUFFER_SIZE: usize> SmartLedsWrite for SmartLedsAdapter<TX, BUFFER_SIZE>
where
TX: TxChannel,
{
type Error = LedAdapterError;
type Color = RGB8;
fn write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error>
where
T: IntoIterator<Item = I>,
I: Into<Self::Color>,
{
let mut seq_iter = self.rmt_buffer.iter_mut();
for item in iterator {
Self::convert_rgb_to_pulse(item.into(), &mut seq_iter, self.pulses)?;
}
*seq_iter.next().ok_or(LedAdapterError::BufferSizeExceeded)? = 0;
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(LedAdapterError::TransmissionError(e))
}
}
}
}