From 999795e505e34c5e3624fc19111d405c6efbc611 Mon Sep 17 00:00:00 2001 From: Jane Petrovna Date: Tue, 28 Dec 2021 15:43:21 -0500 Subject: [PATCH] move parse out of loop --- src/main.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a651b6..02abf6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ pub const BRIGHTNESS: u8 = 150; fn main() { let p: RawColor = [0, 0, 0, 0]; let lock = Arc::new(RwLock::new([p; LED_SIZE])); + let pattern: Arc>>> = Arc::new(RwLock::new(Vec::new())); let read = Arc::clone(&lock); thread::spawn(move || { let mut controller: Controller = ControllerBuilder::new() @@ -29,19 +30,22 @@ fn main() { run_lights(&mut controller, &lights).expect("Error running lights controller."); } }); + let mul_string = format_multiline("unit"); + let res: Result>, ParseError> = mul_string.iter() + .map(|x: &Vec| parse_line((*x).clone())) + .collect(); + if res.is_ok() { + let mut p = pattern.write().unwrap(); + *p = res.unwrap(); + } + let lock_c = Arc::clone(&lock); loop { - let lock_c = Arc::clone(&lock); thread::sleep(time::Duration::from_millis(250)); - let pattern = format_multiline("unit"); - let res: Result>, ParseError> = pattern.iter() - .map(|x: &Vec| parse_line((*x).clone())) - .collect(); - if res.is_ok() { - let mut lights = lock_c.write().unwrap(); - let v: Vec> = res.unwrap(); - v.iter().for_each(move |x| { - x.execute(&mut lights); - }) - } + let pattern_c = Arc::clone(&pattern); + let v = pattern_c.read().expect("could not read pattern"); + let mut lights = lock_c.write().unwrap(); + (*v).iter().for_each(move |x: &Box| { + x.execute(&mut lights); + }); } }