add fillRandom pattern

This commit is contained in:
Tobias Ollive
2022-03-02 18:47:03 +01:00
parent 20983a8c08
commit 3d8adc0d52
6 changed files with 191 additions and 97 deletions

View File

@@ -3,7 +3,6 @@ mod patterns;
mod runner;
mod spectacle;
use crate::patterns::{ColorWipe, Fade, Patterns, PixelColor};
use crate::runner::Runner;
use bluer::gatt::remote::Characteristic;
use patterns::Strip;
@@ -19,44 +18,8 @@ const BASE_STRIP_DATA: [u8; 3] = [0x00, 0x00, 0x01];
#[tokio::main(flavor = "current_thread")]
async fn main() -> bluer::Result<()> {
let pixel = PixelColor {
red: 231,
green: 124,
blue: 0,
};
let fade = Fade::<25> {
current_iteration: 0,
nbr_iterations: 0,
begin_color: pixel,
end_color: Default::default(),
};
let context = runner::Context {
pattern: Patterns::Fade(fade),
period: Duration::from_millis(300),
};
let mut runner = runner::Runner::<25>::new();
runner.push(context);
let context = runner::Context {
pattern: Patterns::ColorWipe(ColorWipe {
current_iteration: 0,
color: Default::default(),
infinite: false,
}),
period: Default::default(),
};
runner.push(context);
let yaml = serde_yaml::to_string(&runner).unwrap();
println!("{}", yaml);
let file = Path::new("spectacle.yml");
let config: Runner<25> = Runner::load_from_file(file);
println!("{:?}", config);
let adapter = bluetooth::create_session().await?;

View File

@@ -1,8 +1,7 @@
use rand::Rng;
use serde_derive::{Deserialize, Serialize};
use std::cmp::{max, min, Ordering};
use std::convert::Infallible;
use std::fmt;
use std::ops::Div;
use std::ops::{Div, Index, IndexMut, SubAssign};
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
@@ -11,6 +10,7 @@ pub(crate) enum Patterns<const N: usize> {
Fade(Fade<N>),
ColorWipe(ColorWipe<N>),
Scanner(Scanner<N>),
FillRandom(FillRandom<N>),
}
impl<const N: usize> Iterator for Patterns<N> {
@@ -22,23 +22,20 @@ impl<const N: usize> Iterator for Patterns<N> {
Patterns::Fade(p) => p.next(),
Patterns::ColorWipe(p) => p.next(),
Patterns::Scanner(p) => p.next(),
Patterns::FillRandom(p) => p.next(),
}
}
}
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
///
/// a struct for an RGB color
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Default)]
pub struct PixelColor {
pub(crate) red: u8,
pub(crate) green: u8,
pub(crate) blue: u8,
}
impl PixelColor {
fn new() -> Self {
Default::default()
}
}
impl fmt::Display for PixelColor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{},{},{}]", self.red, self.green, self.blue)
@@ -57,9 +54,39 @@ impl Div<u8> for PixelColor {
}
}
impl SubAssign for PixelColor {
fn sub_assign(&mut self, rhs: Self) {
println!("self.red {}, red {}", self.red, rhs.red);
self.red = self.red.saturating_sub(rhs.red);
self.green = self.red.saturating_sub(rhs.green);
self.blue = self.red.saturating_sub(rhs.blue);
}
}
///
/// a basic newtype based on a fix array size.
///
#[derive(Copy, Clone)]
pub struct Strip<const N: usize> {
pub strip: [PixelColor; N],
pub struct Strip<const N: usize>([PixelColor; N]);
impl<const N: usize> Default for Strip<N> {
fn default() -> Self {
Strip([PixelColor::default(); N])
}
}
impl<const N: usize> Index<usize> for Strip<N> {
type Output = PixelColor;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
impl<const N: usize> IndexMut<usize> for Strip<N> {
fn index_mut(&mut self, index: usize) -> &mut PixelColor {
&mut self.0[index]
}
}
impl<const N: usize> Strip<N> {
@@ -67,30 +94,26 @@ impl<const N: usize> Strip<N> {
let mut data: Vec<u8> = vec![];
for i in 0..N {
data.append(&mut vec![
self.strip[i].green,
self.strip[i].red,
self.strip[i].blue,
]);
data.append(&mut vec![self[i].green, self[i].red, self[i].blue]);
}
data
}
pub fn fill(&mut self, color: PixelColor) {
self.strip = [color; N];
self.0.fill(color);
}
}
impl<const N: usize> Default for Strip<N> {
fn default() -> Self {
Strip {
strip: [PixelColor::new(); N],
}
}
}
/// Rainbow pattern /////////////////////////////////////////////////
/// # Rainbow pattern
///
/// every pattern implement the iterator trait
///
/// This pattern display a moving rainbow over the strip
///
/// ### Note
///
/// If max iteration is let to None, it's an infinite pattern.
///
#[derive(Serialize, Deserialize, Debug)]
pub struct Rainbow<const N: usize> {
pub(crate) current_iteration: usize,
@@ -111,13 +134,21 @@ impl<const N: usize> Iterator for Rainbow<N> {
let step = self.step.unwrap_or(255 / N);
for i in 0..N {
let pos = (i * step + self.current_iteration) as u8;
strip.strip[i] = wheel(pos)
strip[i] = wheel(pos)
}
self.current_iteration += 1;
Some(strip)
}
}
/// compute **rgb** pixel color according to the **hsv** wheel
///
/// # Arguments
///
/// * `index`: position in the hsv wheel
///
/// returns: PixelColor
///
fn wheel(index: u8) -> PixelColor {
let pos = 255 - index;
match pos {
@@ -145,15 +176,21 @@ fn wheel(index: u8) -> PixelColor {
}
}
//////////////////////////////////////////////////////////////////////////
/// Colorwipe pattern ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/// # Colorwipe pattern
/// every pattern implement the iterator trait
///
/// This pattern fill the strip with a specific color, one by one.
///
/// ### Note
///
/// setting max_iteration to None lead the pattern to loop infinitely
///
#[derive(Serialize, Deserialize, Debug)]
pub struct ColorWipe<const N: usize> {
pub(crate) current_iteration: usize,
pub(crate) max_iteration: Option<usize>,
pub(crate) color: PixelColor,
pub(crate) infinite: bool,
pub(crate) background_color: Option<PixelColor>,
}
impl<const N: usize> Iterator for ColorWipe<N> {
@@ -161,24 +198,26 @@ impl<const N: usize> Iterator for ColorWipe<N> {
fn next(&mut self) -> Option<Self::Item> {
let mut strip = Strip::default();
if self.infinite {
self.current_iteration %= N;
} else if self.current_iteration >= N {
return None;
if let Some(c) = self.background_color {
strip.fill(c);
}
let iteration = self.current_iteration % N;
if let Some(max_iteration) = self.max_iteration {
if self.current_iteration >= max_iteration {
return None;
}
}
for i in 0..self.current_iteration {
strip.strip[i] = self.color;
for i in 0..iteration {
strip[i] = self.color;
}
self.current_iteration += 1;
Some(strip)
}
}
//////////////////////////////////////////////////////////////////////////
/// fade pattern ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/// # fade pattern
/// fade from one color to an other
#[derive(Serialize, Deserialize, Debug)]
pub struct Fade<const N: usize> {
pub(crate) current_iteration: usize,
@@ -229,10 +268,12 @@ impl<const N: usize> Iterator for Fade<N> {
}
}
//////////////////////////////////////////////////////////////////////////
/// scanner pattern ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/// # scanner pattern
/// color one pixel with a color and leave a train of this color fading to black
///
/// # note
///
/// background_color work only for color not set by scanner pattern
#[derive(Serialize, Deserialize, Debug)]
pub struct Scanner<const N: usize> {
pub(crate) current_iteration: usize,
@@ -260,11 +301,58 @@ impl<const N: usize> Iterator for Scanner<N> {
}
let mut c = self.color;
for i in min_led..current_led {
strip.strip[i] = c;
strip[i] = c;
c = c / 2;
println!("{}", c);
}
self.current_iteration += 1;
Some(strip)
}
}
/// # FillRandom
///
/// fill strip with color then apply random variation
#[derive(Serialize, Deserialize, Debug)]
pub struct FillRandom<const N: usize> {
pub(crate) color: PixelColor,
pub(crate) stability: usize,
pub(crate) max_iteration: Option<usize>,
}
impl<const N: usize> Iterator for FillRandom<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(iteration) = self.max_iteration {
if iteration == 0 {
return None;
} else {
self.max_iteration = Some(iteration - 1);
}
}
let mut strip = Strip::<N>::default();
let mut rng = rand::thread_rng();
for i in 0..N {
let red_delta = rng.gen_range(0..self.stability) as u8;
let green_delta = rng.gen_range(0..self.stability) as u8;
let blue_delta = rng.gen_range(0..self.stability) as u8;
let operation = rng.gen_bool(0.5);
let red;
let green;
let blue;
if operation {
red = self.color.red.saturating_add(red_delta);
green = self.color.green.saturating_add(green_delta);
blue = self.color.blue.saturating_add(blue_delta);
} else {
red = self.color.red.saturating_sub(red_delta);
green = self.color.green.saturating_sub(green_delta);
blue = self.color.blue.saturating_sub(blue_delta);
}
let c = PixelColor { red, green, blue };
strip[i] = c;
}
Some(strip)
}
}

View File

@@ -0,0 +1 @@