Cleanup code

This commit is contained in:
Your Name 2021-08-12 00:51:58 +01:00
parent 1ea40ad4c6
commit 7987c1796c
3 changed files with 42 additions and 33 deletions

View File

@ -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<Rgb> {
//let diff = i16::from(to_color) - i16::from(from_color);
let offset = 1.0f32 / (length as f32 + 1.0f32);
let mut ret: Vec<Rgb> = 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
}

View File

@ -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<bool, ()> {
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::<Rgb>()
.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] {

View File

@ -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"));