2022-03-12 20:52:37 +01:00
|
|
|
mod blink;
|
2022-03-11 12:40:12 +01:00
|
|
|
mod color_wipe;
|
2022-03-12 20:52:37 +01:00
|
|
|
mod fade;
|
|
|
|
mod fade_random;
|
|
|
|
mod fade_unstable;
|
2022-03-11 12:40:12 +01:00
|
|
|
mod fill_random;
|
|
|
|
mod fill_unstable;
|
2022-03-11 17:10:33 +01:00
|
|
|
mod rain;
|
2022-03-12 20:52:37 +01:00
|
|
|
mod rainbow;
|
|
|
|
mod scanner;
|
2022-03-11 12:40:12 +01:00
|
|
|
|
|
|
|
use crate::patterns::blink::Blink;
|
|
|
|
use crate::patterns::color_wipe::ColorWipe;
|
|
|
|
use crate::patterns::fade::Fade;
|
2022-03-12 20:45:27 +01:00
|
|
|
use crate::patterns::fade_random::FadeRandom;
|
2022-03-12 20:52:37 +01:00
|
|
|
use crate::patterns::fade_unstable::FadeUnstable;
|
2022-03-11 12:40:12 +01:00
|
|
|
use crate::patterns::fill_random::FillRandom;
|
|
|
|
use crate::patterns::fill_unstable::FillUnstable;
|
2022-03-11 18:43:48 +01:00
|
|
|
use crate::patterns::rain::Rain;
|
2022-03-11 12:40:12 +01:00
|
|
|
use crate::patterns::rainbow::Rainbow;
|
|
|
|
use crate::patterns::scanner::Scanner;
|
2022-03-12 20:52:37 +01:00
|
|
|
use rand::rngs::ThreadRng;
|
|
|
|
use rand::Rng;
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
use std::ops::{AddAssign, Div, Index, IndexMut, SubAssign};
|
2022-01-24 16:17:54 +01:00
|
|
|
|
2022-03-09 15:53:42 +01:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)]
|
2022-03-02 13:45:55 +01:00
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub(crate) enum Patterns<const N: usize> {
|
2022-02-23 19:48:38 +01:00
|
|
|
Rainbow(Rainbow<N>),
|
|
|
|
Fade(Fade<N>),
|
|
|
|
ColorWipe(ColorWipe<N>),
|
2022-03-02 13:44:28 +01:00
|
|
|
Scanner(Scanner<N>),
|
2022-03-02 18:47:03 +01:00
|
|
|
FillRandom(FillRandom<N>),
|
2022-03-10 09:54:02 +01:00
|
|
|
FillUnstable(FillUnstable<N>),
|
|
|
|
Blink(Blink<N>),
|
2022-03-11 18:43:48 +01:00
|
|
|
Rain(Rain<N>),
|
2022-03-12 20:52:37 +01:00
|
|
|
FadeRandom(FadeRandom<N>),
|
|
|
|
FadeUnstable(FadeUnstable<N>),
|
2022-02-23 19:48:38 +01:00
|
|
|
}
|
|
|
|
|
2022-03-12 20:52:37 +01:00
|
|
|
pub trait Pattern: Iterator {
|
2022-03-11 17:10:33 +01:00
|
|
|
type Strip;
|
|
|
|
|
2022-03-11 18:43:48 +01:00
|
|
|
fn init(&mut self) -> Option<Self::Strip>;
|
|
|
|
fn is_last_iteration(&self) -> bool;
|
2022-03-11 17:10:33 +01:00
|
|
|
}
|
|
|
|
|
2022-03-02 13:45:55 +01:00
|
|
|
impl<const N: usize> Iterator for Patterns<N> {
|
|
|
|
type Item = Strip<N>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match self {
|
|
|
|
Patterns::Rainbow(p) => p.next(),
|
|
|
|
Patterns::Fade(p) => p.next(),
|
|
|
|
Patterns::ColorWipe(p) => p.next(),
|
|
|
|
Patterns::Scanner(p) => p.next(),
|
2022-03-02 18:47:03 +01:00
|
|
|
Patterns::FillRandom(p) => p.next(),
|
2022-03-09 18:09:26 +01:00
|
|
|
Patterns::FillUnstable(p) => p.next(),
|
2022-03-10 09:54:02 +01:00
|
|
|
Patterns::Blink(p) => p.next(),
|
2022-03-11 18:43:48 +01:00
|
|
|
Patterns::Rain(p) => p.next(),
|
2022-03-12 20:45:27 +01:00
|
|
|
Patterns::FadeRandom(p) => p.next(),
|
2022-03-12 20:52:37 +01:00
|
|
|
Patterns::FadeUnstable(p) => p.next(),
|
2022-03-02 13:45:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 17:10:33 +01:00
|
|
|
impl<const N: usize> Pattern for Patterns<N> {
|
|
|
|
type Strip = Strip<N>;
|
|
|
|
|
2022-03-11 18:43:48 +01:00
|
|
|
fn init(&mut self) -> Option<Self::Strip> {
|
2022-03-11 17:10:33 +01:00
|
|
|
match self {
|
|
|
|
Patterns::Rainbow(p) => p.init(),
|
|
|
|
Patterns::Fade(p) => p.init(),
|
|
|
|
Patterns::ColorWipe(p) => p.init(),
|
|
|
|
Patterns::Scanner(p) => p.init(),
|
|
|
|
Patterns::FillRandom(p) => p.init(),
|
|
|
|
Patterns::FillUnstable(p) => p.init(),
|
|
|
|
Patterns::Blink(p) => p.init(),
|
2022-03-11 18:43:48 +01:00
|
|
|
Patterns::Rain(p) => p.init(),
|
2022-03-12 20:45:27 +01:00
|
|
|
Patterns::FadeRandom(p) => p.init(),
|
2022-03-12 20:52:37 +01:00
|
|
|
Patterns::FadeUnstable(p) => p.init(),
|
2022-03-11 18:43:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_last_iteration(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Patterns::Rainbow(p) => p.is_last_iteration(),
|
|
|
|
Patterns::Fade(p) => p.is_last_iteration(),
|
|
|
|
Patterns::ColorWipe(p) => p.is_last_iteration(),
|
|
|
|
Patterns::Scanner(p) => p.is_last_iteration(),
|
|
|
|
Patterns::FillRandom(p) => p.is_last_iteration(),
|
|
|
|
Patterns::FillUnstable(p) => p.is_last_iteration(),
|
|
|
|
Patterns::Blink(p) => p.is_last_iteration(),
|
|
|
|
Patterns::Rain(p) => p.is_last_iteration(),
|
2022-03-12 20:45:27 +01:00
|
|
|
Patterns::FadeRandom(p) => p.is_last_iteration(),
|
2022-03-12 20:52:37 +01:00
|
|
|
Patterns::FadeUnstable(p) => p.is_last_iteration(),
|
2022-03-11 17:10:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:47:03 +01:00
|
|
|
///
|
|
|
|
/// a struct for an RGB color
|
2022-03-09 15:53:42 +01:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Default, Eq, Hash, PartialEq)]
|
2022-01-27 20:23:02 +01:00
|
|
|
pub struct PixelColor {
|
2022-01-24 16:17:54 +01:00
|
|
|
pub(crate) red: u8,
|
|
|
|
pub(crate) green: u8,
|
|
|
|
pub(crate) blue: u8,
|
|
|
|
}
|
|
|
|
|
2022-01-27 20:23:02 +01:00
|
|
|
impl fmt::Display for PixelColor {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "[{},{},{}]", self.red, self.green, self.blue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 13:45:55 +01:00
|
|
|
impl Div<u8> for PixelColor {
|
|
|
|
type Output = PixelColor;
|
|
|
|
|
|
|
|
fn div(self, rhs: u8) -> Self::Output {
|
|
|
|
PixelColor {
|
|
|
|
red: self.red / rhs,
|
|
|
|
green: self.green / rhs,
|
|
|
|
blue: self.blue / rhs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:47:03 +01:00
|
|
|
impl SubAssign for PixelColor {
|
|
|
|
fn sub_assign(&mut self, rhs: Self) {
|
|
|
|
self.red = self.red.saturating_sub(rhs.red);
|
|
|
|
self.green = self.red.saturating_sub(rhs.green);
|
|
|
|
self.blue = self.red.saturating_sub(rhs.blue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-12 20:45:27 +01:00
|
|
|
impl AddAssign for PixelColor {
|
|
|
|
fn add_assign(&mut self, rhs: Self) {
|
|
|
|
self.red = self.red.saturating_add(rhs.red);
|
|
|
|
self.green = self.red.saturating_add(rhs.green);
|
|
|
|
self.blue = self.red.saturating_add(rhs.blue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PixelColor {
|
|
|
|
/// apply random delta to pixel and return computed value
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `pixel`: pixel to apply delta
|
|
|
|
/// * `stability`: amplitude of delta between 0 and `stability`
|
|
|
|
///
|
|
|
|
/// returns: PixelColor
|
|
|
|
///
|
|
|
|
pub fn random_delta(&self, stability: usize, rng: &mut ThreadRng) -> PixelColor {
|
|
|
|
let red_delta = rng.gen_range(0..stability) as u8;
|
|
|
|
let green_delta = rng.gen_range(0..stability) as u8;
|
|
|
|
let blue_delta = rng.gen_range(0..stability) as u8;
|
|
|
|
let operation = rng.gen_bool(0.5);
|
|
|
|
let red;
|
|
|
|
let green;
|
|
|
|
let blue;
|
|
|
|
if operation {
|
|
|
|
red = self.red.saturating_add(red_delta);
|
|
|
|
green = self.green.saturating_add(green_delta);
|
|
|
|
blue = self.blue.saturating_add(blue_delta);
|
|
|
|
} else {
|
|
|
|
red = self.red.saturating_sub(red_delta);
|
|
|
|
green = self.green.saturating_sub(green_delta);
|
|
|
|
blue = self.blue.saturating_sub(blue_delta);
|
|
|
|
}
|
2022-03-12 20:52:37 +01:00
|
|
|
PixelColor { red, green, blue }
|
2022-03-12 20:45:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:47:03 +01:00
|
|
|
///
|
|
|
|
/// a basic newtype based on a fix array size.
|
|
|
|
///
|
2022-03-11 17:10:33 +01:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
|
|
|
|
pub struct Strip<const N: usize>([Option<PixelColor>; N]);
|
2022-03-02 18:47:03 +01:00
|
|
|
|
|
|
|
impl<const N: usize> Default for Strip<N> {
|
|
|
|
fn default() -> Self {
|
2022-03-11 17:10:33 +01:00
|
|
|
Strip([None; N])
|
2022-03-02 18:47:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const N: usize> Index<usize> for Strip<N> {
|
2022-03-11 17:10:33 +01:00
|
|
|
type Output = Option<PixelColor>;
|
2022-03-02 18:47:03 +01:00
|
|
|
|
|
|
|
fn index(&self, index: usize) -> &Self::Output {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const N: usize> IndexMut<usize> for Strip<N> {
|
2022-03-11 17:10:33 +01:00
|
|
|
fn index_mut(&mut self, index: usize) -> &mut Option<PixelColor> {
|
2022-03-02 18:47:03 +01:00
|
|
|
&mut self.0[index]
|
|
|
|
}
|
2022-01-24 16:17:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<const N: usize> Strip<N> {
|
2022-02-21 16:16:06 +01:00
|
|
|
pub fn to_array(self) -> Vec<u8> {
|
2022-01-27 20:26:57 +01:00
|
|
|
let mut data: Vec<u8> = vec![];
|
2022-01-24 16:17:54 +01:00
|
|
|
|
|
|
|
for i in 0..N {
|
2022-03-11 17:10:33 +01:00
|
|
|
let color = self[i].unwrap_or(PixelColor::default());
|
|
|
|
data.append(&mut vec![color.green, color.red, color.blue]);
|
2022-01-24 16:17:54 +01:00
|
|
|
}
|
|
|
|
data
|
|
|
|
}
|
2022-02-21 16:16:06 +01:00
|
|
|
|
2022-03-10 09:54:02 +01:00
|
|
|
pub fn new(color: PixelColor) -> Strip<N> {
|
2022-03-12 20:52:37 +01:00
|
|
|
Strip {
|
|
|
|
0: [Some(color); N],
|
|
|
|
}
|
2022-03-10 09:54:02 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 16:16:06 +01:00
|
|
|
pub fn fill(&mut self, color: PixelColor) {
|
2022-03-11 17:10:33 +01:00
|
|
|
self.0.fill(Some(color));
|
2022-01-27 20:23:02 +01:00
|
|
|
}
|
2022-03-12 20:52:37 +01:00
|
|
|
}
|