Compare commits

...

3 Commits

Author SHA1 Message Date
Tobias Ollive
20983a8c08 add show example and scanner pattern 2022-03-02 13:45:55 +01:00
Tobias Ollive
7521c87460 read show from file instead of writing it in the code 2022-03-02 13:44:28 +01:00
Tobias Ollive
b43490e876 WIP working on main patterns loop 2022-02-23 19:48:38 +01:00
7 changed files with 276 additions and 115 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

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

36
spectacle.yml Normal file
View File

@ -0,0 +1,36 @@
- 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,9 +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;
@ -15,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);
@ -49,86 +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,
@ -138,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

@ -1,6 +1,32 @@
use serde_derive::{Deserialize, Serialize};
use std::cmp::{max, min, Ordering};
use std::convert::Infallible;
use std::fmt;
use std::ops::Div;
#[derive(Clone, Copy, Debug, Default)]
#[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>),
}
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,
@ -19,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],
@ -53,6 +91,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>,
@ -110,6 +149,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,
@ -139,6 +179,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,
@ -187,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)
}
}

57
src/runner.rs Normal file
View File

@ -0,0 +1,57 @@
use crate::patterns::Patterns;
use crate::Strip;
use serde_derive::{Deserialize, Serialize};
use std::fs;
use std::time::Duration;
use tokio::sync::watch::Sender;
#[derive(Serialize, Deserialize, Debug)]
pub struct Context<const N: usize> {
pub(crate) pattern: Patterns<N>,
pub period: Duration,
}
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> {
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;
} else {
break;
}
tokio::time::sleep(delay).await;
}
}
}
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