async_touch/
async_touch.rs1use anyhow::Result;
2use rust_patlite_beacon::{AsyncBeacon, Beacon, LedColor, LedPattern};
3use std::time::Duration;
4
5#[tokio::main]
6async fn main() -> Result<()> {
7 println!("Opening beacon device...");
8 let beacon = Beacon::open()?;
9
10 beacon.set_light(LedColor::Blue, LedPattern::Pattern1)?;
12
13 let async_beacon = AsyncBeacon::new(beacon);
14
15 println!("Example 1: Simple wait for touch");
16 println!("Please press the touch sensor...");
17 async_beacon.wait_for_touch().await?;
18 println!("Touch detected!");
19
20 println!("\nExample 2: Wait with callback to show state");
21 println!("Press the touch sensor again...");
22 async_beacon.wait_for_touch_with_callback(|state| {
23 println!("Sensor state: {}", if state { "PRESSED" } else { "RELEASED" });
24 }).await?;
25
26 println!("\nExample 3: Poll sensor for 5 seconds");
27 let start = std::time::Instant::now();
28 async_beacon.poll_touch_sensor(|state| {
29 println!("Polling... state: {}", if state { "PRESSED" } else { "RELEASED" });
30
31 start.elapsed() < Duration::from_secs(5)
33 }, Duration::from_millis(100)).await?;
34
35 println!("\nAll examples completed!");
36 Ok(())
37}