leds/src/main.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

2021-12-23 19:34:26 +00:00
pub mod util;
use util::lights::*;
2021-12-24 16:39:02 +00:00
use std::sync::{Arc, RwLock};
2021-12-23 19:34:26 +00:00
use std::thread;
2021-12-24 23:54:53 +00:00
use rs_ws281x::{RawColor, Controller, ControllerBuilder, ChannelBuilder, StripType};
2021-12-23 19:34:26 +00:00
2021-12-01 01:27:28 +00:00
pub const LED_SIZE: usize = 450;
2021-12-24 23:54:53 +00:00
pub const BRIGHTNESS: u8 = 255;
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
.expect("Could not construct LED Channel.");
2021-12-24 23:54:53 +00:00
)
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.");
2021-12-01 01:27:28 +00:00
run_lights(&mut controller, &lights);
}
2021-12-23 19:34:26 +00:00
});
let mut x: u8 = 0;
loop {
if x == 255 {
x = 0;
}
2021-12-24 18:29:57 +00:00
let color: RawColor = [x, x, x, 255];
2021-12-23 19:34:26 +00:00
let mut lights = lock.write().unwrap();
lights[0] = color;
x += 1;
}
}