ws2812-uart 0.2.0

UART-based driver for WS2812 and similar smart LEDs
Documentation
//! # Use ws2812 leds via UART
//!
//! - For usage with `smart-leds`
//! - Implements the `SmartLedsWrite` trait
//!
//! The UART Tx pin has to be inverted (either in software or
//! in hardware). The UART itself should be pretty fast.
//! Recommended speed is 3_750_000 baud, 8-N-1.

#![no_std]

use core::marker::PhantomData;
use smart_leds_trait::{RGB8, RGBW};

pub mod device {
    pub struct Ws2812;
    pub struct Sk6812w;

    pub trait Ws2812Like {
        type Color: super::EncodeColor;
    }

    impl Ws2812Like for Ws2812 {
        type Color = super::RGB8;
    }

    impl Ws2812Like for Sk6812w {
        type Color = super::RGBW<u8>;
    }
}

pub trait EncodeColor {
    type Bytes: IntoIterator<Item = u8>;
    fn encode(self) -> Self::Bytes;
}

impl EncodeColor for RGB8 {
    type Bytes = [u8; 3];
    fn encode(self) -> Self::Bytes {
        [self.g, self.r, self.b]
    }
}

impl EncodeColor for RGBW<u8> {
    type Bytes = [u8; 4];
    fn encode(self) -> Self::Bytes {
        [self.g, self.r, self.b, self.a.0]
    }
}

pub struct Ws2812<UART, DEV = device::Ws2812> {
    uart: UART,
    _device: PhantomData<DEV>,
}

/// Encode byte as four bytes.
fn encode_byte(mut byte: u8) -> [u8; 4] {
    let mut res = [0; 4];
    for i in 0..4 {
        res[i] = !(0b_0001_0000
            | if (byte & 0b_10_00_0000) != 0 {
                0b_0000_0011
            } else {
                0
            }
            | if (byte & 0b_01_00_0000) != 0 {
                0b_0110_0000
            } else {
                0
            });
        byte <<= 2;
    }
    res
}

impl<UART, DEV> Ws2812<UART, DEV> {
    /// Construct smart LED output from an UART instance.
    pub fn new(uart: UART) -> Self {
        Self {
            uart,
            _device: PhantomData,
        }
    }
}

#[cfg(feature = "blocking")]
mod blocking {
    use super::{device, encode_byte, EncodeColor, Ws2812};
    use embedded_io::{ErrorType as BlockingErrorType, Write as BlockingWrite};
    use smart_leds_trait::SmartLedsWrite;

    impl<UART, DEV> Ws2812<UART, DEV>
    where
        UART: BlockingWrite,
    {
        fn send_byte(&mut self, byte: u8) -> Result<(), <UART as BlockingErrorType>::Error> {
            let bytes = encode_byte(byte);
            self.uart.write_all(&bytes)
        }
    }

    impl<UART, DEV> SmartLedsWrite for Ws2812<UART, DEV>
    where
        UART: BlockingWrite,
        DEV: device::Ws2812Like,
    {
        type Error = <UART as BlockingErrorType>::Error;
        type Color = DEV::Color;

        fn write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error>
        where
            T: IntoIterator<Item = I>,
            I: Into<Self::Color>,
        {
            for item in iterator {
                let color = item.into();
                for byte in color.encode() {
                    self.send_byte(byte)?;
                }
            }
            Ok(())
        }
    }
}

#[cfg(feature = "async")]
mod asyncs {
    use super::{device, encode_byte, EncodeColor, Ws2812};
    use embedded_io_async::{ErrorType as AsyncErrorType, Write as AsyncWrite};
    use smart_leds_trait::SmartLedsWriteAsync;

    impl<UART, DEV> Ws2812<UART, DEV>
    where
        UART: AsyncWrite,
    {
        async fn send_byte_async(
            &mut self,
            byte: u8,
        ) -> Result<(), <UART as AsyncErrorType>::Error> {
            let bytes = encode_byte(byte);
            self.uart.write_all(&bytes).await
        }
    }

    impl<UART, DEV> SmartLedsWriteAsync for Ws2812<UART, DEV>
    where
        UART: AsyncWrite,
        DEV: device::Ws2812Like,
    {
        type Error = <UART as AsyncErrorType>::Error;
        type Color = DEV::Color;

        async fn write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error>
        where
            T: IntoIterator<Item = I>,
            I: Into<Self::Color>,
        {
            for item in iterator {
                let color = item.into();
                for byte in color.encode() {
                    self.send_byte_async(byte).await?;
                }
            }
            Ok(())
        }
    }
}