control_light_idiomatic/
control_light_idiomatic.rs1use 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 let mut controller = Controller::<LedDevice>::new().await?;
17
18 let led_devices = controller.device_discovery().await?;
20
21 for device in led_devices.iter() {
23 println!("Found device: {}", device);
24 }
25 let lights: Vec<LedDevice> = led_devices
27 .into_iter()
28 .filter(|device| device.name.contains("QHM-"))
29 .collect();
30
31 controller.connect_with_devices(lights).await?;
33
34 let protocol = GenericRGB::default();
36
37 controller.set_all_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
41
42 let connected_lights = controller.list();
44 for light in connected_lights.iter_mut() {
45 println!("Connected to : {}", light.name);
46
47 println!("Turning light on...");
49 light.turn_on(&protocol).await?;
50
51 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 println!("Turning light off...");
88 light.turn_off(&protocol).await?;
89 }
90
91 Ok(())
92}