use tokio task to control as much devices as needed

This commit is contained in:
Tobias Ollive 2022-01-31 17:05:59 +01:00
parent 1098b7ba6b
commit 142fdb44d7
3 changed files with 36 additions and 18 deletions

View File

@ -35,7 +35,6 @@ async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid)
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!("device adress {}", addr);
if already_scanned.contains(&addr) { if already_scanned.contains(&addr) {
continue; continue;
} }
@ -54,25 +53,18 @@ async fn bluetooth_scan(tx: mpsc::Sender<Device>, adapter: Adapter, uuid: Uuid)
_ => {} _ => {}
} }
} }
println!("ended");
Ok(()) Ok(())
} }
pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec<Device> { pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec<Device> {
println!("start scanning devices");
let (tx, mut rx) = mpsc::channel(4); let (tx, mut rx) = mpsc::channel(4);
let timeout = tokio::time::timeout(Duration::from_secs(30), bluetooth_scan(tx, adapter, uuid)); let timeout = tokio::time::timeout(Duration::from_secs(30), bluetooth_scan(tx, adapter, uuid));
let scan = tokio::spawn(timeout); let scan = tokio::spawn(timeout);
println!("good");
let mut devices: Vec<Device> = Vec::new(); let mut devices: Vec<Device> = Vec::new();
while let Some(device) = rx.recv().await { while let Some(device) = rx.recv().await {
println!("new device received {}", device.address());
devices.push(device); devices.push(device);
println!("len {}", devices.len());
if devices.len() >= 2 { if devices.len() >= 2 {
println!("drop");
drop(scan); drop(scan);
break; break;
} }
@ -108,7 +100,6 @@ pub async fn get_char(
for service in device.services().await? { for service in device.services().await? {
if service_uuid == service.uuid().await? { if service_uuid == service.uuid().await? {
for char in service.characteristics().await? { for char in service.characteristics().await? {
println!("uuid : {}", &char.uuid().await?);
if char_uuid == char.uuid().await? { if char_uuid == char.uuid().await? {
return Ok(Some(char)); return Ok(Some(char));
} }

View File

@ -1,9 +1,13 @@
mod bluetooth; mod bluetooth;
mod patterns; mod patterns;
use std::convert::TryFrom;
use std::thread::sleep;
use std::time::Duration;
use bluer::gatt::remote::Characteristic; use bluer::gatt::remote::Characteristic;
use patterns::Strip; use patterns::Strip;
use tokio::sync::watch; use tokio::sync::watch;
use crate::patterns::PixelColor;
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);
@ -16,28 +20,48 @@ async fn main() -> bluer::Result<()> {
let devices = bluetooth::scan_devices(adapter, SERVICE_UUID).await; let devices = bluetooth::scan_devices(adapter, SERVICE_UUID).await;
let mut pixels = Vec::<PixelColor>::new();
for _ in 0..STRIP_SIZE{
pixels.push(PixelColor{
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 { for device in devices {
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 {
let mut rx = rx.clone(); let mut rx_device = rx.clone();
tokio::spawn(async move { tokio::spawn(async move {
while rx.changed().await.is_ok() { println!("device connected : {}", &device.address());
let strip = *rx.borrow(); while rx_device.changed().await.is_ok() {
write_strip(&strip, &char).await; println!("ok");
let strip = rx_device.borrow().clone();
write_strip(&strip, &char).await.unwrap();
} }
}) });
.await;
} }
}
loop {
println!("{:?}", device); tx.send(strip);
} tokio::time::sleep(Duration::from_secs(1)).await;
tx.send(Strip::<STRIP_SIZE>::default());
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(()) }
} }
// fn create_pattern_list() -> Vec<patterns>
// async fn send_seq(char: &Characteristic) -> bluer::Result<()> { // async fn send_seq(char: &Characteristic) -> bluer::Result<()> {
// println!(" Characteristic flags : {:?}, ", char.flags().await?); // println!(" Characteristic flags : {:?}, ", char.flags().await?);
// //

View File

@ -19,6 +19,7 @@ impl fmt::Display for PixelColor {
} }
} }
impl Default for PixelColor { impl Default for PixelColor {
fn default() -> Self { fn default() -> Self {
PixelColor { PixelColor {
@ -29,6 +30,8 @@ impl Default for PixelColor {
} }
} }
#[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],