use std::mem;
use std::os::raw::{c_int};
use super::super::bindings::{ws2811_init, ws2811_t, ws2811_channel_t};
use super::super::util::Result;
use super::controller::Controller;
#[derive(Debug)]
pub struct ControllerBuilder(pub ws2811_t);
impl ControllerBuilder {
pub fn new() -> Self {
unsafe {
let mut cb = ControllerBuilder(mem::zeroed());
cb.freq(800_000);
cb.dma(10);
cb
}
}
pub fn freq(&mut self, value: u32) -> &mut Self {
self.0.freq = value;
self
}
pub fn channel(&mut self, index: usize, channel: ws2811_channel_t) -> &mut Self {
self.0.channel[index] = channel;
self
}
pub fn dma(&mut self, value: i32) -> &mut Self {
self.0.dmanum = value as c_int;
self
}
pub fn render_wait_time(&mut self, value: u64) -> &mut Self {
self.0.render_wait_time = value;
self
}
pub fn build(&mut self) -> Result<Controller> {
let mut c_struct = self.0.clone();
unsafe {
let res: Result<()> = ws2811_init(&mut c_struct).into();
match res {
Ok(_) => {}
Err(e) => return Err(e),
}
return Ok(Controller::new(c_struct));
}
}
}