add hotreplug function for devices
This commit is contained in:
parent
a2ab8ac808
commit
3996e1b09f
|
@ -27,13 +27,19 @@ async fn has_service(device: &Device, uuid: &Uuid) -> Result<bool> {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid) -> Result<()> {
|
pub(crate) async fn bluetooth_scan(
|
||||||
|
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();
|
||||||
pin_mut!(discover);
|
pin_mut!(discover);
|
||||||
while let Some(evt) = discover.next().await {
|
while let Some(evt) = discover.next().await {
|
||||||
if let AdapterEvent::DeviceAdded(addr) = evt {
|
match evt {
|
||||||
|
AdapterEvent::DeviceAdded(addr) => {
|
||||||
|
println!("new device {}", addr);
|
||||||
if already_scanned.contains(&addr) {
|
if already_scanned.contains(&addr) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -49,6 +55,13 @@ async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid)
|
||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
AdapterEvent::DeviceRemoved(addr) => {
|
||||||
|
already_scanned.remove(&addr);
|
||||||
|
adapter.remove_device(addr).await;
|
||||||
|
println!("device {} removed", addr);
|
||||||
|
}
|
||||||
|
AdapterEvent::PropertyChanged(_) => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
65
src/main.rs
65
src/main.rs
|
@ -4,8 +4,8 @@ mod patterns;
|
||||||
use crate::patterns::{ColorWipe, Fade, PixelColor, Rainbow};
|
use crate::patterns::{ColorWipe, Fade, PixelColor, Rainbow};
|
||||||
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;
|
||||||
|
|
||||||
const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8);
|
const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8);
|
||||||
|
@ -17,25 +17,18 @@ 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 devices = bluetooth::scan_devices(adapter, SERVICE_UUID).await;
|
let (tx_scan, mut rx_scan) = mpsc::channel(3);
|
||||||
|
|
||||||
let mut pixels = Vec::<PixelColor>::new();
|
tokio::spawn(bluetooth::bluetooth_scan(
|
||||||
|
tx_scan,
|
||||||
for _ in 0..STRIP_SIZE {
|
adapter.clone(),
|
||||||
pixels.push(PixelColor {
|
SERVICE_UUID,
|
||||||
red: 255,
|
));
|
||||||
green: 0,
|
|
||||||
blue: 0,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// let strip = Strip::<STRIP_SIZE> {
|
|
||||||
// strip: <[PixelColor; 25]>::try_from(pixels).unwrap(),
|
|
||||||
// };
|
|
||||||
|
|
||||||
let (tx, rx) = watch::channel(Strip::<STRIP_SIZE>::default());
|
let (tx, rx) = watch::channel(Strip::<STRIP_SIZE>::default());
|
||||||
|
|
||||||
for device in devices {
|
tokio::spawn(async move {
|
||||||
|
while let Some(device) = rx_scan.recv().await {
|
||||||
bluetooth::connect(&device, 3).await?;
|
bluetooth::connect(&device, 3).await?;
|
||||||
let char = bluetooth::get_char(&device, SERVICE_UUID, PIXEL_DATA_UUID).await?;
|
let char = bluetooth::get_char(&device, SERVICE_UUID, PIXEL_DATA_UUID).await?;
|
||||||
if let Some(char) = char {
|
if let Some(char) = char {
|
||||||
|
@ -44,58 +37,74 @@ async fn main() -> bluer::Result<()> {
|
||||||
println!("device connected : {}", &device.address());
|
println!("device connected : {}", &device.address());
|
||||||
while rx_device.changed().await.is_ok() {
|
while rx_device.changed().await.is_ok() {
|
||||||
let strip = *rx_device.borrow();
|
let strip = *rx_device.borrow();
|
||||||
write_strip(&strip, &char).await.unwrap();
|
if write_strip(&strip, &char).await.is_err() {
|
||||||
|
break;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
println!("device {} disconnected", &device.address());
|
||||||
|
// drop(rx_device);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bluer::Result::Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
let mut colorwipe = ColorWipe::<25>{
|
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,
|
current_iteration: 0,
|
||||||
color: PixelColor {
|
color: PixelColor {
|
||||||
red: 0,
|
red: 0,
|
||||||
green: 255,
|
green: 255,
|
||||||
blue: 0
|
blue: 0,
|
||||||
},
|
},
|
||||||
infinite: true
|
infinite: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
let mut fade = Fade::<25> {
|
let mut fade = Fade::<25> {
|
||||||
current_iteration: 0,
|
current_iteration: 0,
|
||||||
nbr_iterations: 25,
|
nbr_iterations: 25,
|
||||||
begin_color: PixelColor {
|
begin_color: PixelColor {
|
||||||
red: 255,
|
red: 255,
|
||||||
green: 0,
|
green: 0,
|
||||||
blue: 0
|
blue: 0,
|
||||||
},
|
},
|
||||||
end_color: PixelColor {
|
end_color: PixelColor {
|
||||||
red: 0,
|
red: 0,
|
||||||
green: 0,
|
green: 0,
|
||||||
blue: 255
|
blue: 255,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut pattern = Rainbow::<25>{
|
let mut pattern = Rainbow::<25> {
|
||||||
current_iteration: 0,
|
current_iteration: 0,
|
||||||
max_iteration: None,
|
max_iteration: None,
|
||||||
step: Some(10),
|
step: Some(10),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut strip = fade.next().unwrap();
|
let mut strip = fade.next().unwrap();
|
||||||
|
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||||
|
println!("starting");
|
||||||
while tx.send(strip).is_ok() {
|
while tx.send(strip).is_ok() {
|
||||||
if let Some(value) = fade.next() {
|
if let Some(value) = fade.next() {
|
||||||
strip = value;
|
strip = value;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
tokio::time::sleep(Duration::from_millis(1000)).await;
|
||||||
}
|
}
|
||||||
|
println!("starting rainbow");
|
||||||
while tx.send(pattern.next().unwrap()).is_ok() {
|
while tx.send(pattern.next().unwrap()).is_ok() {
|
||||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
}
|
}
|
||||||
|
println!("error sending value");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,7 +69,7 @@ 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 = self.step.unwrap_or(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)
|
||||||
|
@ -106,15 +106,14 @@ fn wheel(index: u8) -> PixelColor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
/// Colorwipe pattern ////////////////////////////////////////////////////
|
/// Colorwipe pattern ////////////////////////////////////////////////////
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
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,
|
||||||
pub (crate) infinite: bool,
|
pub(crate) infinite: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Iterator for ColorWipe<N> {
|
impl<const N: usize> Iterator for ColorWipe<N> {
|
||||||
|
@ -128,7 +127,6 @@ impl<const N: usize> Iterator for ColorWipe<N> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for i in 0..self.current_iteration {
|
for i in 0..self.current_iteration {
|
||||||
strip.strip[i] = self.color;
|
strip.strip[i] = self.color;
|
||||||
}
|
}
|
||||||
|
@ -148,8 +146,8 @@ pub struct Fade<const N: usize> {
|
||||||
pub(crate) end_color: PixelColor,
|
pub(crate) end_color: PixelColor,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fadeValue(value_start: u8, value_end: u8, index: usize, nbr_iter: usize) -> u8 {
|
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
|
((value_start as usize * (nbr_iter - index) + value_end as usize * index) / nbr_iter) as u8
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Iterator for Fade<N> {
|
impl<const N: usize> Iterator for Fade<N> {
|
||||||
|
@ -162,15 +160,26 @@ impl<const N: usize> Iterator for Fade<N> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let red = fadeValue(self.begin_color.red, self.end_color.red, self.current_iteration, self.nbr_iterations);
|
let red = fade_value(
|
||||||
let green = fadeValue(self.begin_color.green, self.end_color.green, self.current_iteration, self.nbr_iterations);
|
self.begin_color.red,
|
||||||
let blue = fadeValue(self.begin_color.blue, self.end_color.blue, self.current_iteration, self.nbr_iterations);
|
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 {
|
let current_color = PixelColor { red, green, blue };
|
||||||
red,
|
|
||||||
green,
|
|
||||||
blue
|
|
||||||
};
|
|
||||||
|
|
||||||
strip.fill(current_color);
|
strip.fill(current_color);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user