From 7521c8746098c72d9d9cdae864fe5dfdc6efcd90 Mon Sep 17 00:00:00 2001 From: Tobias Ollive Date: Wed, 2 Mar 2022 13:44:28 +0100 Subject: [PATCH] read show from file instead of writing it in the code --- Cargo.lock | 54 ++++++++++++++-- Cargo.toml | 6 +- spectacle.toml | 13 ---- spectacle.yml | 1 + src/main.rs | 156 ++++++++++++++--------------------------------- src/patterns.rs | 4 ++ src/runner.rs | 48 +++++++++++---- src/spectacle.rs | 0 8 files changed, 142 insertions(+), 140 deletions(-) delete mode 100644 spectacle.toml create mode 100644 spectacle.yml create mode 100644 src/spectacle.rs diff --git a/Cargo.lock b/Cargo.lock index 74df9a2..2d83d2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,6 +224,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + [[package]] name = "heck" version = "0.3.3" @@ -248,6 +254,16 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "indexmap" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "instant" version = "0.1.12" @@ -284,6 +300,12 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "linked-hash-map" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" + [[package]] name = "lock_api" version = "0.4.5" @@ -493,6 +515,9 @@ version = "0.1.0" dependencies = [ "bluer", "futures", + "serde", + "serde_derive", + "serde_yaml", "tokio", ] @@ -510,18 +535,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.134" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96b3c34c1690edf8174f5b289a336ab03f568a4460d8c6df75f2f3a692b3bc6a" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.134" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784ed1fbfa13fe191077537b0d70ec8ad1e903cfe04831da608aa36457cb653d" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" dependencies = [ "proc-macro2", "quote", @@ -539,6 +564,18 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0" +dependencies = [ + "indexmap", + "ryu", + "serde", + "yaml-rust", +] + [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -693,3 +730,12 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] diff --git a/Cargo.toml b/Cargo.toml index c55078e..382813a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,8 @@ edition = "2018" [dependencies] bluer = "0.13.1" tokio = {version = "1.15.0", features = ["full"]} -futures = "0.3.19" \ No newline at end of file +futures = "0.3.19" + +serde_derive = "1.0.136" +serde = "1.0.136" +serde_yaml = "0.8" \ No newline at end of file diff --git a/spectacle.toml b/spectacle.toml deleted file mode 100644 index 50d51ad..0000000 --- a/spectacle.toml +++ /dev/null @@ -1,13 +0,0 @@ - -[pattern] -name = "fade" -period = "300" -nbr_iterations = "25" -initial_color = " - -[pattern] -name = "rainbow" -period = "100" -step = "10" - - diff --git a/spectacle.yml b/spectacle.yml new file mode 100644 index 0000000..b79a2ed --- /dev/null +++ b/spectacle.yml @@ -0,0 +1 @@ +let yaml = serde_yaml::to_string(&runner).unwrap(); \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 335fed8..363a95a 100644 --- a/src/main.rs +++ b/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::::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 -// 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:: { current_iteration: 0,}; -// -// -// for strip in pattern { -// write_strip(strip, char).await?; -// sleep(Duration::from_millis(50)).await; -// } -// -// Ok(()) -// } - pub async fn write_strip( data: &Strip, char: &Characteristic, @@ -141,36 +108,3 @@ pub async fn write_strip( char.write(&*frame).await?; Ok(()) } - -// async fn find_neopixel_service(device: &Device) -> bluer::Result> { -// 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) -// } diff --git a/src/patterns.rs b/src/patterns.rs index 40e9c8d..69750db 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -4,6 +4,7 @@ pub(crate) enum patterns { Rainbow(Rainbow), Fade(Fade), ColorWipe(ColorWipe), + Scanner(Scanner), } #[derive(Clone, Copy, Debug, Default)] @@ -59,6 +60,7 @@ impl Default for Strip { /// Rainbow pattern ///////////////////////////////////////////////// +#[derive(Serialize, Deserialize, Debug)] pub struct Rainbow { pub(crate) current_iteration: usize, pub(crate) max_iteration: Option, @@ -116,6 +118,7 @@ fn wheel(index: u8) -> PixelColor { /// Colorwipe pattern //////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// +#[derive(Serialize, Deserialize, Debug)] pub struct ColorWipe { pub(crate) current_iteration: usize, pub(crate) color: PixelColor, @@ -145,6 +148,7 @@ impl Iterator for ColorWipe { /// fade pattern //////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// +#[derive(Serialize, Deserialize, Debug)] pub struct Fade { pub(crate) current_iteration: usize, pub(crate) nbr_iterations: usize, diff --git a/src/runner.rs b/src/runner.rs index f88ad11..cea0adb 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -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 { - pattern : Box>>, - period: Duration, +#[derive(Serialize, Deserialize, Debug)] +pub struct Context { + pub(crate) pattern: Patterns, + pub period: Duration, } -struct Runner { - list : Vec>, +impl IntoIterator for Runner { + type Item = Context; + type IntoIter = > 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(Vec>); + impl Runner { - async fn runner_loop(self, tx: Sender>) { - for mut context in self.list { + pub fn new() -> Runner { + let runner = Vec::>::new(); + Runner(runner) + } + + pub fn push(&mut self, value: Context) { + self.0.push(value) + } + + pub async fn runner_loop(self, tx: Sender>) { + 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 Runner { } } } + + pub fn load_from_file(file: &std::path::Path) -> Runner { + let file = fs::read_to_string(file).unwrap(); + let spectacle: Runner = serde_yaml::from_str(&*file).unwrap(); + spectacle + } } diff --git a/src/spectacle.rs b/src/spectacle.rs new file mode 100644 index 0000000..e69de29