Compare commits

..

No commits in common. "3996e1b09ffe76e36ea7c30e0bc3efd5774f4b4e" and "517e98dea2ef14308e52a4b4d5220acf106e514c" have entirely different histories.

3 changed files with 58 additions and 185 deletions

View File

@ -27,11 +27,7 @@ async fn has_service(device: &Device, uuid: &Uuid) -> Result<bool> {
Ok(false) Ok(false)
} }
pub(crate) async fn bluetooth_scan( async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid) -> Result<()> {
tx: mpsc::Sender<Device>,
adapter: Adapter,
uuid: Uuid,
) -> Result<()> {
println!("start bluetooth scan"); println!("start bluetooth scan");
let discover = adapter.discover_devices().await?; let discover = adapter.discover_devices().await?;
let mut already_scanned: HashSet<Address> = HashSet::new(); let mut already_scanned: HashSet<Address> = HashSet::new();
@ -39,7 +35,6 @@ pub(crate) async fn bluetooth_scan(
while let Some(evt) = discover.next().await { while let Some(evt) = discover.next().await {
match evt { match evt {
AdapterEvent::DeviceAdded(addr) => { AdapterEvent::DeviceAdded(addr) => {
println!("new device {}", addr);
if already_scanned.contains(&addr) { if already_scanned.contains(&addr) {
continue; continue;
} }
@ -55,12 +50,7 @@ pub(crate) async fn bluetooth_scan(
Err(_) => continue, Err(_) => continue,
} }
} }
AdapterEvent::DeviceRemoved(addr) => { _ => {}
already_scanned.remove(&addr);
adapter.remove_device(addr).await;
println!("device {} removed", addr);
}
AdapterEvent::PropertyChanged(_) => {}
} }
} }
Ok(()) Ok(())

View File

@ -1,12 +1,13 @@
mod bluetooth; mod bluetooth;
mod patterns; mod patterns;
use crate::patterns::{ColorWipe, Fade, PixelColor, Rainbow}; use crate::patterns::PixelColor;
use bluer::gatt::remote::Characteristic; use bluer::gatt::remote::Characteristic;
use patterns::Strip; use patterns::Strip;
use std::convert::TryFrom;
use std::time::Duration; use std::time::Duration;
use tokio::sync::mpsc;
use tokio::sync::watch; use tokio::sync::watch;
use tokio::sync::watch::error::SendError;
const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8); const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8);
const PIXEL_DATA_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0903c33242a893bd25e905756cb8); const PIXEL_DATA_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0903c33242a893bd25e905756cb8);
@ -17,39 +18,10 @@ const BASE_STRIP_DATA: [u8; 3] = [0x00, 0x00, 0x01];
async fn main() -> bluer::Result<()> { async fn main() -> bluer::Result<()> {
let adapter = bluetooth::create_session().await?; let adapter = bluetooth::create_session().await?;
let (tx_scan, mut rx_scan) = mpsc::channel(3); let devices = bluetooth::scan_devices(adapter, SERVICE_UUID).await;
tokio::spawn(bluetooth::bluetooth_scan(
tx_scan,
adapter.clone(),
SERVICE_UUID,
));
let (tx, rx) = watch::channel(Strip::<STRIP_SIZE>::default());
tokio::spawn(async move {
while let Some(device) = rx_scan.recv().await {
bluetooth::connect(&device, 3).await?;
let char = bluetooth::get_char(&device, SERVICE_UUID, PIXEL_DATA_UUID).await?;
if let Some(char) = char {
let mut rx_device = rx.clone();
tokio::spawn(async move {
println!("device connected : {}", &device.address());
while rx_device.changed().await.is_ok() {
let strip = *rx_device.borrow();
if write_strip(&strip, &char).await.is_err() {
break;
};
}
println!("device {} disconnected", &device.address());
// drop(rx_device);
});
}
}
bluer::Result::Ok(())
});
let mut pixels = Vec::<PixelColor>::new(); let mut pixels = Vec::<PixelColor>::new();
for _ in 0..STRIP_SIZE { for _ in 0..STRIP_SIZE {
pixels.push(PixelColor { pixels.push(PixelColor {
red: 255, red: 255,
@ -58,53 +30,43 @@ async fn main() -> bluer::Result<()> {
}) })
} }
let mut _colorwipe = ColorWipe::<25> { let strip = Strip::<STRIP_SIZE> {
current_iteration: 0, strip: <[PixelColor; 25]>::try_from(pixels).unwrap(),
color: PixelColor {
red: 0,
green: 255,
blue: 0,
},
infinite: true,
}; };
let mut fade = Fade::<25> { let (tx, rx) = watch::channel(Strip::<STRIP_SIZE>::default());
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> { for device in devices {
current_iteration: 0, bluetooth::connect(&device, 3).await?;
max_iteration: None, let char = bluetooth::get_char(&device, SERVICE_UUID, PIXEL_DATA_UUID).await?;
step: Some(10), if let Some(char) = char {
}; let mut rx_device = rx.clone();
tokio::spawn(async move {
let mut strip = fade.next().unwrap(); println!("device connected : {}", &device.address());
tokio::time::sleep(Duration::from_secs(5)).await; while rx_device.changed().await.is_ok() {
println!("starting"); println!("ok");
while tx.send(strip).is_ok() { let strip = rx_device.borrow().clone();
if let Some(value) = fade.next() { write_strip(&strip, &char).await.unwrap();
strip = value; }
} else { });
}
}
loop {
match tx.send(strip) {
Ok(_) => {}
Err(_) => {
break; break;
} }
tokio::time::sleep(Duration::from_millis(1000)).await;
} }
println!("starting rainbow"); tokio::time::sleep(Duration::from_secs(1)).await;
while tx.send(pattern.next().unwrap()).is_ok() { match tx.send(Strip::<STRIP_SIZE>::default()) {
tokio::time::sleep(Duration::from_millis(100)).await; Ok(_) => {}
Err(_) => {
break;
}
}
tokio::time::sleep(Duration::from_secs(1)).await;
} }
println!("error sending value");
Ok(()) Ok(())
} }
@ -134,7 +96,7 @@ pub async fn write_strip<const N: usize>(
char: &Characteristic, char: &Characteristic,
) -> bluer::Result<()> { ) -> bluer::Result<()> {
let frame = [BASE_STRIP_DATA.to_vec(), data.to_array()].concat(); let frame = [BASE_STRIP_DATA.to_vec(), data.to_array()].concat();
// print!("{:?}", frame); print!("{:?}", frame);
char.write(&*frame).await?; char.write(&*frame).await?;
Ok(()) Ok(())
} }

View File

@ -1,6 +1,6 @@
use std::fmt; use std::fmt;
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug)]
pub struct PixelColor { pub struct PixelColor {
pub(crate) red: u8, pub(crate) red: u8,
pub(crate) green: u8, pub(crate) green: u8,
@ -19,28 +19,34 @@ impl fmt::Display for PixelColor {
} }
} }
impl Default for PixelColor {
fn default() -> Self {
PixelColor {
red: 0,
green: 0,
blue: 0,
}
}
}
#[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],
} }
impl<const N: usize> Strip<N> { impl<const N: usize> Strip<N> {
pub fn to_array(self) -> Vec<u8> { pub fn to_array(&self) -> Vec<u8> {
let mut data: Vec<u8> = vec![]; let mut data: Vec<u8> = vec![];
for i in 0..N { for i in 0..N {
data.append(&mut vec![ data.append(&mut vec![
self.strip[i].green,
self.strip[i].red, self.strip[i].red,
self.strip[i].green,
self.strip[i].blue, self.strip[i].blue,
]); ]);
} }
data data
} }
pub fn fill(&mut self, color: PixelColor) {
self.strip = [color; N];
}
} }
impl<const N: usize> Default for Strip<N> { impl<const N: usize> Default for Strip<N> {
@ -51,15 +57,12 @@ impl<const N: usize> Default for Strip<N> {
} }
} }
/// Rainbow pattern ///////////////////////////////////////////////// pub struct RainbowPattern<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>,
pub(crate) step: Option<usize>,
} }
impl<const N: usize> Iterator for Rainbow<N> { impl<const N: usize> Iterator for RainbowPattern<N> {
type Item = Strip<N>; type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@ -69,12 +72,12 @@ impl<const N: usize> Iterator for Rainbow<N> {
} }
} }
let mut strip = Strip::default(); let mut strip = Strip::default();
let step = self.step.unwrap_or(255 / N); let step = 255 / N;
for i in 0..N { for i in 0..N {
let pos = (i * step + self.current_iteration) as u8; let pos = (i * step + self.current_iteration) as u8;
strip.strip[i] = wheel(pos) strip.strip[i] = wheel(pos)
} }
self.current_iteration += 1; self.current_iteration = self.current_iteration + 1;
Some(strip) Some(strip)
} }
} }
@ -97,93 +100,11 @@ fn wheel(index: u8) -> PixelColor {
} }
_ => { _ => {
let pos = pos - 170; let pos = pos - 170;
PixelColor { return PixelColor {
red: pos * 3, red: pos * 3,
green: 255 - (pos * 3), green: 255 - (pos * 3),
blue: 0, blue: 0,
};
} }
} }
} }
}
//////////////////////////////////////////////////////////////////////////
/// Colorwipe pattern ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
pub struct ColorWipe<const N: usize> {
pub(crate) current_iteration: usize,
pub(crate) color: PixelColor,
pub(crate) infinite: bool,
}
impl<const N: usize> Iterator for ColorWipe<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
let mut strip = Strip::default();
if self.infinite {
self.current_iteration %= N;
} else if self.current_iteration >= N {
return None;
}
for i in 0..self.current_iteration {
strip.strip[i] = self.color;
}
self.current_iteration += 1;
Some(strip)
}
}
//////////////////////////////////////////////////////////////////////////
/// fade pattern ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
pub struct Fade<const N: usize> {
pub(crate) current_iteration: usize,
pub(crate) nbr_iterations: usize,
pub(crate) begin_color: PixelColor,
pub(crate) end_color: PixelColor,
}
fn fade_value(value_start: u8, value_end: u8, index: usize, nbr_iter: usize) -> u8 {
((value_start as usize * (nbr_iter - index) + value_end as usize * index) / nbr_iter) as u8
}
impl<const N: usize> Iterator for Fade<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
let mut strip = Strip::default();
if self.current_iteration >= self.nbr_iterations {
return None;
}
let red = fade_value(
self.begin_color.red,
self.end_color.red,
self.current_iteration,
self.nbr_iterations,
);
let green = fade_value(
self.begin_color.green,
self.end_color.green,
self.current_iteration,
self.nbr_iterations,
);
let blue = fade_value(
self.begin_color.blue,
self.end_color.blue,
self.current_iteration,
self.nbr_iterations,
);
let current_color = PixelColor { red, green, blue };
strip.fill(current_color);
self.current_iteration += 1;
Some(strip)
}
}