read show from file instead of writing it in the code
This commit is contained in:
156
src/main.rs
156
src/main.rs
@@ -1,10 +1,13 @@
|
||||
mod bluetooth;
|
||||
mod patterns;
|
||||
mod runner;
|
||||
mod spectacle;
|
||||
|
||||
use crate::patterns::{ColorWipe, Fade, PixelColor, Rainbow};
|
||||
use crate::patterns::{ColorWipe, Fade, Patterns, PixelColor};
|
||||
use crate::runner::Runner;
|
||||
use bluer::gatt::remote::Characteristic;
|
||||
use patterns::Strip;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::watch;
|
||||
@@ -16,6 +19,45 @@ const BASE_STRIP_DATA: [u8; 3] = [0x00, 0x00, 0x01];
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() -> bluer::Result<()> {
|
||||
let pixel = PixelColor {
|
||||
red: 231,
|
||||
green: 124,
|
||||
blue: 0,
|
||||
};
|
||||
|
||||
let fade = Fade::<25> {
|
||||
current_iteration: 0,
|
||||
nbr_iterations: 0,
|
||||
begin_color: pixel,
|
||||
end_color: Default::default(),
|
||||
};
|
||||
|
||||
let context = runner::Context {
|
||||
pattern: Patterns::Fade(fade),
|
||||
period: Duration::from_millis(300),
|
||||
};
|
||||
|
||||
let mut runner = runner::Runner::<25>::new();
|
||||
runner.push(context);
|
||||
|
||||
let context = runner::Context {
|
||||
pattern: Patterns::ColorWipe(ColorWipe {
|
||||
current_iteration: 0,
|
||||
color: Default::default(),
|
||||
infinite: false,
|
||||
}),
|
||||
period: Default::default(),
|
||||
};
|
||||
|
||||
runner.push(context);
|
||||
|
||||
let yaml = serde_yaml::to_string(&runner).unwrap();
|
||||
println!("{}", yaml);
|
||||
|
||||
let file = Path::new("spectacle.yml");
|
||||
let config: Runner<25> = Runner::load_from_file(file);
|
||||
println!("{:?}", config);
|
||||
|
||||
let adapter = bluetooth::create_session().await?;
|
||||
|
||||
let (tx_scan, mut rx_scan) = mpsc::channel(3);
|
||||
@@ -50,88 +92,13 @@ async fn main() -> bluer::Result<()> {
|
||||
bluer::Result::Ok(())
|
||||
});
|
||||
|
||||
let mut pixels = Vec::<PixelColor>::new();
|
||||
for _ in 0..STRIP_SIZE {
|
||||
pixels.push(PixelColor {
|
||||
red: 255,
|
||||
green: 0,
|
||||
blue: 0,
|
||||
})
|
||||
}
|
||||
|
||||
let mut _colorwipe = ColorWipe::<25> {
|
||||
current_iteration: 0,
|
||||
color: PixelColor {
|
||||
red: 0,
|
||||
green: 255,
|
||||
blue: 0,
|
||||
},
|
||||
infinite: true,
|
||||
};
|
||||
|
||||
|
||||
let mut fade = Fade::<25> {
|
||||
current_iteration: 0,
|
||||
nbr_iterations: 25,
|
||||
begin_color: PixelColor {
|
||||
red: 255,
|
||||
green: 0,
|
||||
blue: 0,
|
||||
},
|
||||
end_color: PixelColor {
|
||||
red: 0,
|
||||
green: 0,
|
||||
blue: 255,
|
||||
},
|
||||
};
|
||||
|
||||
let mut pattern = Rainbow::<25> {
|
||||
current_iteration: 0,
|
||||
max_iteration: None,
|
||||
step: Some(10),
|
||||
};
|
||||
|
||||
|
||||
let mut strip = fade.next().unwrap();
|
||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||
println!("starting");
|
||||
while tx.send(strip).is_ok() {
|
||||
if let Some(value) = fade.next() {
|
||||
strip = value;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(1000)).await;
|
||||
}
|
||||
println!("starting rainbow");
|
||||
while tx.send(pattern.next().unwrap()).is_ok() {
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
config.runner_loop(tx).await;
|
||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||
println!("error sending value");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// fn create_pattern_list() -> Vec<patterns>
|
||||
// async fn send_seq(char: &Characteristic) -> bluer::Result<()> {
|
||||
// println!(" Characteristic flags : {:?}, ", char.flags().await?);
|
||||
//
|
||||
// let mut strip_red : Strip<10> = Strip::default();
|
||||
// strip_red.strip.fill(PixelColor { red: 255, green: 0, blue: 0});
|
||||
//
|
||||
// let mut strip_green: Strip<10> = Strip::default();
|
||||
// strip_green.strip.fill(PixelColor { red: 0, green: 255, blue: 0});
|
||||
//
|
||||
// let pattern = RainbowPattern::<STRIP_SIZE> { current_iteration: 0,};
|
||||
//
|
||||
//
|
||||
// for strip in pattern {
|
||||
// write_strip(strip, char).await?;
|
||||
// sleep(Duration::from_millis(50)).await;
|
||||
// }
|
||||
//
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
pub async fn write_strip<const N: usize>(
|
||||
data: &Strip<N>,
|
||||
char: &Characteristic,
|
||||
@@ -141,36 +108,3 @@ pub async fn write_strip<const N: usize>(
|
||||
char.write(&*frame).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// async fn find_neopixel_service(device: &Device) -> bluer::Result<Option<Characteristic>> {
|
||||
// let addr = device.address();
|
||||
// let uuids = device.uuids().await?.unwrap_or_default();
|
||||
//
|
||||
// if uuids.contains(&SERVICE_UUID) {
|
||||
// println!("service neopixel found for device {}", &addr);
|
||||
// match connect(device, 3).await {
|
||||
// Ok(()) => {
|
||||
// println!("successefully connected");
|
||||
// }
|
||||
// Err(err) => {
|
||||
// println!("unable to connect, trying new device, {}", err);
|
||||
// return Err(err)
|
||||
// }
|
||||
// }
|
||||
// for service in device.services().await? {
|
||||
// if SERVICE_UUID == service.uuid().await? {
|
||||
// for char in service.characteristics().await? {
|
||||
// println!("uuid : {}", &char.uuid().await?);
|
||||
// if PIXEL_DATA_UUID == char.uuid().await? {
|
||||
//
|
||||
// return Ok(Some(char));
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// Ok(None)
|
||||
// }
|
||||
|
||||
@@ -4,6 +4,7 @@ pub(crate) enum patterns<const N: usize> {
|
||||
Rainbow(Rainbow<N>),
|
||||
Fade(Fade<N>),
|
||||
ColorWipe(ColorWipe<N>),
|
||||
Scanner(Scanner<N>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
@@ -59,6 +60,7 @@ impl<const N: usize> Default for Strip<N> {
|
||||
|
||||
/// Rainbow pattern /////////////////////////////////////////////////
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Rainbow<const N: usize> {
|
||||
pub(crate) current_iteration: usize,
|
||||
pub(crate) max_iteration: Option<usize>,
|
||||
@@ -116,6 +118,7 @@ fn wheel(index: u8) -> PixelColor {
|
||||
/// Colorwipe pattern ////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ColorWipe<const N: usize> {
|
||||
pub(crate) current_iteration: usize,
|
||||
pub(crate) color: PixelColor,
|
||||
@@ -145,6 +148,7 @@ impl<const N: usize> Iterator for ColorWipe<N> {
|
||||
/// fade pattern ////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Fade<const N: usize> {
|
||||
pub(crate) current_iteration: usize,
|
||||
pub(crate) nbr_iterations: usize,
|
||||
|
||||
@@ -1,23 +1,43 @@
|
||||
use std::borrow::Borrow;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::time::Duration;
|
||||
use crate::patterns::patterns;
|
||||
use crate::patterns::Patterns;
|
||||
use crate::Strip;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch::Sender;
|
||||
|
||||
struct Context<const N: usize> {
|
||||
pattern : Box<dyn Iterator<Item = Strip<N>>>,
|
||||
period: Duration,
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Context<const N: usize> {
|
||||
pub(crate) pattern: Patterns<N>,
|
||||
pub period: Duration,
|
||||
}
|
||||
|
||||
struct Runner<const N: usize> {
|
||||
list : Vec<Context<N>>,
|
||||
impl<const N: usize> IntoIterator for Runner<N> {
|
||||
type Item = Context<N>;
|
||||
type IntoIter = <Vec<Context<N>> as IntoIterator>::IntoIter; // so that you don't have to write std::vec::IntoIter, which nobody remembers anyway
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub(crate) struct Runner<const N: usize>(Vec<Context<N>>);
|
||||
|
||||
impl<const N: usize> Runner<N> {
|
||||
async fn runner_loop(self, tx: Sender<Strip<N>>) {
|
||||
for mut context in self.list {
|
||||
pub fn new() -> Runner<N> {
|
||||
let runner = Vec::<Context<N>>::new();
|
||||
Runner(runner)
|
||||
}
|
||||
|
||||
pub fn push(&mut self, value: Context<N>) {
|
||||
self.0.push(value)
|
||||
}
|
||||
|
||||
pub async fn runner_loop(self, tx: Sender<Strip<N>>) {
|
||||
for mut context in self {
|
||||
let mut strip = context.pattern.next().unwrap();
|
||||
let delay = context.period;
|
||||
println!("{:?}", delay);
|
||||
while tx.send(strip).is_ok() {
|
||||
if let Some(value) = context.pattern.next() {
|
||||
strip = value;
|
||||
@@ -28,4 +48,10 @@ impl<const N: usize> Runner<N> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_from_file(file: &std::path::Path) -> Runner<N> {
|
||||
let file = fs::read_to_string(file).unwrap();
|
||||
let spectacle: Runner<N> = serde_yaml::from_str(&*file).unwrap();
|
||||
spectacle
|
||||
}
|
||||
}
|
||||
|
||||
0
src/spectacle.rs
Normal file
0
src/spectacle.rs
Normal file
Reference in New Issue
Block a user