leds/src/main.rs

58 lines
1.6 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-28 22:37:02 +00:00
pub const LOOP_WAIT: u64 = 250;
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]));
2021-12-28 22:25:59 +00:00
let mut pattern: Vec<Box<dyn Pattern>> = Vec::new();
2021-12-28 20:27:23 +00:00
let read = Arc::clone(&lock);
2021-12-28 22:40:46 +00:00
//light management
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-28 20:27:23 +00:00
let lights = read.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
});
2021-12-28 22:37:02 +00:00
2021-12-28 22:40:46 +00:00
//pattern parsing
let mul_string = format_multiline(concat!(
"val 0 0 255",
"val r 255"
));
2021-12-28 20:43:21 +00:00
let res: Result<Vec<Box<dyn Pattern>>, ParseError> = mul_string.iter()
.map(|x: &Vec<String>| parse_line((*x).clone()))
.collect();
if res.is_ok() {
2021-12-28 22:25:59 +00:00
pattern = res.unwrap();
2021-12-28 20:43:21 +00:00
}
2021-12-28 22:37:02 +00:00
2021-12-28 22:40:46 +00:00
//pattern management
loop {
2021-12-28 22:37:02 +00:00
thread::sleep(time::Duration::from_millis(LOOP_WAIT));
2021-12-28 22:25:59 +00:00
for p in &pattern {
let c_lock = Arc::clone(&lock);
let mut lights = c_lock.write().unwrap();
p.execute(&mut lights);
}
2021-12-23 19:34:26 +00:00
}
}