add random stars pattern

fix wrong multi devices selection
add color section to simplify devices section
This commit is contained in:
Tobias Ollive 2022-03-15 16:18:29 +01:00
parent 13326a49d9
commit 6deb31836f
18 changed files with 240 additions and 218 deletions

View File

@ -1,39 +1,46 @@
colors:
- &red
red: 255
green: 0
blue: 0
devices:
- name : toto - name : toto
mac_addresses : mac_addresses :
- C9:81:9C:BA:53:BC - C9:81:9C:BA:53:BC
sequence: sequence:
# - pattern:
# type: Blink
# fade:
# current_iteration: 10
# nbr_iterations: 20
# begin_color: &black
# red: 0
# green: 0
# blue: 0
# end_color: &purple
# red: 255
# green: 0
# blue: 255
# max_iteration: 10
# period: 50
- pattern: - pattern:
type: Rain type: Rain
color: &blue color: &blue
red: 0 red: 0
green: 0 green: 0
blue: 255 blue: 255
background_color: &black background_color: &yellow
red: 0 red: 0
green: 255 green: 255
blue: 0 blue: 255
stability: 10 stability: 10
lines: 5 lines: 5
period: 200 period: 200
- pattern:
type: Blink
fade:
current_iteration: 10
steps: 20
begin_color: &black
red: 0
green: 0
blue: 0
end_color: &purple
red: 255
green: 0
blue: 255
max_iteration: 10
period: 50
- pattern: - pattern:
type: Fade type: Fade
steps: 20 steps: 20
begin_color: &red begin_color:
red: 255 red: 255
green: 0 green: 0
blue: 0 blue: 0
@ -55,11 +62,15 @@
- name: titi - name: titi
sequence: sequence:
- pattern: - pattern:
type: RingScanner type: StarsRandom
color: *green color: *green
ring_size: 4 ratio: 8
max_iteration : 50 steps: 20
period: 100 background_color:
red: 0
green: 255
blue: 100
period: 50
- pattern: - pattern:
type: Rainbow type: Rainbow
max_iteration: 200 max_iteration: 200

View File

@ -1,9 +1,15 @@
use crate::patterns::PixelColor;
use crate::runner::DeviceSequence; use crate::runner::DeviceSequence;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::collections::HashSet; use std::collections::HashSet;
use std::fs; use std::fs;
pub(crate) type Config<const N: usize> = Vec<Device<N>>;
#[derive(Serialize, Deserialize)]
pub struct Config<const N: usize> {
devices: Vec<Device<N>>,
colors: Vec<PixelColor>,
}
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct Device<const N: usize> { pub struct Device<const N: usize> {
@ -12,8 +18,8 @@ pub struct Device<const N: usize> {
pub sequence: DeviceSequence<N>, pub sequence: DeviceSequence<N>,
} }
pub fn load_from_file<const N: usize>(file: &std::path::Path) -> Config<N> { pub fn load_from_file<const N: usize>(file: &std::path::Path) -> Vec<Device<N>> {
let file = fs::read_to_string(file).unwrap(); let file = fs::read_to_string(file).unwrap();
let config: Config<N> = serde_yaml::from_str(&*file).unwrap(); let config: Config<N> = serde_yaml::from_str(&*file).unwrap();
config config.devices
} }

View File

@ -6,10 +6,9 @@ mod patterns;
mod runner; mod runner;
mod spectacle; mod spectacle;
use pattern_derive;
use crate::args::Cli; use crate::args::Cli;
use crate::audio::play_sound; use crate::audio::play_sound;
use crate::config::{load_from_file, Config}; use crate::config::load_from_file;
use crate::runner::Spectacle; use crate::runner::Spectacle;
use bluer::gatt::remote::Characteristic; use bluer::gatt::remote::Characteristic;
use bluer::{Device, Error}; use bluer::{Device, Error};
@ -30,7 +29,7 @@ async fn main() -> bluer::Result<()> {
let command_line = Cli::parse(); let command_line = Cli::parse();
let file = Path::new(&command_line.spectacle_file); let file = Path::new(&command_line.spectacle_file);
let config: Config<25> = load_from_file(file); let config = load_from_file(file);
let spectacle = Spectacle::from(config); let spectacle = Spectacle::from(config);
let spectacle = Arc::new(Mutex::new(spectacle)); let spectacle = Arc::new(Mutex::new(spectacle));
@ -87,7 +86,6 @@ async fn connect_device(
}; };
} }
println!("device {} disconnected", &device.address()); println!("device {} disconnected", &device.address());
// drop(rx_device);
}); });
} }
} }

View File

@ -10,6 +10,7 @@ mod rainbow;
mod ring; mod ring;
mod ring_scanner; mod ring_scanner;
mod scanner; mod scanner;
mod stars_random;
use crate::patterns::blink::Blink; use crate::patterns::blink::Blink;
use crate::patterns::color_wipe::ColorWipe; use crate::patterns::color_wipe::ColorWipe;
@ -23,11 +24,12 @@ use crate::patterns::rainbow::Rainbow;
use crate::patterns::ring::Ring; use crate::patterns::ring::Ring;
use crate::patterns::ring_scanner::RingScanner; use crate::patterns::ring_scanner::RingScanner;
use crate::patterns::scanner::Scanner; use crate::patterns::scanner::Scanner;
use crate::patterns::stars_random::StarsRandom;
use rand::rngs::ThreadRng; use rand::rngs::ThreadRng;
use rand::Rng; use rand::Rng;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::ops::{AddAssign, Div, Index, IndexMut, SubAssign}; use std::ops::{Add, AddAssign, Div, Index, IndexMut, Sub, SubAssign};
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)] #[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)]
#[serde(tag = "type")] #[serde(tag = "type")]
@ -44,6 +46,7 @@ pub(crate) enum Patterns<const N: usize> {
FadeUnstable(FadeUnstable<N>), FadeUnstable(FadeUnstable<N>),
Ring(Ring<N>), Ring(Ring<N>),
RingScanner(RingScanner<N>), RingScanner(RingScanner<N>),
StarsRandom(StarsRandom<N>),
} }
pub trait Pattern: Iterator { pub trait Pattern: Iterator {
@ -52,31 +55,38 @@ pub trait Pattern: Iterator {
fn init(&mut self) -> Option<Self::Strip> { fn init(&mut self) -> Option<Self::Strip> {
None None
} }
fn is_last_iteration(&self) -> bool ; fn is_last_iteration(&self) -> bool;
} }
#[macro_export] #[macro_export]
macro_rules! impl_pattern_none { macro_rules! impl_pattern_init_background {
($name:ty, $generic: tt) => { ($generic: tt) => {
type Strip = Strip<N>; fn init(&mut self) -> Option<Self::Strip> {
if let Some(color) = self.background_color {
fn init(&mut self) -> Option<Self::Strip> { Some(Strip::<N>::new(color))
} else {
None None
} }
}
};
}
fn is_last_iteration(&self) -> bool { #[macro_export]
match self.max_iteration { macro_rules! impl_pattern_last_iteration {
None => false, ($name:ty) => {
Some(max_iter) => { fn is_last_iteration(&self) -> bool {
if self.current_iteration >= max_iter { match self.max_iteration {
true None => false,
} else { Some(max_iter) => {
false if self.current_iteration >= max_iter {
} true
} else {
false
} }
} }
} }
} }
};
} }
impl<const N: usize> Iterator for Patterns<N> { impl<const N: usize> Iterator for Patterns<N> {
@ -96,6 +106,7 @@ impl<const N: usize> Iterator for Patterns<N> {
Patterns::FadeUnstable(p) => p.next(), Patterns::FadeUnstable(p) => p.next(),
Patterns::Ring(p) => p.next(), Patterns::Ring(p) => p.next(),
Patterns::RingScanner(p) => p.next(), Patterns::RingScanner(p) => p.next(),
Patterns::StarsRandom(p) => p.next(),
} }
} }
} }
@ -117,6 +128,7 @@ impl<const N: usize> Pattern for Patterns<N> {
Patterns::FadeUnstable(p) => p.init(), Patterns::FadeUnstable(p) => p.init(),
Patterns::Ring(p) => p.init(), Patterns::Ring(p) => p.init(),
Patterns::RingScanner(p) => p.init(), Patterns::RingScanner(p) => p.init(),
Patterns::StarsRandom(p) => p.init(),
} }
} }
@ -134,6 +146,7 @@ impl<const N: usize> Pattern for Patterns<N> {
Patterns::FadeUnstable(p) => p.is_last_iteration(), Patterns::FadeUnstable(p) => p.is_last_iteration(),
Patterns::Ring(p) => p.is_last_iteration(), Patterns::Ring(p) => p.is_last_iteration(),
Patterns::RingScanner(p) => p.is_last_iteration(), Patterns::RingScanner(p) => p.is_last_iteration(),
Patterns::StarsRandom(p) => p.is_last_iteration(),
} }
} }
} }
@ -165,6 +178,30 @@ impl Div<u8> for PixelColor {
} }
} }
impl Add for PixelColor {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
PixelColor {
red: self.red.saturating_add(rhs.red),
green: self.green.saturating_add(rhs.green),
blue: self.blue.saturating_add(rhs.blue),
}
}
}
impl Sub<u8> for PixelColor {
type Output = Self;
fn sub(self, rhs: u8) -> Self::Output {
PixelColor {
red: self.red.saturating_sub(rhs),
green: self.green.saturating_sub(rhs),
blue: self.blue.saturating_sub(rhs),
}
}
}
impl SubAssign for PixelColor { impl SubAssign for PixelColor {
fn sub_assign(&mut self, rhs: Self) { fn sub_assign(&mut self, rhs: Self) {
self.red = self.red.saturating_sub(rhs.red); self.red = self.red.saturating_sub(rhs.red);
@ -192,23 +229,27 @@ impl PixelColor {
/// returns: PixelColor /// returns: PixelColor
/// ///
pub fn random_delta(&self, stability: usize, rng: &mut ThreadRng) -> PixelColor { pub fn random_delta(&self, stability: usize, rng: &mut ThreadRng) -> PixelColor {
let red_delta = rng.gen_range(0..stability) as u8; let minus_interval = -(stability as i8);
let green_delta = rng.gen_range(0..stability) as u8; let max_interval = stability as i8;
let blue_delta = rng.gen_range(0..stability) as u8; let red_delta = rng.gen_range(minus_interval..max_interval) as i8;
let operation = rng.gen_bool(0.5); let green_delta = rng.gen_range(minus_interval..max_interval) as i8;
let red; let blue_delta = rng.gen_range(minus_interval..max_interval) as i8;
let green; self.delta((red_delta, green_delta, blue_delta))
let blue; }
if operation {
red = self.red.saturating_add(red_delta); pub fn delta(&self, delta: (i8, i8, i8)) -> PixelColor {
green = self.green.saturating_add(green_delta); let (red, green, blue) = delta;
blue = self.blue.saturating_add(blue_delta); PixelColor {
} else { red: (self.red as i16 + red as i16) as u8,
red = self.red.saturating_sub(red_delta); green: (self.green as i16 + green as i16) as u8,
green = self.green.saturating_sub(green_delta); blue: (self.blue as i16 + blue as i16) as u8,
blue = self.blue.saturating_sub(blue_delta);
} }
PixelColor { red, green, blue } }
pub fn is_closer(&self, pixel: PixelColor, distance: PixelColor) -> bool {
(self.red as i16 - pixel.red as i16).abs() <= distance.red as i16
&& (self.green as i16 - pixel.green as i16).abs() <= distance.green as i16
&& (self.blue as i16 - pixel.blue as i16).abs() <= distance.blue as i16
} }
} }

View File

@ -1,6 +1,6 @@
use crate::patterns::fade::Fade; use crate::patterns::fade::Fade;
use crate::patterns::Pattern; use crate::patterns::Pattern;
use crate::{impl_pattern_none, Strip}; use crate::{impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Blink /// # Blink
@ -15,7 +15,8 @@ pub struct Blink<const N: usize> {
} }
impl<const N: usize> Pattern for Blink<N> { impl<const N: usize> Pattern for Blink<N> {
impl_pattern_none!(FadeRandom, N); type Strip = Strip<N>;
impl_pattern_last_iteration!(ColorWipe);
} }
impl<const N: usize> Iterator for Blink<N> { impl<const N: usize> Iterator for Blink<N> {

View File

@ -1,5 +1,5 @@
use crate::patterns::{Pattern, PixelColor}; use crate::patterns::{Pattern, PixelColor};
use crate::Strip; use crate::{impl_pattern_init_background, impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Colorwipe pattern /// # Colorwipe pattern
@ -22,27 +22,8 @@ pub struct ColorWipe<const N: usize> {
impl<const N: usize> Pattern for ColorWipe<N> { impl<const N: usize> Pattern for ColorWipe<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_init_background!(N);
fn init(&mut self) -> Option<Self::Strip> { impl_pattern_last_iteration!(ColorWipe);
if let Some(color) = self.background_color {
Some(Strip::<N>::new(color))
} else {
None
}
}
fn is_last_iteration(&self) -> bool {
match self.max_iteration {
None => false,
Some(max_iter) => {
if self.current_iteration >= max_iter {
true
} else {
false
}
}
}
}
} }
impl<const N: usize> Iterator for ColorWipe<N> { impl<const N: usize> Iterator for ColorWipe<N> {

View File

@ -20,10 +20,6 @@ fn fade_value(value_start: u8, value_end: u8, index: usize, nbr_iter: usize) ->
impl<const N: usize> Pattern for Fade<N> { impl<const N: usize> Pattern for Fade<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
None
}
fn is_last_iteration(&self) -> bool { fn is_last_iteration(&self) -> bool {
self.current_iteration >= self.steps self.current_iteration >= self.steps
} }

View File

@ -1,6 +1,6 @@
use crate::patterns::fade::Fade; use crate::patterns::fade::Fade;
use crate::patterns::Pattern; use crate::patterns::Pattern;
use crate::{impl_pattern_none, Strip}; use crate::{impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # FadeRandom /// # FadeRandom
@ -16,25 +16,9 @@ pub struct FadeRandom<const N: usize> {
} }
impl<const N: usize> Pattern for FadeRandom<N> { impl<const N: usize> Pattern for FadeRandom<N> {
impl_pattern_none!(FadeRandom, N); type Strip = Strip<N>;
impl_pattern_last_iteration!(ColorWipe);
} }
//
// impl<const N: usize> Pattern for FadeRandom<N> {
// type Strip = Strip<N>;
//
// fn is_last_iteration(&self) -> bool {
// match self.max_iteration {
// None => false,
// Some(max_iter) => {
// if self.current_iteration >= max_iter {
// true
// } else {
// false
// }
// }
// }
// }
// }
impl<const N: usize> Iterator for FadeRandom<N> { impl<const N: usize> Iterator for FadeRandom<N> {
type Item = Strip<N>; type Item = Strip<N>;

View File

@ -1,6 +1,6 @@
use crate::patterns::fade::Fade; use crate::patterns::fade::Fade;
use crate::patterns::Pattern; use crate::patterns::Pattern;
use crate::Strip; use crate::{impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # FadeUnstable /// # FadeUnstable
@ -17,19 +17,7 @@ pub struct FadeUnstable<const N: usize> {
impl<const N: usize> Pattern for FadeUnstable<N> { impl<const N: usize> Pattern for FadeUnstable<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_last_iteration!(ColorWipe);
fn is_last_iteration(&self) -> bool {
match self.max_iteration {
None => false,
Some(max_iter) => {
if self.current_iteration >= max_iter {
true
} else {
false
}
}
}
}
} }
impl<const N: usize> Iterator for FadeUnstable<N> { impl<const N: usize> Iterator for FadeUnstable<N> {

View File

@ -1,5 +1,5 @@
use crate::patterns::{Pattern, PixelColor}; use crate::patterns::{Pattern, PixelColor};
use crate::{impl_pattern_none, Strip}; use crate::{impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # FillRandom /// # FillRandom
@ -15,7 +15,8 @@ pub struct FillRandom<const N: usize> {
} }
impl<const N: usize> Pattern for FillRandom<N> { impl<const N: usize> Pattern for FillRandom<N> {
impl_pattern_none!(FadeRandom, N); type Strip = Strip<N>;
impl_pattern_last_iteration!(ColorWipe);
} }
impl<const N: usize> Iterator for FillRandom<N> { impl<const N: usize> Iterator for FillRandom<N> {

View File

@ -1,5 +1,5 @@
use crate::patterns::{Pattern, PixelColor}; use crate::patterns::{Pattern, PixelColor};
use crate::{impl_pattern_none, Strip}; use crate::{impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # FillUnstable /// # FillUnstable
@ -15,7 +15,8 @@ pub struct FillUnstable<const N: usize> {
} }
impl<const N: usize> Pattern for FillUnstable<N> { impl<const N: usize> Pattern for FillUnstable<N> {
impl_pattern_none!(FadeRandom, N); type Strip = Strip<N>;
impl_pattern_last_iteration!(FillUnstable);
} }
impl<const N: usize> Iterator for FillUnstable<N> { impl<const N: usize> Iterator for FillUnstable<N> {

View File

@ -1,5 +1,5 @@
use crate::patterns::{Pattern, PixelColor}; use crate::patterns::{Pattern, PixelColor};
use crate::Strip; use crate::{impl_pattern_last_iteration, Strip};
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -28,28 +28,15 @@ pub struct Rain<const N: usize> {
impl<const N: usize> Pattern for Rain<N> { impl<const N: usize> Pattern for Rain<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> { fn init(&mut self) -> Option<Self::Strip> {
if let Some(color) = self.background_color { if let Some(color) = self.background_color {
self.drops = Strip::<N>::new(color); self.drops = Strip::<N>::new(color);
Some(Strip::<N>::new(color)) Some(self.drops)
} else { } else {
None None
} }
} }
impl_pattern_last_iteration!(ColorRain);
fn is_last_iteration(&self) -> bool {
match self.max_iteration {
None => false,
Some(max_iter) => {
if self.current_iteration >= max_iter {
true
} else {
false
}
}
}
}
} }
impl<const N: usize> Iterator for Rain<N> { impl<const N: usize> Iterator for Rain<N> {
@ -68,7 +55,7 @@ impl<const N: usize> Iterator for Rain<N> {
} else { } else {
let c = strip[i + self.lines]; let c = strip[i + self.lines];
if self.background_color != c && c != Some(PixelColor::default()) { if self.background_color != c && c != Some(PixelColor::default()) {
strip[i] = Some(strip[i + self.lines].unwrap() / 2); strip[i] = Some(c.unwrap() / 2);
} else { } else {
strip[i] = self.background_color; strip[i] = self.background_color;
} }

View File

@ -1,5 +1,5 @@
use crate::patterns::{Pattern, PixelColor}; use crate::patterns::{Pattern, PixelColor};
use crate::{impl_pattern_none, Strip}; use crate::{impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Rainbow pattern /// # Rainbow pattern
@ -21,7 +21,8 @@ pub struct Rainbow<const N: usize> {
} }
impl<const N: usize> Pattern for Rainbow<N> { impl<const N: usize> Pattern for Rainbow<N> {
impl_pattern_none!(FadeRandom, N); type Strip = Strip<N>;
impl_pattern_last_iteration!(Rainbow);
} }
impl<const N: usize> Iterator for Rainbow<N> { impl<const N: usize> Iterator for Rainbow<N> {

View File

@ -1,5 +1,5 @@
use crate::patterns::{Pattern, PixelColor}; use crate::patterns::{Pattern, PixelColor};
use crate::Strip; use crate::{impl_pattern_init_background, impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Ring /// # Ring
@ -17,23 +17,8 @@ pub struct Ring<const N: usize> {
impl<const N: usize> Pattern for Ring<N> { impl<const N: usize> Pattern for Ring<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_init_background!(N);
fn init(&mut self) -> Option<Self::Strip> { impl_pattern_last_iteration!(Ring);
None
} //FIXME
fn is_last_iteration(&self) -> bool {
match self.max_iteration {
None => false,
Some(max_iter) => {
if self.current_iteration >= max_iter {
true
} else {
false
}
}
}
}
} }
impl<const N: usize> Iterator for Ring<N> { impl<const N: usize> Iterator for Ring<N> {

View File

@ -1,5 +1,5 @@
use crate::patterns::{Pattern, PixelColor}; use crate::patterns::{Pattern, PixelColor};
use crate::Strip; use crate::{impl_pattern_init_background, impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # RingScanner /// # RingScanner
@ -17,23 +17,8 @@ pub struct RingScanner<const N: usize> {
impl<const N: usize> Pattern for RingScanner<N> { impl<const N: usize> Pattern for RingScanner<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_init_background!(N);
fn init(&mut self) -> Option<Self::Strip> { impl_pattern_last_iteration!(ColorWipe);
None
} //FIXME
fn is_last_iteration(&self) -> bool {
match self.max_iteration {
None => false,
Some(max_iter) => {
if self.current_iteration >= max_iter {
true
} else {
false
}
}
}
}
} }
impl<const N: usize> Iterator for RingScanner<N> { impl<const N: usize> Iterator for RingScanner<N> {

View File

@ -1,5 +1,5 @@
use crate::patterns::{Pattern, PixelColor}; use crate::patterns::{Pattern, PixelColor};
use crate::Strip; use crate::{impl_pattern_init_background, impl_pattern_last_iteration, Strip};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # scanner pattern /// # scanner pattern
@ -20,27 +20,8 @@ pub struct Scanner<const N: usize> {
impl<const N: usize> Pattern for Scanner<N> { impl<const N: usize> Pattern for Scanner<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_init_background!(N);
fn init(&mut self) -> Option<Self::Strip> { impl_pattern_last_iteration!(ColorWipe);
if let Some(color) = self.background_color {
Some(Strip::<N>::new(color))
} else {
None
}
}
fn is_last_iteration(&self) -> bool {
match self.max_iteration {
None => false,
Some(max_iter) => {
if self.current_iteration >= max_iter {
true
} else {
false
}
}
}
}
} }
impl<const N: usize> Iterator for Scanner<N> { impl<const N: usize> Iterator for Scanner<N> {

View File

@ -0,0 +1,85 @@
use crate::patterns::{Pattern, PixelColor};
use crate::{impl_pattern_last_iteration, Strip};
use rand::Rng;
use serde::{Deserialize, Serialize};
/// # StarsRandom
///
/// fill strip with background color then color random pixel with other color fading to background color
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)]
pub struct StarsRandom<const N: usize> {
pub(crate) color: PixelColor,
pub(crate) ratio: usize,
pub(crate) max_iteration: Option<usize>,
pub(crate) background_color: Option<PixelColor>,
pub(crate) steps: usize,
#[serde(default)]
pub(crate) current_iteration: usize,
#[serde(skip)]
strip: Strip<N>,
#[serde(skip)]
step: (i8, i8, i8),
}
impl<const N: usize> Pattern for StarsRandom<N> {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
if let Some(color) = self.background_color {
self.strip = Strip::<N>::new(color);
let red = (self.color.red as i16 - color.red as i16) as i8;
let green = (color.green as i16 - self.color.green as i16) as i8;
let blue = (self.color.blue as i16 - color.blue as i16) as i8;
println!("{} {} {}", red, green, blue);
self.step = (
red / self.steps as i8,
green / self.steps as i8,
blue / self.steps as i8,
);
Some(self.strip)
} else {
self.step = (
-((self.color.red / self.steps as u8) as i8),
-((self.color.green / self.steps as u8) as i8),
-((self.color.blue / self.steps as u8) as i8),
);
None
}
}
impl_pattern_last_iteration!(StarsRandom);
}
impl<const N: usize> Iterator for StarsRandom<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
if self.is_last_iteration() {
return None;
}
let mut rng = rand::thread_rng();
for i in 0..N {
let current_color = self.strip[i];
if current_color == self.background_color {
if rng.gen_bool(1.0 / self.ratio as f64) {
self.strip[i] = Some(self.color);
}
} else {
let color = current_color.unwrap();
let background = self.background_color.unwrap_or(PixelColor::default());
let distance = PixelColor {
red: self.step.0.abs() as u8,
green: self.step.1.abs() as u8,
blue: self.step.2.abs() as u8,
};
if color.is_closer(background, distance) {
self.strip[i] = self.background_color;
} else {
self.strip[i] = Some(color.delta(self.step));
}
}
}
self.current_iteration += 1;
Some(self.strip)
}
}

View File

@ -1,4 +1,4 @@
use crate::config::Config; use crate::config::Device;
use crate::patterns::{Pattern, Patterns, PixelColor}; use crate::patterns::{Pattern, Patterns, PixelColor};
use crate::Strip; use crate::Strip;
use bluer::Address; use bluer::Address;
@ -90,7 +90,7 @@ impl<const N: usize> Spectacle<N> {
blue: 0, blue: 0,
})); }));
self.runners.insert(runner.clone(), (tx, rx.clone())); self.runners.insert(runner.clone(), (tx, rx.clone()));
self.channels.insert(mac_address, rx); self.channels.insert(mac_address, rx.clone());
} }
} }
@ -100,20 +100,10 @@ impl<const N: usize> Spectacle<N> {
green: 0, green: 0,
blue: 255, blue: 255,
})); }));
// self.tasks.push(runner.runner_loop(tx));
self.default_runner = Some((runner, tx)); self.default_runner = Some((runner, tx));
self.default_receiver = Some(rx); self.default_receiver = Some(rx);
} }
// pub fn nbr_tasks(&self) -> usize {
// let mut nbr = 0;
// if self.default_receiver.is_some() {
// nbr += 1;
// }
// nbr += self.runners.len();
// nbr
// }
pub fn get_channel(&self, mac_address: &Address) -> Option<Receiver<Strip<N>>> { pub fn get_channel(&self, mac_address: &Address) -> Option<Receiver<Strip<N>>> {
let channel = self.channels.get(mac_address); let channel = self.channels.get(mac_address);
match channel { match channel {
@ -143,7 +133,7 @@ impl<const N: usize> Spectacle<N> {
println!("add default runner loop"); println!("add default runner loop");
} }
for (runner, (tx, _)) in self.runners.drain().take(1) { for (runner, (tx, _)) in self.runners.drain().into_iter() {
joinhandles.push(runner_loop(runner.clone(), tx)); joinhandles.push(runner_loop(runner.clone(), tx));
println!("add custom runner loop"); println!("add custom runner loop");
} }
@ -151,8 +141,8 @@ impl<const N: usize> Spectacle<N> {
} }
} }
impl<const N: usize> From<Config<N>> for Spectacle<N> { impl<const N: usize> From<Vec<Device<N>>> for Spectacle<N> {
fn from(config: Config<N>) -> Self { fn from(config: Vec<Device<N>>) -> Self {
let mut spectacle: Spectacle<N> = Spectacle::default(); let mut spectacle: Spectacle<N> = Spectacle::default();
for device in config { for device in config {
if let Some(mac_addresses) = device.mac_addresses { if let Some(mac_addresses) = device.mac_addresses {