102 lines
3 KiB
Rust
102 lines
3 KiB
Rust
use rs_ws281x::RawColor;
|
|
use std::{
|
|
vec::Vec,
|
|
string::String
|
|
};
|
|
use std::sync::RwLockWriteGuard;
|
|
|
|
type ParseError = Box<dyn std::error::Error>;
|
|
|
|
pub trait Pattern<'a> {
|
|
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>);
|
|
fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized;
|
|
}
|
|
|
|
impl<'a> TryFrom<Vec<String>> for Box<dyn Pattern<'a>> {
|
|
type Error = ParseError;
|
|
fn try_from(s: Vec<String>) -> Result<Self, Self::Error> {
|
|
match s[0].as_str() {
|
|
"unit" => Ok(Box::new(Unit::parse(s).unwrap()) as Box<dyn Pattern>),
|
|
"val" => Ok(Box::new(Value::parse(s).unwrap()) as Box<dyn Pattern>),
|
|
_ => Err("No Match".into())
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Unit;
|
|
impl<'a> Pattern<'a> for Unit {
|
|
fn execute(&self, _values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {}
|
|
fn parse(_args: Vec<String>) -> Result<Self, ParseError> {
|
|
Ok(Unit {})
|
|
}
|
|
}
|
|
|
|
struct Value {
|
|
r: Option<u8>,
|
|
g: Option<u8>,
|
|
b: Option<u8>
|
|
}
|
|
|
|
impl<'a> Pattern<'a> for Value {
|
|
fn execute(&self, values: &'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() {
|
|
color[0] = self.r.unwrap();
|
|
}
|
|
if self.g.is_some() {
|
|
color[0] = self.g.unwrap();
|
|
}
|
|
if self.b.is_some() {
|
|
color[0] = self.b.unwrap();
|
|
}
|
|
(*values)[i] = color;
|
|
}
|
|
}
|
|
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
|
|
let param1 = args[1].parse::<u8>();
|
|
let param2 = args[2].parse::<u8>();
|
|
let param3 = args[2].parse::<u8>();
|
|
if param1.is_ok() && param2.is_ok() && param3.is_ok() {
|
|
Ok(Value{
|
|
r: Some(param1.unwrap()),
|
|
g: Some(param2.unwrap()),
|
|
b: Some(param3.unwrap())
|
|
})
|
|
}
|
|
else {
|
|
match param2 {
|
|
Ok(i) => match args[1].as_str() {
|
|
"r" => {
|
|
Ok(Value {
|
|
r: Some(i),
|
|
g: None,
|
|
b: None
|
|
})
|
|
},
|
|
"g" => {
|
|
Ok(Value {
|
|
r: None,
|
|
g: Some(i),
|
|
b: None
|
|
})
|
|
},
|
|
"b" => {
|
|
Ok(Value {
|
|
r: None,
|
|
g: None,
|
|
b: Some(i)
|
|
})
|
|
}
|
|
_ => Err("unreachable".into())
|
|
},
|
|
Err(e) => Err(Box::new(e))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn parse_line<'a>(v: Vec<String>) -> Result<Box<dyn Pattern<'a>>, ParseError> {
|
|
let res: Result<Box<dyn Pattern>, ParseError> = v.try_into();
|
|
res
|
|
}
|