diff --git a/src/bluetooth.rs b/src/bluetooth.rs index 9c2dd53..e7210b6 100644 --- a/src/bluetooth.rs +++ b/src/bluetooth.rs @@ -35,7 +35,6 @@ async fn bluetooth_scan(tx: mpsc::Sender, adapter: Adapter, uuid: Uuid) while let Some(evt) = discover.next().await { match evt { AdapterEvent::DeviceAdded(addr) => { - println!("device adress {}", addr); if already_scanned.contains(&addr) { continue; } @@ -54,25 +53,18 @@ async fn bluetooth_scan(tx: mpsc::Sender, adapter: Adapter, uuid: Uuid) _ => {} } } - - println!("ended"); Ok(()) } pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec { - println!("start scanning devices"); let (tx, mut rx) = mpsc::channel(4); let timeout = tokio::time::timeout(Duration::from_secs(30), bluetooth_scan(tx, adapter, uuid)); let scan = tokio::spawn(timeout); - println!("good"); let mut devices: Vec = Vec::new(); while let Some(device) = rx.recv().await { - println!("new device received {}", device.address()); devices.push(device); - println!("len {}", devices.len()); if devices.len() >= 2 { - println!("drop"); drop(scan); break; } @@ -108,7 +100,6 @@ pub async fn get_char( for service in device.services().await? { if service_uuid == service.uuid().await? { for char in service.characteristics().await? { - println!("uuid : {}", &char.uuid().await?); if char_uuid == char.uuid().await? { return Ok(Some(char)); } diff --git a/src/main.rs b/src/main.rs index 10fb1a5..1677830 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,13 @@ mod bluetooth; mod patterns; +use std::convert::TryFrom; +use std::thread::sleep; +use std::time::Duration; use bluer::gatt::remote::Characteristic; use patterns::Strip; use tokio::sync::watch; +use crate::patterns::PixelColor; const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8); 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 mut pixels = Vec::::new(); + + for _ in 0..STRIP_SIZE{ + pixels.push(PixelColor{ + red: 255, green: 0, blue:0 + }) + } + + + let strip = Strip:: { + strip : <[PixelColor; 25]>::try_from(pixels).unwrap() + }; + let (tx, rx) = watch::channel(Strip::::default()); + for device in devices { 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 = rx.clone(); + let mut rx_device = rx.clone(); tokio::spawn(async move { - while rx.changed().await.is_ok() { - let strip = *rx.borrow(); - write_strip(&strip, &char).await; + println!("device connected : {}", &device.address()); + while rx_device.changed().await.is_ok() { + println!("ok"); + let strip = rx_device.borrow().clone(); + write_strip(&strip, &char).await.unwrap(); } - }) - .await; + }); } - - println!("{:?}", device); } + loop { - Ok(()) + tx.send(strip); + tokio::time::sleep(Duration::from_secs(1)).await; + tx.send(Strip::::default()); + tokio::time::sleep(Duration::from_secs(1)).await; + + } } +// fn create_pattern_list() -> Vec // async fn send_seq(char: &Characteristic) -> bluer::Result<()> { // println!(" Characteristic flags : {:?}, ", char.flags().await?); // diff --git a/src/patterns.rs b/src/patterns.rs index 51bf5bc..6d40766 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -19,6 +19,7 @@ impl fmt::Display for PixelColor { } } + impl Default for PixelColor { fn default() -> Self { PixelColor { @@ -29,6 +30,8 @@ impl Default for PixelColor { } } + + #[derive(Copy, Clone)] pub struct Strip { pub strip: [PixelColor; N],