control_light/
control_light.rs

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