rust_lum/src/patterns.rs

271 lines
7.0 KiB
Rust
Raw Normal View History

2022-03-02 13:45:55 +01:00
use serde_derive::{Deserialize, Serialize};
use std::cmp::{max, min, Ordering};
use std::convert::Infallible;
2022-01-27 20:23:02 +01:00
use std::fmt;
2022-03-02 13:45:55 +01:00
use std::ops::Div;
2022-01-24 16:17:54 +01:00
2022-03-02 13:45:55 +01:00
#[derive(Serialize, Deserialize, Debug)]
#[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>),
Scanner(Scanner<N>),
2022-02-23 19:48:38 +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(),
}
}
}
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
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 PixelColor {
fn new() -> Self {
Default::default()
}
}
2022-01-24 16:17:54 +01:00
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-01-27 20:23:02 +01:00
#[derive(Copy, Clone)]
pub struct Strip<const N: usize> {
2022-01-24 16:17:54 +01:00
pub strip: [PixelColor; N],
}
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-01-27 20:26:57 +01:00
data.append(&mut vec![
self.strip[i].green,
2022-02-21 16:16:06 +01:00
self.strip[i].red,
2022-01-27 20:26:57 +01:00
self.strip[i].blue,
]);
2022-01-24 16:17:54 +01:00
}
data
}
2022-02-21 16:16:06 +01:00
pub fn fill(&mut self, color: PixelColor) {
self.strip = [color; N];
}
2022-01-24 16:17:54 +01:00
}
2022-01-27 20:23:02 +01:00
impl<const N: usize> Default for Strip<N> {
fn default() -> Self {
2022-01-27 20:26:57 +01:00
Strip {
strip: [PixelColor::new(); N],
}
2022-01-27 20:23:02 +01:00
}
}
2022-02-21 16:16:06 +01:00
/// Rainbow pattern /////////////////////////////////////////////////
#[derive(Serialize, Deserialize, Debug)]
2022-02-21 16:16:06 +01:00
pub struct Rainbow<const N: usize> {
2022-01-27 20:23:02 +01:00
pub(crate) current_iteration: usize,
2022-01-27 20:26:57 +01:00
pub(crate) max_iteration: Option<usize>,
2022-02-21 16:16:06 +01:00
pub(crate) step: Option<usize>,
2022-01-24 16:17:54 +01:00
}
2022-01-27 20:23:02 +01:00
2022-02-21 16:16:06 +01:00
impl<const N: usize> Iterator for Rainbow<N> {
2022-01-27 20:23:02 +01:00
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(nbr_iteration) = self.max_iteration {
if nbr_iteration == self.current_iteration {
2022-01-27 20:26:57 +01:00
return None;
2022-01-27 20:23:02 +01:00
}
}
let mut strip = Strip::default();
2022-02-23 13:40:56 +01:00
let step = self.step.unwrap_or(255 / N);
2022-01-27 20:23:02 +01:00
for i in 0..N {
2022-01-27 20:26:57 +01:00
let pos = (i * step + self.current_iteration) as u8;
2022-01-27 20:23:02 +01:00
strip.strip[i] = wheel(pos)
}
2022-02-21 16:16:06 +01:00
self.current_iteration += 1;
2022-01-27 20:23:02 +01:00
Some(strip)
}
}
fn wheel(index: u8) -> PixelColor {
let pos = 255 - index;
match pos {
2022-01-27 20:26:57 +01:00
0..=85 => PixelColor {
2022-01-27 20:23:02 +01:00
red: 255 - (pos * 3),
2022-01-27 20:26:57 +01:00
green: 0,
blue: pos * 3,
},
2022-01-27 20:23:02 +01:00
86..=170 => {
let pos = pos - 85;
2022-01-27 20:26:57 +01:00
PixelColor {
2022-01-27 20:23:02 +01:00
red: 0,
2022-01-27 20:26:57 +01:00
green: pos * 3,
blue: 255 - (pos * 3),
}
2022-01-27 20:23:02 +01:00
}
_ => {
let pos = pos - 170;
2022-02-21 16:16:06 +01:00
PixelColor {
2022-01-27 20:26:57 +01:00
red: pos * 3,
green: 255 - (pos * 3),
blue: 0,
2022-02-21 16:16:06 +01:00
}
2022-01-27 20:23:02 +01:00
}
}
2022-01-27 20:26:57 +01:00
}
2022-02-21 16:16:06 +01:00
//////////////////////////////////////////////////////////////////////////
/// Colorwipe pattern ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
#[derive(Serialize, Deserialize, Debug)]
2022-02-23 13:40:56 +01:00
pub struct ColorWipe<const N: usize> {
2022-02-21 16:16:06 +01:00
pub(crate) current_iteration: usize,
2022-02-23 13:40:56 +01:00
pub(crate) color: PixelColor,
pub(crate) infinite: bool,
2022-02-21 16:16:06 +01:00
}
impl<const N: usize> Iterator for ColorWipe<N> {
type Item = Strip<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 {
2022-02-23 13:40:56 +01:00
return None;
2022-02-21 16:16:06 +01:00
}
for i in 0..self.current_iteration {
strip.strip[i] = self.color;
}
self.current_iteration += 1;
Some(strip)
}
}
//////////////////////////////////////////////////////////////////////////
/// fade pattern ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
#[derive(Serialize, Deserialize, Debug)]
2022-02-21 16:16:06 +01:00
pub struct Fade<const N: usize> {
pub(crate) current_iteration: usize,
pub(crate) nbr_iterations: usize,
pub(crate) begin_color: PixelColor,
pub(crate) end_color: PixelColor,
}
2022-02-23 13:40:56 +01:00
fn fade_value(value_start: u8, value_end: u8, index: usize, nbr_iter: usize) -> u8 {
((value_start as usize * (nbr_iter - index) + value_end as usize * index) / nbr_iter) as u8
2022-02-21 16:16:06 +01:00
}
impl<const N: usize> Iterator for Fade<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
let mut strip = Strip::default();
if self.current_iteration >= self.nbr_iterations {
2022-02-23 13:40:56 +01:00
return None;
2022-02-21 16:16:06 +01:00
}
2022-02-23 13:40:56 +01:00
let red = fade_value(
self.begin_color.red,
self.end_color.red,
self.current_iteration,
self.nbr_iterations,
);
let green = fade_value(
self.begin_color.green,
self.end_color.green,
self.current_iteration,
self.nbr_iterations,
);
let blue = fade_value(
self.begin_color.blue,
self.end_color.blue,
self.current_iteration,
self.nbr_iterations,
);
let current_color = PixelColor { red, green, blue };
2022-02-21 16:16:06 +01:00
strip.fill(current_color);
self.current_iteration += 1;
Some(strip)
}
2022-02-23 13:40:56 +01:00
}
2022-03-02 13:45:55 +01:00
//////////////////////////////////////////////////////////////////////////
/// scanner pattern ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
#[derive(Serialize, Deserialize, Debug)]
pub struct Scanner<const N: usize> {
pub(crate) current_iteration: usize,
pub(crate) color: PixelColor,
pub(crate) background_color: Option<PixelColor>,
}
impl<const N: usize> Iterator for Scanner<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
let mut strip = Strip::<N>::default();
if let Some(c) = self.background_color {
strip.fill(c);
} else {
strip.fill(PixelColor::default());
}
let current_led = self.current_iteration % N;
let min_led;
if current_led < 8 {
min_led = 0;
} else {
min_led = current_led - 8;
}
let mut c = self.color;
for i in min_led..current_led {
strip.strip[i] = c;
c = c / 2;
println!("{}", c);
}
self.current_iteration += 1;
Some(strip)
}
}