This commit is contained in:
jane 2021-12-28 15:05:59 -05:00
parent 1e6f006e10
commit 7ea486dea6
2 changed files with 8 additions and 5 deletions

View file

@ -34,14 +34,14 @@ fn main() {
thread::sleep(time::Duration::from_millis(250));
let mut lights = lock.write().unwrap();
let pattern = format_multiline("unit");
let res: Result<Vec<Box<dyn Pattern>>> = pattern.iter()
let res: Result<Vec<Box<dyn Pattern>>, ParseError> = pattern.iter()
.map(|x: &Vec<String>| parse_line((*x).clone()))
.collect();
if res.is_ok() {
let v: Vec<Box<dyn Pattern>> = res.unwrap();
for x in 0..v.len() {
v[x].execute(&mut lights);
lights = *(v[x].execute(&mut lights));
}
}
}

View file

@ -8,7 +8,7 @@ use std::sync::RwLockWriteGuard;
pub type ParseError = Box<dyn std::error::Error>;
pub trait Pattern<'a> {
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>);
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) -> &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>;
fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized;
}
@ -25,7 +25,9 @@ impl<'a> TryFrom<Vec<String>> for Box<dyn Pattern<'a>> {
struct Unit;
impl<'a> Pattern<'a> for Unit {
fn execute(&self, _values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {}
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) -> &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]> {
values
}
fn parse(_args: Vec<String>) -> Result<Self, ParseError> {
Ok(Unit {})
}
@ -38,7 +40,7 @@ struct Value {
}
impl<'a> Pattern<'a> for Value {
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) -> &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]> {
for i in 0..crate::LED_SIZE {
let mut color: RawColor = (*values)[i];
if self.r.is_some() {
@ -52,6 +54,7 @@ impl<'a> Pattern<'a> for Value {
}
(*values)[i] = color;
}
values
}
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
let param1 = args[1].parse::<u8>();