use core::marker::PhantomData;
use embedded_hal::spi::SpiBus;
#[cfg(feature = "async")]
use embedded_hal_async::spi::SpiBus as SpiBusAsync;
#[cfg(feature = "async")]
use crate::driver::DriverAsync;
use crate::{
color::{ColorCorrection, FromColor},
driver::Driver,
};
use super::{ClockedLed, ClockedWriter};
#[cfg(feature = "async")]
use super::{ClockedLedAsync, ClockedWriterAsync};
#[derive(Debug)]
pub struct ClockedSpiDriver<Led, Spi> {
led: PhantomData<Led>,
writer: Spi,
}
impl<Led, Spi> ClockedSpiDriver<Led, Spi> {
pub fn new(spi: Spi) -> Self {
Self {
led: PhantomData,
writer: spi,
}
}
}
impl<Led, Spi> Driver for ClockedSpiDriver<Led, Spi>
where
Led: ClockedLed<Word = u8>,
Spi: SpiBus<u8>,
{
type Error = Spi::Error;
type Color = Led::Color;
fn write<I, C>(
&mut self,
pixels: I,
brightness: f32,
correction: ColorCorrection,
) -> Result<(), Self::Error>
where
I: IntoIterator<Item = C>,
Self::Color: FromColor<C>,
{
Led::clocked_write(&mut self.writer, pixels, brightness, correction)
}
}
#[cfg(feature = "async")]
impl<Led, Spi> DriverAsync for ClockedSpiDriver<Led, Spi>
where
Led: ClockedLedAsync<Word = u8>,
Spi: SpiBusAsync<u8>,
{
type Error = Spi::Error;
type Color = Led::Color;
async fn write<I, C>(
&mut self,
pixels: I,
brightness: f32,
correction: ColorCorrection,
) -> Result<(), Self::Error>
where
I: IntoIterator<Item = C>,
Self::Color: FromColor<C>,
{
Led::clocked_write(&mut self.writer, pixels, brightness, correction).await
}
}
impl<Spi> ClockedWriter for Spi
where
Spi: SpiBus<u8>,
{
type Error = Spi::Error;
type Word = u8;
fn write(&mut self, words: &[Self::Word]) -> Result<(), Self::Error> {
self.write(words)
}
}
#[cfg(feature = "async")]
impl<Spi> ClockedWriterAsync for Spi
where
Spi: SpiBusAsync<u8>,
{
type Error = Spi::Error;
type Word = u8;
async fn write(&mut self, words: &[Self::Word]) -> Result<(), Self::Error> {
self.write(words).await
}
}