Compare commits

..

No commits in common. "6deb31836f3d0bc6747a9e411b10eaa99abefbcf" and "20b8f6d4db49013867123db0e979b275d19f5902" have entirely different histories.

20 changed files with 271 additions and 258 deletions

9
Cargo.lock generated
View File

@ -920,14 +920,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "pattern_derive"
version = "0.1.0"
dependencies = [
"quote",
"syn",
]
[[package]] [[package]]
name = "peeking_take_while" name = "peeking_take_while"
version = "0.1.2" version = "0.1.2"
@ -1104,7 +1096,6 @@ dependencies = [
"bluer", "bluer",
"clap", "clap",
"futures", "futures",
"pattern_derive",
"rand", "rand",
"rodio", "rodio",
"serde", "serde",

View File

@ -12,7 +12,6 @@ futures = "0.3.19"
rand = "0.8.5" rand = "0.8.5"
rodio = "0.15.0" rodio = "0.15.0"
clap = { version = "3.1.6", features = ["derive"] } clap = { version = "3.1.6", features = ["derive"] }
pattern_derive = { path = "pattern_derive" }
serde_derive = "1.0.136" serde_derive = "1.0.136"
serde = "1.0.136" serde = "1.0.136"

View File

@ -1,46 +1,39 @@
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: &yellow background_color: &black
red: 0 red: 0
green: 255 green: 255
blue: 255 blue: 0
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: begin_color: &red
red: 255 red: 255
green: 0 green: 0
blue: 0 blue: 0
@ -62,15 +55,11 @@ devices:
- name: titi - name: titi
sequence: sequence:
- pattern: - pattern:
type: StarsRandom type: RingScanner
color: *green color: *green
ratio: 8 ring_size: 4
steps: 20 max_iteration : 50
background_color: period: 100
red: 0
green: 255
blue: 100
period: 50
- pattern: - pattern:
type: Rainbow type: Rainbow
max_iteration: 200 max_iteration: 200

View File

@ -1,15 +1,9 @@
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> {
@ -18,8 +12,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) -> Vec<Device<N>> { pub fn load_from_file<const N: usize>(file: &std::path::Path) -> Config<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.devices config
} }

View File

@ -8,7 +8,7 @@ mod spectacle;
use crate::args::Cli; use crate::args::Cli;
use crate::audio::play_sound; use crate::audio::play_sound;
use crate::config::load_from_file; use crate::config::{load_from_file, Config};
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};
@ -29,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 = load_from_file(file); let config: Config<25> = 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));
@ -86,6 +86,7 @@ async fn connect_device(
}; };
} }
println!("device {} disconnected", &device.address()); println!("device {} disconnected", &device.address());
// drop(rx_device);
}); });
} }
} }

View File

@ -10,7 +10,6 @@ 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;
@ -24,12 +23,11 @@ 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::{Add, AddAssign, Div, Index, IndexMut, Sub, SubAssign}; use std::ops::{AddAssign, Div, Index, IndexMut, 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")]
@ -46,49 +44,15 @@ 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 {
type Strip; type Strip;
fn init(&mut self) -> Option<Self::Strip> { fn init(&mut self) -> Option<Self::Strip>;
None
}
fn is_last_iteration(&self) -> bool; fn is_last_iteration(&self) -> bool;
} }
#[macro_export]
macro_rules! impl_pattern_init_background {
($generic: tt) => {
fn init(&mut self) -> Option<Self::Strip> {
if let Some(color) = self.background_color {
Some(Strip::<N>::new(color))
} else {
None
}
}
};
}
#[macro_export]
macro_rules! impl_pattern_last_iteration {
($name:ty) => {
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 Patterns<N> { impl<const N: usize> Iterator for Patterns<N> {
type Item = Strip<N>; type Item = Strip<N>;
@ -106,7 +70,6 @@ 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(),
} }
} }
} }
@ -128,7 +91,6 @@ 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(),
} }
} }
@ -146,7 +108,6 @@ 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(),
} }
} }
} }
@ -178,30 +139,6 @@ 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);
@ -229,27 +166,23 @@ 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 minus_interval = -(stability as i8); let red_delta = rng.gen_range(0..stability) as u8;
let max_interval = stability as i8; let green_delta = rng.gen_range(0..stability) as u8;
let red_delta = rng.gen_range(minus_interval..max_interval) as i8; let blue_delta = rng.gen_range(0..stability) as u8;
let green_delta = rng.gen_range(minus_interval..max_interval) as i8; let operation = rng.gen_bool(0.5);
let blue_delta = rng.gen_range(minus_interval..max_interval) as i8; let red;
self.delta((red_delta, green_delta, blue_delta)) let green;
} let blue;
if operation {
pub fn delta(&self, delta: (i8, i8, i8)) -> PixelColor { red = self.red.saturating_add(red_delta);
let (red, green, blue) = delta; green = self.green.saturating_add(green_delta);
PixelColor { blue = self.blue.saturating_add(blue_delta);
red: (self.red as i16 + red as i16) as u8, } else {
green: (self.green as i16 + green as i16) as u8, red = self.red.saturating_sub(red_delta);
blue: (self.blue as i16 + blue as i16) as u8, green = self.green.saturating_sub(green_delta);
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_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Blink /// # Blink
@ -16,7 +16,23 @@ pub struct Blink<const N: usize> {
impl<const N: usize> Pattern for Blink<N> { impl<const N: usize> Pattern for Blink<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_last_iteration!(ColorWipe);
fn init(&mut self) -> Option<Self::Strip> {
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 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::{impl_pattern_init_background, impl_pattern_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Colorwipe pattern /// # Colorwipe pattern
@ -22,8 +22,27 @@ 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);
impl_pattern_last_iteration!(ColorWipe); fn init(&mut self) -> Option<Self::Strip> {
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,6 +20,10 @@ 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_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # FadeRandom /// # FadeRandom
@ -17,7 +17,23 @@ pub struct FadeRandom<const N: usize> {
impl<const N: usize> Pattern for FadeRandom<N> { impl<const N: usize> Pattern for FadeRandom<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_last_iteration!(ColorWipe);
fn init(&mut self) -> Option<Self::Strip> {
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 FadeRandom<N> { impl<const N: usize> Iterator for FadeRandom<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::{impl_pattern_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # FadeUnstable /// # FadeUnstable
@ -17,7 +17,23 @@ 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 init(&mut self) -> Option<Self::Strip> {
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 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_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # FillRandom /// # FillRandom
@ -16,7 +16,23 @@ pub struct FillRandom<const N: usize> {
impl<const N: usize> Pattern for FillRandom<N> { impl<const N: usize> Pattern for FillRandom<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_last_iteration!(ColorWipe);
fn init(&mut self) -> Option<Self::Strip> {
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 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_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # FillUnstable /// # FillUnstable
@ -16,7 +16,23 @@ pub struct FillUnstable<const N: usize> {
impl<const N: usize> Pattern for FillUnstable<N> { impl<const N: usize> Pattern for FillUnstable<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_last_iteration!(FillUnstable);
fn init(&mut self) -> Option<Self::Strip> {
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 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::{impl_pattern_last_iteration, Strip}; use crate::Strip;
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -28,15 +28,28 @@ 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(self.drops) Some(Strip::<N>::new(color))
} 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> {
@ -55,7 +68,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(c.unwrap() / 2); strip[i] = Some(strip[i + self.lines].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_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Rainbow pattern /// # Rainbow pattern
@ -22,7 +22,23 @@ pub struct Rainbow<const N: usize> {
impl<const N: usize> Pattern for Rainbow<N> { impl<const N: usize> Pattern for Rainbow<N> {
type Strip = Strip<N>; type Strip = Strip<N>;
impl_pattern_last_iteration!(Rainbow);
fn init(&mut self) -> Option<Self::Strip> {
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 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::{impl_pattern_init_background, impl_pattern_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # Ring /// # Ring
@ -17,8 +17,23 @@ 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);
impl_pattern_last_iteration!(Ring); fn init(&mut self) -> Option<Self::Strip> {
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 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::{impl_pattern_init_background, impl_pattern_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # RingScanner /// # RingScanner
@ -17,8 +17,23 @@ 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);
impl_pattern_last_iteration!(ColorWipe); fn init(&mut self) -> Option<Self::Strip> {
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 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::{impl_pattern_init_background, impl_pattern_last_iteration, Strip}; use crate::Strip;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// # scanner pattern /// # scanner pattern
@ -20,8 +20,27 @@ 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);
impl_pattern_last_iteration!(ColorWipe); fn init(&mut self) -> Option<Self::Strip> {
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

@ -1,85 +0,0 @@
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::Device; use crate::config::Config;
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.clone()); self.channels.insert(mac_address, rx);
} }
} }
@ -100,10 +100,20 @@ 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 {
@ -133,7 +143,7 @@ impl<const N: usize> Spectacle<N> {
println!("add default runner loop"); println!("add default runner loop");
} }
for (runner, (tx, _)) in self.runners.drain().into_iter() { for (runner, (tx, _)) in self.runners.drain().take(1) {
joinhandles.push(runner_loop(runner.clone(), tx)); joinhandles.push(runner_loop(runner.clone(), tx));
println!("add custom runner loop"); println!("add custom runner loop");
} }
@ -141,8 +151,8 @@ impl<const N: usize> Spectacle<N> {
} }
} }
impl<const N: usize> From<Vec<Device<N>>> for Spectacle<N> { impl<const N: usize> From<Config<N>> for Spectacle<N> {
fn from(config: Vec<Device<N>>) -> Self { fn from(config: Config<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 {