diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index 2f9b232..75ebcbd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ use std::{collections::HashSet, pin::pin, time::Duration}; use bluer::{ - Adapter, AdapterEvent, Device, DiscoveryFilter, DiscoveryTransport, Session, Uuid, - gatt::remote::Characteristic, + Adapter, AdapterEvent, Device, DeviceEvent, DeviceProperty, DiscoveryFilter, + DiscoveryTransport, Session, Uuid, gatt::remote::Characteristic, }; use evdev::{ AbsInfo, AbsoluteAxisCode, AttributeSet, EventType, InputEvent, KeyCode, PropType, @@ -94,6 +94,8 @@ async fn run(adapter: &Adapter) -> bluer::Result<()> { eprintln!("DBG connecting"); remote.connect().await?; + let mut remote_events = pin!(remote.events().await?); + let chars = RemoteCharateristics::get(&remote).await?; let mut touch_stream = pin!(chars.touch.notify().await?); let mut button_stream = pin!(chars.button.notify().await?); @@ -133,8 +135,14 @@ async fn run(adapter: &Adapter) -> bluer::Result<()> { let mut tx = 0f32; let mut ty = 0f32; let mut touch = 0; - loop { + + 'main: loop { select! { + ev = remote_events.next() => { + if let Some(DeviceEvent::PropertyChanged(DeviceProperty::Connected(false))) = ev { + break 'main; + } + } ev = touch_stream.next() => { let ev = ev.unwrap(); let touch_next = if ev[3] > 0 { 1 } else { 0 }; @@ -205,6 +213,8 @@ async fn run(adapter: &Adapter) -> bluer::Result<()> { }, }; } + + Ok(()) } #[tokio::main(flavor = "current_thread")] @@ -212,9 +222,9 @@ async fn main() -> bluer::Result<()> { let session = Session::new().await?; let adapter = session.default_adapter().await?; - if let Err(e) = run(&adapter).await { - eprintln!("ERR {e}"); + loop { + if let Err(e) = run(&adapter).await { + eprintln!("ERR {e}"); + } } - - Ok(()) } |