blinksy 0.11.0

no-std, no-alloc LED control library designed for 1D, 2D, and 3D layouts
Documentation
use embedded_hal::spi::SpiBus;
#[cfg(feature = "async")]
use embedded_hal_async::spi::SpiBus as SpiBusAsync;

use super::ClockedWriter;
#[cfg(feature = "async")]
use super::ClockedWriterAsync;

/// Writer for clocked LEDs using a hardware SPI peripheral.
///
/// - Separate GPIO pins for data and clock
/// - A dedicated hardware SPI perhipheral for data transmission
///   - Higher data rates than bit-banging
///   - More efficient CPU usage
///   - Better timing precision
/// - Parameters defined by a ClockedLed implementation
///
/// ## Usage
///
/// ```rust
/// use embedded_hal::spi::SpiBus;
/// use blinksy::{driver::clocked::ClockedDriver, leds::Apa102};
///
/// fn setup_leds<S>(spi: S) -> ClockedDriver<Apa102, S>
/// where
///     S: SpiBus<u8>,
/// {
///     // Create a new APA102 driver using SPI
///     ClockedDriver::default()
///         .with_led::<Apa102>()
///         .with_writer(spi)
/// }
/// ```
///
/// # Type Parameters
///
/// - `Spi` - The SPI interface type
///
/// This allows any type implementing the `SpiBus` trait to be used
/// as a writer for clocked LED protocols.
impl<Word, Spi> ClockedWriter<Word> for Spi
where
    Word: Copy + 'static,
    Spi: SpiBus<Word>,
{
    type Error = Spi::Error;

    /// Writes an iterator of bytes using the SPI interface.
    ///
    /// # Arguments
    ///
    /// - `words` - Iterator of bytes to write
    ///
    /// # Returns
    ///
    /// Ok(()) on success or an error if SPI transmission fails
    fn write<Words>(&mut self, words: Words) -> Result<(), Self::Error>
    where
        Words: AsRef<[Word]>,
    {
        self.write(words.as_ref())
    }
}

/// Implementation of ClockedWriterAsync for SPI interfaces.
///
/// This allows any type implementing the SpiBus trait to be used
/// as a writer for clocked LED protocols.
#[cfg(feature = "async")]
impl<Word, Spi> ClockedWriterAsync<Word> for Spi
where
    Word: Copy + 'static,
    Spi: SpiBusAsync<Word>,
{
    type Error = Spi::Error;

    /// Writes an iterator of bytes using the SPI interface.
    ///
    /// # Arguments
    ///
    /// - `words` - Iterator of bytes to write
    ///
    /// # Returns
    ///
    /// Ok(()) on success or an error if SPI transmission fails
    async fn write<Words>(&mut self, words: Words) -> Result<(), Self::Error>
    where
        Words: AsRef<[Word]>,
    {
        self.write(words.as_ref()).await
    }
}