Compare commits

..

No commits in common. "20983a8c0827b7f779e168c8358a51bef54d7d64" and "3996e1b09ffe76e36ea7c30e0bc3efd5774f4b4e" have entirely different histories.

7 changed files with 115 additions and 276 deletions

54
Cargo.lock generated
View File

@ -224,12 +224,6 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.3.3" version = "0.3.3"
@ -254,16 +248,6 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" 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]] [[package]]
name = "instant" name = "instant"
version = "0.1.12" version = "0.1.12"
@ -300,12 +284,6 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "linked-hash-map"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.5" version = "0.4.5"
@ -515,9 +493,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bluer", "bluer",
"futures", "futures",
"serde",
"serde_derive",
"serde_yaml",
"tokio", "tokio",
] ]
@ -535,18 +510,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.136" version = "1.0.134"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" checksum = "96b3c34c1690edf8174f5b289a336ab03f568a4460d8c6df75f2f3a692b3bc6a"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.136" version = "1.0.134"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" checksum = "784ed1fbfa13fe191077537b0d70ec8ad1e903cfe04831da608aa36457cb653d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -564,18 +539,6 @@ dependencies = [
"serde", "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]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.0" version = "1.4.0"
@ -730,12 +693,3 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 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,8 +8,4 @@ edition = "2018"
[dependencies] [dependencies]
bluer = "0.13.1" bluer = "0.13.1"
tokio = {version = "1.15.0", features = ["full"]} 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"

View File

@ -1,36 +0,0 @@
- 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,9 @@
mod bluetooth; mod bluetooth;
mod patterns; mod patterns;
mod runner;
mod spectacle;
use crate::patterns::{ColorWipe, Fade, Patterns, PixelColor}; use crate::patterns::{ColorWipe, Fade, PixelColor, Rainbow};
use crate::runner::Runner;
use bluer::gatt::remote::Characteristic; use bluer::gatt::remote::Characteristic;
use patterns::Strip; use patterns::Strip;
use std::path::Path;
use std::time::Duration; use std::time::Duration;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::sync::watch; use tokio::sync::watch;
@ -19,45 +15,6 @@ const BASE_STRIP_DATA: [u8; 3] = [0x00, 0x00, 0x01];
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> bluer::Result<()> { 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 adapter = bluetooth::create_session().await?;
let (tx_scan, mut rx_scan) = mpsc::channel(3); let (tx_scan, mut rx_scan) = mpsc::channel(3);
@ -92,13 +49,86 @@ async fn main() -> bluer::Result<()> {
bluer::Result::Ok(()) bluer::Result::Ok(())
}); });
println!("starting"); let mut pixels = Vec::<PixelColor>::new();
config.runner_loop(tx).await; 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; 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;
}
println!("error sending value"); println!("error sending value");
Ok(()) 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>( pub async fn write_strip<const N: usize>(
data: &Strip<N>, data: &Strip<N>,
char: &Characteristic, char: &Characteristic,
@ -108,3 +138,36 @@ pub async fn write_strip<const N: usize>(
char.write(&*frame).await?; char.write(&*frame).await?;
Ok(()) 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,32 +1,6 @@
use serde_derive::{Deserialize, Serialize};
use std::cmp::{max, min, Ordering};
use std::convert::Infallible;
use std::fmt; use std::fmt;
use std::ops::Div;
#[derive(Serialize, Deserialize, Debug)] #[derive(Clone, Copy, Debug, Default)]
#[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 struct PixelColor {
pub(crate) red: u8, pub(crate) red: u8,
pub(crate) green: u8, pub(crate) green: u8,
@ -45,18 +19,6 @@ 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)] #[derive(Copy, Clone)]
pub struct Strip<const N: usize> { pub struct Strip<const N: usize> {
pub strip: [PixelColor; N], pub strip: [PixelColor; N],
@ -91,7 +53,6 @@ impl<const N: usize> Default for Strip<N> {
/// Rainbow pattern ///////////////////////////////////////////////// /// Rainbow pattern /////////////////////////////////////////////////
#[derive(Serialize, Deserialize, Debug)]
pub struct Rainbow<const N: usize> { pub struct Rainbow<const N: usize> {
pub(crate) current_iteration: usize, pub(crate) current_iteration: usize,
pub(crate) max_iteration: Option<usize>, pub(crate) max_iteration: Option<usize>,
@ -149,7 +110,6 @@ fn wheel(index: u8) -> PixelColor {
/// Colorwipe pattern //////////////////////////////////////////////////// /// Colorwipe pattern ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
#[derive(Serialize, Deserialize, Debug)]
pub struct ColorWipe<const N: usize> { pub struct ColorWipe<const N: usize> {
pub(crate) current_iteration: usize, pub(crate) current_iteration: usize,
pub(crate) color: PixelColor, pub(crate) color: PixelColor,
@ -179,7 +139,6 @@ impl<const N: usize> Iterator for ColorWipe<N> {
/// fade pattern //////////////////////////////////////////////////// /// fade pattern ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
#[derive(Serialize, Deserialize, Debug)]
pub struct Fade<const N: usize> { pub struct Fade<const N: usize> {
pub(crate) current_iteration: usize, pub(crate) current_iteration: usize,
pub(crate) nbr_iterations: usize, pub(crate) nbr_iterations: usize,
@ -228,43 +187,3 @@ impl<const N: usize> Iterator for Fade<N> {
Some(strip) 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)
}
}

View File

@ -1,57 +0,0 @@
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
}
}

View File