leds/src/util/pattern.rs

104 lines
3.0 KiB
Rust
Raw Normal View History

2021-12-26 18:14:03 +00:00
use rs_ws281x::RawColor;
use std::{
vec::Vec,
string::String
};
2021-12-27 02:32:42 +00:00
use std::sync::RwLockWriteGuard;
2021-12-26 18:14:03 +00:00
type ParseError = Box<dyn std::error::Error>;
2021-12-03 00:01:16 +00:00
pub trait Pattern<'a> {
2021-12-27 02:36:05 +00:00
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>);
2021-12-03 00:01:16 +00:00
fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized;
2021-12-26 18:14:03 +00:00
}
2021-12-27 01:26:29 +00:00
impl<'a> TryFrom<Vec<String>> for Box<dyn Pattern<'a>> {
2021-12-02 23:46:52 +00:00
type Error = ParseError;
2021-12-03 00:01:16 +00:00
fn try_from(s: Vec<String>) -> Result<Self, Self::Error> {
2021-12-26 18:14:03 +00:00
match s[0].as_str() {
2021-12-03 00:01:16 +00:00
"unit" => Ok(Box::new(Unit::parse(s).unwrap()) as Box<dyn Pattern>),
"val" => Ok(Box::new(Value::parse(s).unwrap()) as Box<dyn Pattern>),
2021-12-26 18:14:03 +00:00
_ => Err("No Match".into())
}
}
}
struct Unit;
2021-12-27 01:26:29 +00:00
impl<'a> Pattern<'a> for Unit {
2021-12-27 02:36:05 +00:00
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {}
2021-12-26 18:14:03 +00:00
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
Ok(Unit {})
}
}
struct Value {
r: Option<u8>,
g: Option<u8>,
b: Option<u8>
2021-12-27 01:26:29 +00:00
}
impl<'a> Pattern<'a> for Value {
2021-12-27 02:36:05 +00:00
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {
let mut v = *values;
2021-12-26 18:14:03 +00:00
for i in 0..crate::LED_SIZE {
2021-12-27 02:36:05 +00:00
let mut color: RawColor = v[i];
2021-12-26 18:14:03 +00:00
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();
}
2021-12-27 02:36:05 +00:00
v[i] = color;
2021-12-26 18:14:03 +00:00
}
2021-12-27 02:36:05 +00:00
*values = v;
2021-12-26 18:14:03 +00:00
}
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
2021-12-27 01:26:29 +00:00
let param1 = args[1].parse::<u8>();
let param2 = args[2].parse::<u8>();
let param3 = args[2].parse::<u8>();
2021-12-26 18:14:03 +00:00
if param1.is_ok() && param2.is_ok() && param3.is_ok() {
Ok(Value{
2021-12-27 02:13:11 +00:00
r: Some(param1.unwrap()),
g: Some(param2.unwrap()),
b: Some(param3.unwrap())
2021-12-26 18:14:03 +00:00
})
}
else {
match param2 {
2021-12-27 01:26:29 +00:00
Ok(i) => match args[1].as_str() {
2021-12-26 18:14:03 +00:00
"r" => {
2021-12-27 01:26:29 +00:00
Ok(Value {
2021-12-26 18:14:03 +00:00
r: Some(i),
g: None,
b: None
})
},
"g" => {
Ok(Value {
r: None,
g: Some(i),
b: None
})
},
"b" => {
2021-12-27 01:26:29 +00:00
Ok(Value {
2021-12-26 18:14:03 +00:00
r: None,
g: None,
b: Some(i)
})
}
},
2021-12-03 00:01:16 +00:00
Err(e) => Err(Box::new(e))
2021-12-26 18:14:03 +00:00
}
}
}
}
2021-12-27 02:39:39 +00:00
pub fn parse_line<'a>(v: Vec<String>) -> Result<Box<dyn Pattern<'a>>, ParseError> {
2021-12-27 01:26:29 +00:00
let res: Result<Box<dyn Pattern>, ParseError> = v.try_into();
res
2021-12-02 23:46:52 +00:00
}