possibly pattern parsing?

This commit is contained in:
jane 2021-12-26 13:14:03 -05:00
parent 635dfd9467
commit 94662eda45
3 changed files with 103 additions and 7 deletions

View File

@ -1,6 +1,7 @@
pub mod util;
use util::lights::*;
use util::pattern::*;
use std::sync::{Arc, RwLock};
use rs_ws281x::{RawColor, Controller, ControllerBuilder, ChannelBuilder, StripType};
use std::{thread, time};
@ -32,14 +33,11 @@ fn main() {
let mut x: u8 = 0;
loop {
thread::sleep(time::Duration::from_millis(250));
if x == 255 {
x = 0;
}
let mut color: RawColor = [x, x, x, 150];
let mut lights = lock.write().unwrap();
for i in 0..LED_SIZE {
lights[i] = color;
let pattern = vec!["val".into(), "255".into(), "150".into(), "0".into()];
let res = parse_line(pattern);
if res.is_ok() {
res.unwrap().execute(&mut lights);
}
x += 1;
}
}

View File

@ -1,3 +1,4 @@
pub mod lights;
pub mod parser;
pub mod pattern;
pub mod webserver;

97
src/util/pattern.rs Normal file
View File

@ -0,0 +1,97 @@
use rs_ws281x::RawColor;
use std::{
vec::Vec,
string::String
};
type ParseError = Box<dyn std::error::Error>;
trait Pattern {
fn execute(&self, values: &mut [RawColor]);
fn parse(args: Vec<String>) -> Result<Self, ParseError>;
}
impl TryFrom<Vec<String>> for Box<dyn Pattern> {
fn try_from(s: Vec<String>) -> Result<Self, ParseError> {
match s[0].as_str() {
"unit" => Unit::parse(s)
"val" => Value::parse(s)
_ => Err("No Match".into())
}
}
}
struct Unit;
impl<'a> Pattern for Unit<'a> {
fn execute(&self, values: &'a mut [RawColor]) {}
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
Ok(Unit {})
}
}
struct Value {
r: Option<u8>,
g: Option<u8>,
b: Option<u8>
};
impl<'a> Pattern for Value<'a> {
fn execute(&self, values: &'a mut [RawColor]) {
for i in 0..crate::LED_SIZE {
let 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]
}
}
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
let param1 = params[1].parse::<u8>();
let param2 = params[2].parse::<u8>();
let param3 = params[2].parse::<u8>();
if param1.is_ok() && param2.is_ok() && param3.is_ok() {
Ok(Value{
r: param1.unwrap(),
g: param2.unwrap(),
b: param3.unwrap()
})
}
else {
match param2 {
Ok(i) => match params[1].as_str() {
"r" => {
Ok(i) => Ok(Value {
r: Some(i),
g: None,
b: None
})
},
"g" => {
Ok(Value {
r: None,
g: Some(i),
b: None
})
},
"b" => {
Ok(i) => Ok(Value {
r: None,
g: None,
b: Some(i)
})
}
},
Err() => Err()
}
}
}
}
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern>, ParseError> {
}