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
|
|
|
|
2021-12-28 00:09:59 +00:00
|
|
|
pub type ParseError = Box<dyn std::error::Error>;
|
2021-12-26 18:14:03 +00:00
|
|
|
|
2021-12-28 22:37:02 +00:00
|
|
|
pub trait Pattern {
|
|
|
|
fn execute(&self, values: &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-28 22:37:02 +00:00
|
|
|
impl TryFrom<Vec<String>> for Box<dyn Pattern> {
|
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-28 22:37:02 +00:00
|
|
|
impl Pattern for Unit {
|
|
|
|
fn execute(&self, _values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {}
|
2021-12-27 02:42:44 +00:00
|
|
|
fn parse(_args: Vec<String>) -> Result<Self, ParseError> {
|
2021-12-26 18:14:03 +00:00
|
|
|
Ok(Unit {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Value {
|
|
|
|
r: Option<u8>,
|
|
|
|
g: Option<u8>,
|
|
|
|
b: Option<u8>
|
2021-12-27 01:26:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 22:37:02 +00:00
|
|
|
impl Pattern for Value {
|
|
|
|
fn execute(&self, values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {
|
2021-12-26 18:14:03 +00:00
|
|
|
for i in 0..crate::LED_SIZE {
|
2021-12-27 02:42:44 +00:00
|
|
|
let mut color: RawColor = (*values)[i];
|
2021-12-26 18:14:03 +00:00
|
|
|
if self.r.is_some() {
|
2021-12-27 02:49:43 +00:00
|
|
|
color[2] = self.r.unwrap();
|
2021-12-26 18:14:03 +00:00
|
|
|
}
|
|
|
|
if self.g.is_some() {
|
2021-12-27 02:49:43 +00:00
|
|
|
color[1] = self.g.unwrap();
|
2021-12-26 18:14:03 +00:00
|
|
|
}
|
|
|
|
if self.b.is_some() {
|
|
|
|
color[0] = self.b.unwrap();
|
|
|
|
}
|
2021-12-27 02:42:44 +00:00
|
|
|
(*values)[i] = color;
|
2021-12-26 18:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
|
2021-12-28 22:51:13 +00:00
|
|
|
let param1 = if args.len() >= 1 {
|
2021-12-28 22:49:16 +00:00
|
|
|
match args[1].parse::<u8>() {
|
|
|
|
Ok(i) => Some(i),
|
|
|
|
Err(_) => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2021-12-28 22:52:51 +00:00
|
|
|
};
|
2021-12-28 22:51:13 +00:00
|
|
|
let param2 = if args.len() >= 2 {
|
2021-12-28 22:49:16 +00:00
|
|
|
match args[2].parse::<u8>() {
|
|
|
|
Ok(i) => Some(i),
|
|
|
|
Err(_) => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2021-12-28 22:52:51 +00:00
|
|
|
};
|
2021-12-28 22:51:13 +00:00
|
|
|
let param3 = if args.len() >= 3 {
|
2021-12-28 22:49:16 +00:00
|
|
|
match args[3].parse::<u8>() {
|
|
|
|
Ok(i) => Some(i),
|
|
|
|
Err(_) => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2021-12-28 22:52:51 +00:00
|
|
|
};
|
2021-12-28 22:49:16 +00:00
|
|
|
if param1.is_some() && param2.is_some() && param3.is_some() {
|
2021-12-26 18:14:03 +00:00
|
|
|
Ok(Value{
|
2021-12-28 22:49:16 +00:00
|
|
|
r: param1,
|
|
|
|
g: param2,
|
|
|
|
b: param3
|
2021-12-26 18:14:03 +00:00
|
|
|
})
|
|
|
|
}
|
2021-12-28 22:49:16 +00:00
|
|
|
else if args.len() >= 2 {
|
2021-12-26 18:14:03 +00:00
|
|
|
match param2 {
|
2021-12-28 22:49:16 +00:00
|
|
|
Some(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-28 22:49:16 +00:00
|
|
|
_ => Err("no rgb".into())
|
2021-12-26 18:14:03 +00:00
|
|
|
},
|
2021-12-28 22:49:16 +00:00
|
|
|
None => Err("no param2".into())
|
2021-12-26 18:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-28 22:49:16 +00:00
|
|
|
else {
|
|
|
|
Err("incorrect number of arguments".into())
|
|
|
|
}
|
2021-12-26 18:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 22:37:02 +00:00
|
|
|
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern>, ParseError> {
|
2021-12-28 00:00:42 +00:00
|
|
|
v.try_into()
|
2021-12-02 23:46:52 +00:00
|
|
|
}
|
2021-12-28 00:00:42 +00:00
|
|
|
|
|
|
|
pub fn format_multiline(input: &str) -> Vec<Vec<String>> {
|
|
|
|
input.lines()
|
2021-12-28 00:07:48 +00:00
|
|
|
.map(|x: &str|
|
|
|
|
x.split_whitespace()
|
|
|
|
.map(|y| String::from(y))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
)
|
2021-12-28 00:00:42 +00:00
|
|
|
.collect()
|
|
|
|
}
|