sync_touch/
sync_touch.rs

1use anyhow::Result;
2use rust_patlite_beacon::{Beacon, LedColor, LedPattern};
3use std::time::Duration;
4
5fn main() -> Result<()> {
6    println!("Opening beacon device...");
7    let beacon = Beacon::open()?;
8    
9    // Set LED to green while waiting
10    beacon.set_light(LedColor::Green, LedPattern::On)?;
11    
12    println!("Example 1: Simple synchronous wait for touch");
13    println!("Please press the touch sensor...");
14    beacon.wait_for_touch_sync()?;
15    println!("Touch detected!");
16    
17    // Set LED to yellow for next test
18    beacon.set_light(LedColor::Yellow, LedPattern::Pattern2)?;
19    
20    println!("\nExample 2: Wait with callback to show state");
21    println!("Press the touch sensor again...");
22    beacon.wait_for_touch_with_callback_sync(|state| {
23        println!("Sensor state: {}", if state { "PRESSED" } else { "RELEASED" });
24    })?;
25    
26    // Set LED to purple for polling
27    beacon.set_light(LedColor::Purple, LedPattern::Pattern3)?;
28    
29    println!("\nExample 3: Poll sensor for 5 seconds");
30    let start = std::time::Instant::now();
31    beacon.poll_touch_sensor_sync(|state| {
32        println!("Polling... state: {}", if state { "PRESSED" } else { "RELEASED" });
33        
34        // Continue polling for 5 seconds
35        start.elapsed() < Duration::from_secs(5)
36    }, Duration::from_millis(100))?;
37    
38    // Turn off LED
39    beacon.reset()?;
40    
41    println!("\nAll examples completed!");
42    Ok(())
43}