read show from file instead of writing it in the code

This commit is contained in:
Tobias Ollive 2022-03-02 13:44:28 +01:00
parent b43490e876
commit 7521c87460
8 changed files with 142 additions and 140 deletions

54
Cargo.lock generated
View File

@ -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",
]

View File

@ -9,3 +9,7 @@ edition = "2018"
bluer = "0.13.1"
tokio = {version = "1.15.0", features = ["full"]}
futures = "0.3.19"
serde_derive = "1.0.136"
serde = "1.0.136"
serde_yaml = "0.8"

View File

@ -1,13 +0,0 @@
[pattern]
name = "fade"
period = "300"
nbr_iterations = "25"
initial_color = "
[pattern]
name = "rainbow"
period = "100"
step = "10"

1
spectacle.yml Normal file
View File

@ -0,0 +1 @@
let yaml = serde_yaml::to_string(&runner).unwrap();

View File

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

View File

@ -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,

View File

@ -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
View File