use crate::color::*;
use crate::device::*;
use std::io;
use std::thread;
use std::time;
pub struct Ws2812 {}
impl Device for Ws2812 {
fn color_correction(&self) -> Correction {
Correction::srgb(255, 255, 255)
}
fn spidev_config(&self) -> Option<spidev::Config> {
Some(spidev::Config {
clock_polarity: 0, clock_phase: 0, first_bit: spidev::FirstBit::Msb,
speed_hz: 2_400_000, })
}
fn write_frame(&self, writer: &mut dyn io::Write, pixels: &[Pixel]) -> io::Result<()> {
let buf: Vec<u8> = pixels
.iter()
.flat_map(|pix| vec![pix.g, pix.r, pix.b])
.flat_map(|b| {
let mut obits: u32 = 0;
for i in 0..8 {
let middle_bit = (b >> i) & 1;
obits |= (0b100 | u32::from(middle_bit << 1)) << (i * 3);
}
vec![
((obits >> 16) & 0xff) as u8,
((obits >> 8) & 0xff) as u8,
(obits & 0xff) as u8,
]
})
.collect();
writer.write_all(&buf)?;
thread::sleep(time::Duration::from_micros(50)); Ok(())
}
}
pub fn command<'a, 'b>() -> clap::App<'a, 'b> {
clap::SubCommand::with_name("ws2812")
}
pub fn from_command(_: &clap::ArgMatches, _: &GlobalArgs) -> io::Result<FromCommand> {
Ok(FromCommand::Device(Box::new(Ws2812 {})))
}