|
| 1 | +//! # Use WS2812 LEDs via SPI on Linux hosts |
| 2 | +//! |
| 3 | +//! This dynamically allocates an output buffer and writes out the data in a single call. |
| 4 | +//! Much better suited for linux or similar environments, but may not always work |
| 5 | +//! |
| 6 | +//! Intendded for use with rppal or linux-embedded-hal |
| 7 | +
|
| 8 | +use embedded_hal as hal; |
| 9 | + |
| 10 | +use hal::spi::{Mode, Phase, Polarity, SpiBus}; |
| 11 | + |
| 12 | +use core::marker::PhantomData; |
| 13 | + |
| 14 | +use smart_leds_trait::{SmartLedsWrite, RGB8, RGBW}; |
| 15 | + |
| 16 | +use std::vec; |
| 17 | +use std::vec::Vec; |
| 18 | + |
| 19 | +/// SPI mode that can be used for this crate |
| 20 | +/// |
| 21 | +/// Provided for convenience |
| 22 | +/// Doesn't really matter |
| 23 | +pub const MODE: Mode = Mode { |
| 24 | + polarity: Polarity::IdleLow, |
| 25 | + phase: Phase::CaptureOnFirstTransition, |
| 26 | +}; |
| 27 | + |
| 28 | +pub mod devices { |
| 29 | + pub struct Ws2812; |
| 30 | + pub struct Sk6812w; |
| 31 | +} |
| 32 | + |
| 33 | +pub struct Ws2812<SPI, DEVICE = devices::Ws2812> { |
| 34 | + spi: SPI, |
| 35 | + data: Vec<u8>, |
| 36 | + device: PhantomData<DEVICE>, |
| 37 | +} |
| 38 | + |
| 39 | +impl<SPI, E> Ws2812<SPI> |
| 40 | +where |
| 41 | + SPI: SpiBus<u8, Error = E>, |
| 42 | +{ |
| 43 | + /// Use ws2812 devices via spi |
| 44 | + /// |
| 45 | + /// The SPI bus should run within 2 MHz to 3.8 MHz |
| 46 | + /// |
| 47 | + /// You may need to look at the datasheet and your own hal to verify this. |
| 48 | + /// |
| 49 | + pub fn new(spi: SPI) -> Self { |
| 50 | + let data = vec![0; 140]; |
| 51 | + |
| 52 | + Self { |
| 53 | + spi, |
| 54 | + data, |
| 55 | + device: PhantomData {}, |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<SPI, E> Ws2812<SPI, devices::Sk6812w> |
| 61 | +where |
| 62 | + SPI: SpiBus<u8, Error = E>, |
| 63 | +{ |
| 64 | + /// Use sk6812w devices via spi |
| 65 | + /// |
| 66 | + /// The SPI bus should run within 2.3 MHz to 3.8 MHz at least. |
| 67 | + /// |
| 68 | + /// You may need to look at the datasheet and your own hal to verify this. |
| 69 | + /// |
| 70 | + // The spi frequencies are just the limits, the available timing data isn't |
| 71 | + // complete |
| 72 | + pub fn new_sk6812w(spi: SPI) -> Self { |
| 73 | + let data = vec![0; 140]; |
| 74 | + |
| 75 | + Self { |
| 76 | + spi, |
| 77 | + data, |
| 78 | + device: PhantomData {}, |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl<SPI, D, E> Ws2812<SPI, D> |
| 84 | +where |
| 85 | + SPI: SpiBus<u8, Error = E>, |
| 86 | +{ |
| 87 | + /// Write a single byte for ws2812 devices |
| 88 | + fn write_byte(&mut self, mut data: u8) { |
| 89 | + // Send two bits in one spi byte. High time first, then the low time |
| 90 | + // The maximum for T0H is 500ns, the minimum for one bit 1063 ns. |
| 91 | + // These result in the upper and lower spi frequency limits |
| 92 | + let patterns = [0b1000_1000, 0b1000_1110, 0b11101000, 0b11101110]; |
| 93 | + for _ in 0..4 { |
| 94 | + let bits = (data & 0b1100_0000) >> 6; |
| 95 | + self.data.push(patterns[bits as usize]); |
| 96 | + data <<= 2; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + fn send_data(&mut self) -> Result<(), E> { |
| 101 | + self.data.extend_from_slice(&[0; 140]); |
| 102 | + self.spi.write(&self.data)?; |
| 103 | + self.data.truncate(140); |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl<SPI, E> SmartLedsWrite for Ws2812<SPI> |
| 109 | +where |
| 110 | + SPI: SpiBus<u8, Error = E>, |
| 111 | +{ |
| 112 | + type Error = E; |
| 113 | + type Color = RGB8; |
| 114 | + /// Write all the items of an iterator to a ws2812 strip |
| 115 | + fn write<T, I>(&mut self, iterator: T) -> Result<(), E> |
| 116 | + where |
| 117 | + T: IntoIterator<Item = I>, |
| 118 | + I: Into<Self::Color>, |
| 119 | + { |
| 120 | + for item in iterator { |
| 121 | + let item = item.into(); |
| 122 | + self.write_byte(item.g); |
| 123 | + self.write_byte(item.r); |
| 124 | + self.write_byte(item.b); |
| 125 | + } |
| 126 | + self.send_data() |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl<SPI, E> SmartLedsWrite for Ws2812<SPI, devices::Sk6812w> |
| 131 | +where |
| 132 | + SPI: SpiBus<u8, Error = E>, |
| 133 | +{ |
| 134 | + type Error = E; |
| 135 | + type Color = RGBW<u8, u8>; |
| 136 | + /// Write all the items of an iterator to a ws2812 strip |
| 137 | + fn write<T, I>(&mut self, iterator: T) -> Result<(), E> |
| 138 | + where |
| 139 | + T: IntoIterator<Item = I>, |
| 140 | + I: Into<Self::Color>, |
| 141 | + { |
| 142 | + for item in iterator { |
| 143 | + let item = item.into(); |
| 144 | + self.write_byte(item.g); |
| 145 | + self.write_byte(item.r); |
| 146 | + self.write_byte(item.b); |
| 147 | + self.write_byte(item.a.0); |
| 148 | + } |
| 149 | + self.send_data() |
| 150 | + } |
| 151 | +} |
0 commit comments