diff --git a/.gitignore b/.gitignore index bad1f81..925302b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ target/ **/*.rs.bk .idea/ +*.iml diff --git a/src/bluetooth.rs b/src/bluetooth.rs index 9039815..9c2dd53 100644 --- a/src/bluetooth.rs +++ b/src/bluetooth.rs @@ -1,16 +1,15 @@ +use bluer::gatt::remote::Characteristic; +use bluer::{Adapter, AdapterEvent, Address, Device, Result, Uuid}; +use futures::{pin_mut, StreamExt}; use std::collections::HashSet; use std::time::Duration; -use bluer::{Adapter, AdapterEvent, Address, Device, Result, Uuid}; -use bluer::gatt::remote::Characteristic; -use futures::{pin_mut, StreamExt}; use tokio::sync::mpsc; // const CONNECT_RETRIES: u8 = 3; // const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8); // const PIXEL_DATA_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0903c33242a893bd25e905756cb8); -pub(crate) async fn create_session() -> Result -{ +pub(crate) async fn create_session() -> Result { let session = bluer::Session::new().await?; let adapter_names = session.adapter_names().await?; let adapter_name = adapter_names.first().expect("no Bluetooth adapter found"); @@ -21,10 +20,9 @@ pub(crate) async fn create_session() -> Result } async fn has_service(device: &Device, uuid: &Uuid) -> Result { - let uuids = device.uuids().await?.unwrap_or_default(); if uuids.contains(uuid) { - return Ok(true) + return Ok(true); } Ok(false) } @@ -50,7 +48,7 @@ async fn bluetooth_scan(tx: mpsc::Sender, adapter: Adapter, uuid: Uuid) println!("found service in device {}", addr); } } - Err(_) => { continue } + Err(_) => continue, } } _ => {} @@ -61,7 +59,6 @@ async fn bluetooth_scan(tx: mpsc::Sender, adapter: Adapter, uuid: Uuid) Ok(()) } - pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec { println!("start scanning devices"); let (tx, mut rx) = mpsc::channel(4); @@ -70,7 +67,7 @@ pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec { let scan = tokio::spawn(timeout); println!("good"); let mut devices: Vec = 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); println!("len {}", devices.len()); @@ -86,7 +83,7 @@ pub(crate) async fn scan_devices(adapter: Adapter, uuid: Uuid) -> Vec { pub async fn connect(device: &Device, retries: u8) -> bluer::Result<()> { if device.is_connected().await? { - return Ok(()) + return Ok(()); } for i in 0..retries { match device.connect().await { @@ -95,7 +92,6 @@ pub async fn connect(device: &Device, retries: u8) -> bluer::Result<()> { println!("connection error ({}), retry…", i); } } - } Err(bluer::Error { @@ -104,18 +100,20 @@ pub async fn connect(device: &Device, retries: u8) -> bluer::Result<()> { }) } -pub async fn get_char(device: &Device, service_uuid: Uuid, char_uuid: Uuid) -> Result> { +pub async fn get_char( + device: &Device, + service_uuid: Uuid, + char_uuid: Uuid, +) -> Result> { 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)); - } - + 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)); } } } + } Ok(None) -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 5790686..10fb1a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,17 @@ -mod patterns; mod bluetooth; +mod patterns; -use patterns::Strip; use bluer::gatt::remote::Characteristic; +use patterns::Strip; use tokio::sync::watch; const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8); const PIXEL_DATA_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0903c33242a893bd25e905756cb8); const STRIP_SIZE: usize = 25; -const BASE_STRIP_DATA : [u8; 3] = [0x00, 0x00, 0x01]; - +const BASE_STRIP_DATA: [u8; 3] = [0x00, 0x00, 0x01]; #[tokio::main(flavor = "current_thread")] async fn main() -> bluer::Result<()> { - let adapter = bluetooth::create_session().await?; let devices = bluetooth::scan_devices(adapter, SERVICE_UUID).await; @@ -30,19 +28,16 @@ async fn main() -> bluer::Result<()> { let strip = *rx.borrow(); write_strip(&strip, &char).await; } - }).await; + }) + .await; } println!("{:?}", device); } - - Ok(()) } - - // async fn send_seq(char: &Characteristic) -> bluer::Result<()> { // println!(" Characteristic flags : {:?}, ", char.flags().await?); // @@ -63,15 +58,16 @@ async fn main() -> bluer::Result<()> { // Ok(()) // } -pub async fn write_strip(data: &Strip, char: &Characteristic) -> bluer::Result<()> { +pub async fn write_strip( + data: &Strip, + char: &Characteristic, +) -> bluer::Result<()> { let frame = [BASE_STRIP_DATA.to_vec(), data.to_array()].concat(); - print!("{:?}",frame); + print!("{:?}", frame); char.write(&*frame).await?; Ok(()) } - - // async fn find_neopixel_service(device: &Device) -> bluer::Result> { // let addr = device.address(); // let uuids = device.uuids().await?.unwrap_or_default(); @@ -103,4 +99,4 @@ pub async fn write_strip(data: &Strip, char: &Characteristic) // } // // Ok(None) -// } \ No newline at end of file +// } diff --git a/src/patterns.rs b/src/patterns.rs index b9f1ba4..51bf5bc 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -21,11 +21,14 @@ impl fmt::Display for PixelColor { impl Default for PixelColor { fn default() -> Self { - PixelColor { red: 0, green: 0, blue: 0 } + PixelColor { + red: 0, + green: 0, + blue: 0, + } } } - #[derive(Copy, Clone)] pub struct Strip { pub strip: [PixelColor; N], @@ -33,10 +36,14 @@ pub struct Strip { impl Strip { pub fn to_array(&self) -> Vec { - let mut data : Vec = vec![]; + let mut data: Vec = vec![]; for i in 0..N { - data.append(&mut vec![self.strip[i].red, self.strip[i].green, self.strip[i].blue]); + data.append(&mut vec![ + self.strip[i].red, + self.strip[i].green, + self.strip[i].blue, + ]); } data } @@ -44,33 +51,33 @@ impl Strip { impl Default for Strip { fn default() -> Self { - Strip { strip: [PixelColor::new(); N] } + Strip { + strip: [PixelColor::new(); N], + } } } - pub struct RainbowPattern { pub(crate) current_iteration: usize, - pub(crate) max_iteration: Option + pub(crate) max_iteration: Option, } - impl Iterator for RainbowPattern { type Item = Strip; fn next(&mut self) -> Option { if let Some(nbr_iteration) = self.max_iteration { if nbr_iteration == self.current_iteration { - return None + return None; } } let mut strip = Strip::default(); let step = 255 / 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) } - self.current_iteration = self.current_iteration +1 ; + self.current_iteration = self.current_iteration + 1; Some(strip) } } @@ -78,20 +85,26 @@ impl Iterator for RainbowPattern { fn wheel(index: u8) -> PixelColor { let pos = 255 - index; match pos { - 0..=85 => PixelColor{ + 0..=85 => PixelColor { red: 255 - (pos * 3), - green :0, - blue : pos * 3}, + green: 0, + blue: pos * 3, + }, 86..=170 => { let pos = pos - 85; - PixelColor{ + PixelColor { red: 0, - green :pos*3, - blue : 255 - (pos * 3)} + green: pos * 3, + blue: 255 - (pos * 3), + } } _ => { let pos = pos - 170; - return PixelColor{red : pos*3, green : 255 - (pos*3), blue : 0}; + return PixelColor { + red: pos * 3, + green: 255 - (pos * 3), + blue: 0, + }; } } -} \ No newline at end of file +}