leds/src/util/pattern.rs

212 lines
5.7 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
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-29 02:10:00 +00:00
pub trait Pattern: Send {
2021-12-29 02:45:00 +00:00
fn execute(&self, values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>, ticks: u64);
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-30 03:57:36 +00:00
"rand" => Ok(Box::new(Random::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 {
2021-12-29 02:46:48 +00:00
fn execute(&self, _values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>, _ticks: u64) {}
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 {
2021-12-29 02:46:48 +00:00
fn execute(&self, values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>, _ticks: u64) {
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:55:37 +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:55:37 +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:55:37 +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-30 03:57:36 +00:00
struct Random {
r: bool,
g: bool,
b: bool,
min: u8,
max: u8
}
impl Pattern for Random {
fn execute(&self, values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>, _ticks: u64) {
use rand::prelude::*;
let mut rng = rand::thread_rng();
for x in 0..values.len() {
let mut color = (*values)[x];
if self.r {
let y: f64 = rng.gen();
color[2] = ((y * (max - min))+min) as u8;
}
if self.g {
let y: f64 = rng.gen();
color[1] = ((y * (max - min))+min) as u8;
}
if self.b {
let y: f64 = rng.gen();
color[0] = ((y * (max - min))+min) as u8;
}
}
}
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
let end = args.len() - 1;
let e = args[end].parse::<u8>();
let en = args[end-1].parse::<u8>();
let mut r = false;
let mut g = false;
let mut b = false;
let (min, max) = match en {
Ok(i) => {
match e {
Ok(j) => (i, j),
Err(_) => (0, 255)
}
},
Err(_) => {
match e {
Ok(i) => (0, i),
Err(_) => (0, 255)
}
}
};
for x in 1..end {
match args[x].as_str() {
"r" => {r = true;},
"g" => {g = true;},
"b" => {b = true;},
_ => {}
}
}
Ok(Random {
r: r,
g: g,
b: b,
min: min,
max: max
})
}
}
struct Rainbow {
offset_r: Option<usize>,
offset_g: Option<usize>,
offset_b: Option<usize>
}
// todo
2021-12-28 22:37:02 +00:00
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern>, ParseError> {
2021-12-28 22:53:14 +00:00
println!("{:?}", v);
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()
2021-12-28 22:53:14 +00:00
}