use ble_ledly::capability::color::*;
use ble_ledly::capability::hw_animate::*;
use ble_ledly::capability::light::*;
use ble_ledly::capability::sw_animate::*;
use ble_ledly::communication_protocol::GenericRGB;
use ble_ledly::device::LedDevice;
use ble_ledly::device::{CharKind, UuidKind};
use ble_ledly::Controller;
use std::error::Error;
use std::time::Duration;
use tokio::time;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut controller = Controller::<LedDevice>::new().await?;
let led_devices = controller.device_discovery().await?;
for device in led_devices.iter() {
println!("Found device: {}", device);
}
let lights: Vec<LedDevice> = led_devices
.into_iter()
.filter(|device| device.name.contains("QHM-"))
.collect();
controller.connect_with_devices(lights).await?;
let protocol = GenericRGB::default();
controller.set_all_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
let connected_lights = controller.list();
for light in connected_lights.iter_mut() {
println!("Connected to : {}", light.name);
println!("Turning light on...");
Light::set(light, &protocol, &LightOption::On).await?;
println!("Setting color...");
Color::set(light, &protocol, &ColorOption::RGB(255, 0, 0)).await?;
time::sleep(Duration::from_millis(800)).await;
Color::set(light, &protocol, &ColorOption::RGB(0, 255, 0)).await?;
time::sleep(Duration::from_millis(800)).await;
Color::set(light, &protocol, &ColorOption::RGB(0, 0, 255)).await?;
time::sleep(Duration::from_millis(800)).await;
println!("HW Animation - Pulsating effect...");
HWAnimate::set(
light,
&protocol,
&HWAnimateOption::Pulsating(
&HWStaticColorOption::Red,
&HWAnimationSpeedSetting::Speed9,
),
)
.await?;
time::sleep(Duration::from_millis(2000)).await;
HWAnimate::set(
light,
&protocol,
&HWAnimateOption::Pulsating(
&HWStaticColorOption::Green,
&HWAnimationSpeedSetting::Speed9,
),
)
.await?;
time::sleep(Duration::from_millis(2000)).await;
HWAnimate::set(
light,
&protocol,
&HWAnimateOption::Pulsating(
&HWStaticColorOption::Blue,
&HWAnimationSpeedSetting::Speed9,
),
)
.await?;
println!("SW Animation - Breathing effect...");
light
.breathing(
&protocol,
&ColorOption::RGB(255, 0, 0),
&SWAnimationRepeat::FiniteCount(2),
&SWAnimationSpeed::Fastest,
)
.await?;
light
.breathing(
&GenericRGB {},
&ColorOption::RGB(0, 255, 0),
&SWAnimationRepeat::FiniteCount(2),
&SWAnimationSpeed::Fastest,
)
.await?;
light
.breathing(
&GenericRGB {},
&ColorOption::RGB(0, 0, 255),
&SWAnimationRepeat::FiniteCount(2),
&SWAnimationSpeed::Fastest,
)
.await?;
println!("Turning light off...");
light.turn_off(&protocol).await?;
}
Ok(())
}