control_light_idiomatic/
control_light_idiomatic.rs

1use ble_ledly::capability::color::*;
2use ble_ledly::capability::light::*;
3use ble_ledly::capability::sw_animate::*;
4use ble_ledly::communication_protocol::GenericRGB;
5use ble_ledly::Controller;
6use ble_ledly::device::LedDevice;
7use ble_ledly::device::{CharKind, UuidKind};
8
9use std::error::Error;
10use std::time::Duration;
11use tokio::time;
12
13#[tokio::main]
14async fn main() -> Result<(), Box<dyn Error>> {
15    // Create a new Light controller
16    let mut controller = Controller::<LedDevice>::new().await?;
17
18    // Discover devices (scan)
19    let led_devices = controller.device_discovery().await?;
20
21    // inspect all found devices
22    for device in led_devices.iter() {
23        println!("Found device: {}", device);
24    }
25    // filter devices
26    let lights: Vec<LedDevice> = led_devices
27        .into_iter()
28        .filter(|device| device.name.contains("QHM-"))
29        .collect();
30
31    // Connect
32    controller.connect_with_devices(lights).await?;
33
34    // Choose your communication protocol
35    let protocol = GenericRGB::default();
36
37    // set the default write Characteristic
38    // for all devices. Optionally you can also
39    // set it per-device. Look the examples folder for more
40    controller.set_all_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
41
42    // list all connected devices
43    let connected_lights = controller.list();
44    for light in connected_lights.iter_mut() {
45        println!("Connected to : {}", light.name);
46
47        // Control the lights
48        println!("Turning light on...");
49        light.turn_on(&protocol).await?;
50
51        // Set color
52        println!("Setting color...");
53        light.color(&protocol, 255, 0, 0).await?;
54        time::sleep(Duration::from_millis(800)).await;
55        light.color(&protocol, 0, 255, 0).await?;
56        time::sleep(Duration::from_millis(800)).await;
57        light.color(&protocol, 0, 0, 255).await?;
58        time::sleep(Duration::from_millis(800)).await;
59
60        println!("SW Animation - Breathing effect...");
61        light
62            .breathing(
63                &GenericRGB {},
64                &ColorOption::RGB(255, 0, 0),
65                &SWAnimationRepeat::FiniteCount(2),
66                &SWAnimationSpeed::Fastest,
67            )
68            .await?;
69        light
70            .breathing(
71                &GenericRGB {},
72                &ColorOption::RGB(0, 255, 0),
73                &SWAnimationRepeat::FiniteCount(2),
74                &SWAnimationSpeed::Fastest,
75            )
76            .await?;
77        light
78            .breathing(
79                &GenericRGB {},
80                &ColorOption::RGB(0, 0, 255),
81                &SWAnimationRepeat::FiniteCount(2),
82                &SWAnimationSpeed::Fastest,
83            )
84            .await?;
85
86        // Control the lights
87        println!("Turning light off...");
88        light.turn_off(&protocol).await?;
89    }
90
91    Ok(())
92}