60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
pub mod util;
|
|
|
|
use util::lights::*;
|
|
use util::pattern::*;
|
|
use std::sync::{Arc, RwLock};
|
|
use rs_ws281x::{RawColor, Controller, ControllerBuilder, ChannelBuilder, StripType};
|
|
use std::{thread, time};
|
|
|
|
pub const LED_SIZE: usize = 450; //450
|
|
pub const BRIGHTNESS: u8 = 150;
|
|
pub const LOOP_WAIT: u64 = 250;
|
|
|
|
fn main() {
|
|
let p: RawColor = [0, 0, 0, 0];
|
|
let lock = Arc::new(RwLock::new([p; LED_SIZE]));
|
|
let mut pattern: Vec<Box<dyn Pattern>> = Vec::new();
|
|
let read = Arc::clone(&lock);
|
|
//light management
|
|
thread::spawn(move || {
|
|
let mut controller: Controller = ControllerBuilder::new()
|
|
.channel(0, ChannelBuilder::new()
|
|
.pin(18)
|
|
.count(i32::try_from(LED_SIZE).unwrap())
|
|
.strip_type(StripType::Ws2812)
|
|
.brightness(BRIGHTNESS)
|
|
.build()
|
|
)
|
|
.build()
|
|
.expect("Could not construct LED Controller.");
|
|
loop {
|
|
let lights = read.read().expect("Could not read array lock.");
|
|
run_lights(&mut controller, &lights).expect("Error running lights controller.");
|
|
}
|
|
});
|
|
|
|
//pattern parsing
|
|
let mul_string = format_multiline(concat!(
|
|
"val 0 0 255\n",
|
|
"val r 255"
|
|
));
|
|
let res: Result<Vec<Box<dyn Pattern>>, ParseError> = mul_string.iter()
|
|
.map(|x: &Vec<String>| parse_line((*x).clone()))
|
|
.collect();
|
|
if res.is_ok() {
|
|
pattern = res.unwrap();
|
|
}
|
|
else {
|
|
println!("ret err {:?}", res.err());
|
|
}
|
|
|
|
//pattern management
|
|
loop {
|
|
thread::sleep(time::Duration::from_millis(LOOP_WAIT));
|
|
for p in &pattern {
|
|
let c_lock = Arc::clone(&lock);
|
|
let mut lights = c_lock.write().unwrap();
|
|
p.execute(&mut lights);
|
|
}
|
|
}
|
|
}
|