diff --git a/Cargo.toml b/Cargo.toml index 707fb30..eca6fad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ serde = {version = "1.0", features = ["derive"]} actix-web = {version = "3", default_features = false} rust-embed="6.0.0" hex = "0.4.3" +serde_json = "1" [target.armv7-unknown-linux-gnueabihf] linker = "armv7-unknown-linux-gnueabihf" diff --git a/src/color.rs b/src/color.rs index 23e7965..b4f7918 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,8 +1,9 @@ use hex; use std::num::ParseIntError; use std::str::FromStr; +use serde::Deserialize; -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Deserialize)] pub struct Rgb(pub u8, pub u8, pub u8); impl Rgb { pub const fn to_tuple(self) -> (u8, u8, u8) { diff --git a/src/pattern.rs b/src/pattern.rs index e95cf6f..a3047b3 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -1,4 +1,5 @@ use crate::color::Rgb; +use serde::Deserialize; pub mod collide; pub mod fade; @@ -13,6 +14,20 @@ pub use moving_rainbow::MovingRainbow; pub use orb::Orb; pub use solid::Solid; +#[derive(Deserialize, Debug)] +enum PatternParameters { + Collide(Rgb, Rgb, Rgb), +} + +pub enum PatternParameters { + Collide(Rgb,Rgb,Rgb), + Fade(Rgb), + MovingPixel(Rgb), + MovingRainbow(u8, bool), + Orb(Rgb, u8,u8), + Solid(Rgb), +} + pub trait Pattern: std::fmt::Debug { fn init(&mut self, num_lights: u16) -> Result<(), ()>; fn step(&mut self) -> Result; diff --git a/src/webui.rs b/src/webui.rs index 8ccdf20..4ec720c 100644 --- a/src/webui.rs +++ b/src/webui.rs @@ -2,11 +2,13 @@ use crate::color::Rgb; use crate::pattern; use crate::strip; use actix_web::web::Form; -use actix_web::{get, post, web, App, HttpRequest, HttpServer, Responder}; +use actix_web::{get, post, web, App, HttpRequest, HttpServer, Responder, Result, }; +// use actix_web::error::InternalError; use rust_embed::RustEmbed; use serde::Deserialize; use std::str::FromStr; use std::sync::{Arc, Mutex}; +use std::io; use std::sync::mpsc::Sender; @@ -19,21 +21,26 @@ struct AppState { strip_tx: Arc>>, } -#[derive(Deserialize, Debug)] -struct ColorForm { - color: String, -} +// #[derive(Deserialize, Debug)] +// struct ColorForm { +// color: String, +// e: E +// } #[post("/setcolor")] -async fn set_color(data: web::Data, params: Form) -> impl Responder { +async fn set_color(data: web::Data, params: web::Json) -> Result { + println!("{:?}", params); + let pattern = match params.0 { + PatternParameters::Collide(l, r, c) => pattern::Collide::new(l,r,c) + }; data.strip_tx - .lock() - .unwrap() + .lock().or(Err(io::Error::new(io::ErrorKind::Other, "Failed to get a lock")))? .send(strip::Message::ChangePattern(Box::new( - pattern::Solid::new(Rgb::from_str(¶ms.color).unwrap()), + // pattern::Solid::new(Rgb::from_str(¶ms.color).or(Err(io::Error::new(io::ErrorKind::Other, "Failed to parse")))?), + pattern ))) - .unwrap(); - format!("{:?}", params) + .or(Err(io::Error::new(io::ErrorKind::Other, "Failed to send to channel")))?; + Ok(format!("{:?}", params)) } #[get("/")]