use std::slice::{from_raw_parts, from_raw_parts_mut};
use super::super::bindings::{ws2811_fini, ws2811_render, ws2811_t};
use super::super::util::{Result, RawColor};
#[derive(Clone, Debug)]
pub struct Controller {
c_struct: ws2811_t,
}
impl Controller {
pub fn new(c_struct: ws2811_t) -> Self {
Controller { c_struct }
}
pub fn render(&mut self) -> Result<()> {
unsafe {
return ws2811_render(&mut self.c_struct).into();
}
}
pub fn channels(&self) -> Vec<usize> {
(0..self.c_struct.channel.len()).filter(|x: _ | {
self.c_struct.channel[x.clone()].count > 0
}).collect::<Vec<_>>()
}
pub fn leds(&self, channel: usize) -> &[RawColor] {
unsafe {
from_raw_parts(
self.c_struct.channel[channel].leds as *const RawColor,
self.c_struct.channel[channel].count as usize
)
}
}
pub fn leds_mut(&mut self, channel: usize) -> &mut [RawColor] {
unsafe {
from_raw_parts_mut(
self.c_struct.channel[channel].leds as *mut RawColor,
self.c_struct.channel[channel].count as usize
)
}
}
}
impl Drop for Controller {
fn drop(&mut self) {
unsafe {
ws2811_fini(&mut self.c_struct);
}
}
}