leds/src/main.rs

49 lines
1.4 KiB
Rust
Raw Normal View History

2021-12-23 19:34:26 +00:00
pub mod util;
use util::lights::*;
2021-12-26 18:14:03 +00:00
use util::pattern::*;
2021-12-24 16:39:02 +00:00
use std::sync::{Arc, RwLock};
2021-12-24 23:54:53 +00:00
use rs_ws281x::{RawColor, Controller, ControllerBuilder, ChannelBuilder, StripType};
use std::{thread, time};
2021-12-23 19:34:26 +00:00
pub const LED_SIZE: usize = 450; //450
pub const BRIGHTNESS: u8 = 150;
2021-12-23 19:34:26 +00:00
fn main() {
2021-12-24 18:29:57 +00:00
let p: RawColor = [0, 0, 0, 0];
2021-12-24 16:39:02 +00:00
let lock = Arc::new(RwLock::new([p; LED_SIZE]));
let lock_c = Arc::clone(&lock);
2021-12-24 23:54:53 +00:00
2021-12-01 01:27:28 +00:00
thread::spawn(move || {
let mut controller: Controller = ControllerBuilder::new()
.channel(0, ChannelBuilder::new()
2021-12-24 23:54:53 +00:00
.pin(18)
2021-12-01 01:27:28 +00:00
.count(i32::try_from(LED_SIZE).unwrap())
2021-12-24 23:54:53 +00:00
.strip_type(StripType::Ws2812)
.brightness(BRIGHTNESS)
.build()
)
2021-12-01 00:55:08 +00:00
.build()
.expect("Could not construct LED Controller.");
2021-12-01 01:27:28 +00:00
loop {
2021-12-01 00:55:08 +00:00
let lights = lock_c.read().expect("Could not read array lock.");
run_lights(&mut controller, &lights).expect("Error running lights controller.");
2021-12-01 01:27:28 +00:00
}
2021-12-23 19:34:26 +00:00
});
loop {
thread::sleep(time::Duration::from_millis(250));
2021-12-28 20:00:29 +00:00
let mut lights = lock.write().unwrap();
let pattern = format_multiline("unit");
let res: Result<Vec<Box<dyn Pattern>>> = pattern.iter()
2021-12-28 00:09:59 +00:00
.map(|x: &Vec<String>| parse_line((*x).clone()))
2021-12-28 20:00:29 +00:00
.collect();
if res.is_ok() {
let v: Vec<Box<dyn Pattern>> = res.unwrap();
for x in 0..v.len() {
v[x].execute(&mut lights);
}
}
2021-12-23 19:34:26 +00:00
}
}