add show example and scanner pattern

This commit is contained in:
Tobias Ollive 2022-03-02 13:45:55 +01:00
parent 7521c87460
commit 20983a8c08
2 changed files with 109 additions and 3 deletions

View File

@ -1 +1,36 @@
let yaml = serde_yaml::to_string(&runner).unwrap();
- pattern:
type: Fade
current_iteration: 0
nbr_iterations: 20
begin_color:
red: 255
green: 0
blue: 0
end_color:
red: 0
green: 0
blue: 255
period:
secs: 0
nanos: 200000000
- pattern:
type: ColorWipe
current_iteration: 0
color:
red: 0
green: 255
blue: 0
infinite: false
period:
secs: 0
nanos: 100000000
- pattern:
type: Scanner
current_iteration: 0
color:
red: 0
green: 0
blue: 255
period:
secs: 0
nanos: 100000000

View File

@ -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)
}
}