From 7987c1796cc05ca903a9076288bb212520b5f89f Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 12 Aug 2021 00:51:58 +0100 Subject: [PATCH] Cleanup code --- src/color.rs | 16 ++++----------- src/pattern/orb.rs | 49 +++++++++++++++++++++++++++++++++------------- src/webui.rs | 10 +++------- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/color.rs b/src/color.rs index 4bcd0d4..d1ef2c0 100644 --- a/src/color.rs +++ b/src/color.rs @@ -96,19 +96,11 @@ pub fn merge_colors(from_color: Rgb, to_color: Rgb, factor: f32) -> Rgb { /// Builds a color ramp of length `length` of the (exclusive) bounds of `from_color` to `to_color` pub fn build_ramp(from_color: Rgb, to_color: Rgb, length: usize) -> Vec { - //let diff = i16::from(to_color) - i16::from(from_color); + let offset = 1.0f32 / (length as f32 + 1.0f32); let mut ret: Vec = vec![]; - // for i in 1..=length { - // () - // } - - // ret - // let (r_step,g_step,b_step) = ( - // (to_color.r - from_color.r).div_euclid(length), - // (to_color.g - from_color.g).div_euclid(length), - // (to_color.b - from_color.b).div_euclid(length), - // ); - // step = 255 + for step in 1..=length { + ret.push(merge_colors(from_color, to_color, offset * step as f32)); + } ret } diff --git a/src/pattern/orb.rs b/src/pattern/orb.rs index 585f7b4..ed3df14 100644 --- a/src/pattern/orb.rs +++ b/src/pattern/orb.rs @@ -1,5 +1,6 @@ use super::Pattern; -use crate::color::Rgb; +use crate::color::{self, Rgb}; +use std::iter; #[derive(Clone, Debug)] pub struct Orb { @@ -9,6 +10,8 @@ pub struct Orb { backoff_width: u8, total_width: u8, bounces: bool, + step: isize, + direction: bool, } impl Orb { pub const fn new(color: Rgb, center_width: u8, backoff_width: u8) -> Self { @@ -19,28 +22,46 @@ impl Orb { backoff_width, total_width: center_width + backoff_width * 2, bounces: false, + step: 0, + direction: true, } } } impl Pattern for Orb { fn step(&mut self) -> Result { - self.lights_buf.rotate_right(1); + if self.direction { + self.lights_buf.rotate_right(1); + self.step += 1; + } else { + self.lights_buf.rotate_left(1); + self.step -= 1; + } + + if step == 0 { + self.direction = true; + } else if self.lights_buf - self.step == self.total_width { + self.direction = false; + } + Ok(true) } fn init(&mut self, num_lights: u16) -> Result<(), ()> { - // if num_lights < 1 || num_lights > 255 { - // return Err(()); - // } - - // self.lights_buf = - - // self.lights_buf = RAINBOW - // .iter() - // .flat_map(|&x| iter::repeat(x).take(self.width.into())) - // .cycle() - // .take(buf_length.into()) - // .collect(); + if num_lights < 1 { + return Err(()); + } + self.step = 0; + let other_color = color::BLACK; + let ramp = color::build_ramp(other_color, self.color, self.backoff_width.into()); + self.lights_buf = iter::empty::() + .chain(ramp.clone().into_iter()) + .chain(iter::repeat(self.color).take(self.center_width.into())) + .chain(ramp.clone().into_iter().rev()) + .chain( + iter::repeat(other_color) + .take(num_lights.saturating_sub(self.total_width.into()).into()), + ) + .collect(); Ok(()) } fn get_strip(&self) -> &[Rgb] { diff --git a/src/webui.rs b/src/webui.rs index d0f2548..2e8f969 100644 --- a/src/webui.rs +++ b/src/webui.rs @@ -1,15 +1,11 @@ -use crate::color::Rgb; use crate::pattern; use crate::strip; -use actix_web::web::{Form, JsonConfig}; -use actix_web::{get, post, web, App, HttpRequest, HttpServer, Responder, Result}; +// use actix_web::web::JsonConfig; +use actix_web::{post, web, App, HttpServer, Responder, Result}; // use actix_web::error::InternalError; use actix_web_static_files; -use pattern::{Pattern, PatternParameters}; -use rust_embed::RustEmbed; -use serde::Deserialize; +use pattern::PatternParameters; use std::io; -use std::str::FromStr; use std::sync::{Arc, Mutex}; include!(concat!(env!("OUT_DIR"), "/generated.rs"));