add show example and scanner pattern
This commit is contained in:
@@ -1,13 +1,32 @@
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::cmp::{max, min, Ordering};
|
||||
use std::convert::Infallible;
|
||||
use std::fmt;
|
||||
use std::ops::Div;
|
||||
|
||||
pub(crate) enum patterns<const N: usize> {
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[serde(tag = "type")]
|
||||
pub(crate) enum Patterns<const N: usize> {
|
||||
Rainbow(Rainbow<N>),
|
||||
Fade(Fade<N>),
|
||||
ColorWipe(ColorWipe<N>),
|
||||
Scanner(Scanner<N>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
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)]
|
||||
pub struct PixelColor {
|
||||
pub(crate) red: u8,
|
||||
pub(crate) green: u8,
|
||||
@@ -26,6 +45,18 @@ impl fmt::Display for PixelColor {
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Strip<const N: usize> {
|
||||
pub strip: [PixelColor; N],
|
||||
@@ -197,3 +228,43 @@ impl<const N: usize> Iterator for Fade<N> {
|
||||
Some(strip)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
/// 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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user