use macro rule for default implementation of pattern
This commit is contained in:
parent
20b8f6d4db
commit
13326a49d9
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -920,6 +920,14 @@ 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"
|
||||||
|
@ -1096,6 +1104,7 @@ dependencies = [
|
||||||
"bluer",
|
"bluer",
|
||||||
"clap",
|
"clap",
|
||||||
"futures",
|
"futures",
|
||||||
|
"pattern_derive",
|
||||||
"rand",
|
"rand",
|
||||||
"rodio",
|
"rodio",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -12,6 +12,7 @@ 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"
|
||||||
|
|
|
@ -6,6 +6,7 @@ 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, Config};
|
||||||
|
|
|
@ -49,10 +49,36 @@ pub(crate) enum Patterns<const N: usize> {
|
||||||
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_none {
|
||||||
|
($name:ty, $generic: tt) => {
|
||||||
|
type Strip = Strip<N>;
|
||||||
|
|
||||||
|
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 Patterns<N> {
|
impl<const N: usize> Iterator for Patterns<N> {
|
||||||
type Item = Strip<N>;
|
type Item = Strip<N>;
|
||||||
|
|
||||||
|
|
|
@ -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_none, Strip};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// # Blink
|
/// # Blink
|
||||||
|
@ -15,24 +15,7 @@ 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>;
|
impl_pattern_none!(FadeRandom, N);
|
||||||
|
|
||||||
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> {
|
||||||
|
|
|
@ -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_none, Strip};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// # FadeRandom
|
/// # FadeRandom
|
||||||
|
@ -16,25 +16,25 @@ 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>;
|
impl_pattern_none!(FadeRandom, N);
|
||||||
|
|
||||||
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> 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>;
|
||||||
|
|
|
@ -18,10 +18,6 @@ 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>;
|
||||||
|
|
||||||
fn init(&mut self) -> Option<Self::Strip> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_last_iteration(&self) -> bool {
|
fn is_last_iteration(&self) -> bool {
|
||||||
match self.max_iteration {
|
match self.max_iteration {
|
||||||
None => false,
|
None => false,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::patterns::{Pattern, PixelColor};
|
use crate::patterns::{Pattern, PixelColor};
|
||||||
use crate::Strip;
|
use crate::{impl_pattern_none, Strip};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// # FillRandom
|
/// # FillRandom
|
||||||
|
@ -15,24 +15,7 @@ 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>;
|
impl_pattern_none!(FadeRandom, N);
|
||||||
|
|
||||||
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> {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::patterns::{Pattern, PixelColor};
|
use crate::patterns::{Pattern, PixelColor};
|
||||||
use crate::Strip;
|
use crate::{impl_pattern_none, Strip};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// # FillUnstable
|
/// # FillUnstable
|
||||||
|
@ -15,24 +15,7 @@ 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>;
|
impl_pattern_none!(FadeRandom, N);
|
||||||
|
|
||||||
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> {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::patterns::{Pattern, PixelColor};
|
use crate::patterns::{Pattern, PixelColor};
|
||||||
use crate::Strip;
|
use crate::{impl_pattern_none, Strip};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// # Rainbow pattern
|
/// # Rainbow pattern
|
||||||
|
@ -21,24 +21,7 @@ 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>;
|
impl_pattern_none!(FadeRandom, N);
|
||||||
|
|
||||||
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> {
|
||||||
|
|
|
@ -20,7 +20,7 @@ impl<const N: usize> Pattern for Ring<N> {
|
||||||
|
|
||||||
fn init(&mut self) -> Option<Self::Strip> {
|
fn init(&mut self) -> Option<Self::Strip> {
|
||||||
None
|
None
|
||||||
}
|
} //FIXME
|
||||||
|
|
||||||
fn is_last_iteration(&self) -> bool {
|
fn is_last_iteration(&self) -> bool {
|
||||||
match self.max_iteration {
|
match self.max_iteration {
|
||||||
|
|
|
@ -20,7 +20,7 @@ impl<const N: usize> Pattern for RingScanner<N> {
|
||||||
|
|
||||||
fn init(&mut self) -> Option<Self::Strip> {
|
fn init(&mut self) -> Option<Self::Strip> {
|
||||||
None
|
None
|
||||||
}
|
} //FIXME
|
||||||
|
|
||||||
fn is_last_iteration(&self) -> bool {
|
fn is_last_iteration(&self) -> bool {
|
||||||
match self.max_iteration {
|
match self.max_iteration {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user