This commit is contained in:
jane 2021-12-26 20:26:29 -05:00
parent 94662eda45
commit ac08d4d3a4
2 changed files with 16 additions and 14 deletions

View file

@ -1,10 +1,10 @@
use rs_ws281x::RawColor; use rs_ws281x::RawColor;
use rs_ws281x::Controller; use rs_ws281x::Controller;
use rs_ws281x::WS2811Error; use rs_ws281x::WS2811Error;
use std::{thread, time}; // use std::{thread, time};
pub fn run_lights(controller: &mut Controller, values: &[RawColor; crate::LED_SIZE]) -> Result<(), WS2811Error> { pub fn run_lights(controller: &mut Controller, values: &[RawColor; crate::LED_SIZE]) -> Result<(), WS2811Error> {
println!("Value: {:?}", values[0]); // println!("Value: {:?}", values[0]);
//thread::sleep(time::Duration::from_millis(10)); //thread::sleep(time::Duration::from_millis(10));
let channels: Vec<usize> = controller.channels(); let channels: Vec<usize> = controller.channels();
let mut strip = controller.leds_mut(channels[0]); let mut strip = controller.leds_mut(channels[0]);

View file

@ -6,16 +6,16 @@ use std::{
type ParseError = Box<dyn std::error::Error>; type ParseError = Box<dyn std::error::Error>;
trait Pattern { trait Pattern<'a> {
fn execute(&self, values: &mut [RawColor]); fn execute(&self, values: &'a mut [RawColor]);
fn parse(args: Vec<String>) -> Result<Self, ParseError>; fn parse(args: Vec<String>) -> Result<Self, ParseError>;
} }
impl TryFrom<Vec<String>> for Box<dyn Pattern> { impl TryFrom<Vec<String>> for Box<dyn Pattern> {
fn try_from(s: Vec<String>) -> Result<Self, ParseError> { fn try_from(s: Vec<String>) -> Result<Self, ParseError> {
match s[0].as_str() { match s[0].as_str() {
"unit" => Unit::parse(s) "unit" => Unit::parse(s),
"val" => Value::parse(s) "val" => Value::parse(s),
_ => Err("No Match".into()) _ => Err("No Match".into())
} }
} }
@ -33,7 +33,8 @@ struct Value {
r: Option<u8>, r: Option<u8>,
g: Option<u8>, g: Option<u8>,
b: Option<u8> b: Option<u8>
}; }
impl<'a> Pattern for Value<'a> { impl<'a> Pattern for Value<'a> {
fn execute(&self, values: &'a mut [RawColor]) { fn execute(&self, values: &'a mut [RawColor]) {
for i in 0..crate::LED_SIZE { for i in 0..crate::LED_SIZE {
@ -51,9 +52,9 @@ impl<'a> Pattern for Value<'a> {
} }
} }
fn parse(args: Vec<String>) -> Result<Self, ParseError> { fn parse(args: Vec<String>) -> Result<Self, ParseError> {
let param1 = params[1].parse::<u8>(); let param1 = args[1].parse::<u8>();
let param2 = params[2].parse::<u8>(); let param2 = args[2].parse::<u8>();
let param3 = params[2].parse::<u8>(); let param3 = args[2].parse::<u8>();
if param1.is_ok() && param2.is_ok() && param3.is_ok() { if param1.is_ok() && param2.is_ok() && param3.is_ok() {
Ok(Value{ Ok(Value{
r: param1.unwrap(), r: param1.unwrap(),
@ -63,9 +64,9 @@ impl<'a> Pattern for Value<'a> {
} }
else { else {
match param2 { match param2 {
Ok(i) => match params[1].as_str() { Ok(i) => match args[1].as_str() {
"r" => { "r" => {
Ok(i) => Ok(Value { Ok(Value {
r: Some(i), r: Some(i),
g: None, g: None,
b: None b: None
@ -79,7 +80,7 @@ impl<'a> Pattern for Value<'a> {
}) })
}, },
"b" => { "b" => {
Ok(i) => Ok(Value { Ok(Value {
r: None, r: None,
g: None, g: None,
b: Some(i) b: Some(i)
@ -93,5 +94,6 @@ impl<'a> Pattern for Value<'a> {
} }
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern>, ParseError> { pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern>, ParseError> {
let res: Result<Box<dyn Pattern>, ParseError> = v.try_into();
res
} }