Controller

Struct Controller 

Source
pub struct Controller<D: Device> { /* private fields */ }

Implementations§

Source§

impl<D: Device> Controller<D>

Source

pub async fn new() -> Result<Controller<D>, BluetoothError>

Creates a new Device controller

§Examples
use ble_ledly::device::LedDevice;
use ble_ledly::Controller;
use std::error::Error;

 async fn test() -> Result<(), Box<dyn Error>> {
    let mut controller = Controller::<LedDevice>::new().await?;
    Ok(())
}
Examples found in repository?
examples/known_characteristic.rs (line 16)
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    // list all connected devices
38    let connected_lights = controller.list();
39    for light in connected_lights.iter_mut() {
40        println!("-- Manipulating light {} --", light);
41
42        // set the write_char for the current device
43        // you can set different write characteristics for different
44        // devices, as one controller support devices with different communication
45        // protocols
46        // Set it with an Uuid, an u32, or u16
47        light.set_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
48
49        /////////////////////////////////
50        // Control the lights as usual //
51        /////////////////////////////////
52
53        // Control the lights
54        println!("Turning light on...");
55        light.turn_on(&protocol).await?;
56
57        // Set color
58        println!("Setting color...");
59        light.color(&protocol, 255, 0, 0).await?;
60        time::sleep(Duration::from_millis(800)).await;
61        light.color(&protocol, 0, 255, 0).await?;
62        time::sleep(Duration::from_millis(800)).await;
63        light.color(&protocol, 0, 0, 255).await?;
64        time::sleep(Duration::from_millis(800)).await;
65
66        println!("Turning light off...");
67        light.turn_off(&protocol).await?;
68    }
69
70    Ok(())
71}
More examples
Hide additional examples
examples/control_light_idiomatic.rs (line 16)
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}
examples/find_characteristic.rs (line 15)
13async fn main() -> Result<(), Box<dyn Error>> {
14    // Create a new Light controller
15    let mut controller = Controller::<LedDevice>::new().await?;
16
17    // Discover devices (scan)
18    let led_devices = controller.device_discovery().await?;
19
20    // inspect all found devices
21    for device in led_devices.iter() {
22        println!("Found device: {}", device);
23    }
24    // filter devices
25    let lights: Vec<LedDevice> = led_devices
26        .into_iter()
27        .filter(|device| device.name.contains("QHM-"))
28        .collect();
29
30    // Connect
31    controller.connect_with_devices(lights).await?;
32
33    // Choose your communication protocol
34    let protocol = GenericRGB::default();
35
36    // list all connected devices
37    let connected_lights = controller.list();
38    for light in connected_lights.iter_mut() {
39        println!("--- Found characteristics for device {}: ---", light);
40
41        // inspect all characteristic for every device
42        for characteristic in light.characteristics().unwrap().iter() {
43            println!(
44                "\tUuid: {:?}, Type: {:?}",
45                characteristic.uuid, characteristic.properties
46            );
47        }
48
49        println!("--- Filtered characteristics for device {}: ---", light);
50
51        // otherwise inspect all characteristic by supported operation kind
52        let char_kind_filter = OpKind::Write | OpKind::WriteWithoutResponse;
53
54        for characteristic in light
55            .characteristics_by_type(char_kind_filter)
56            .unwrap()
57            .iter()
58        {
59            println!(
60                "\tUuid: {:?}, Type: {:?}",
61                characteristic.uuid, characteristic.properties
62            );
63        }
64
65        // choose the characteristic to use to write to the device
66        let chosen = light.characteristics_by_type(char_kind_filter).unwrap();
67        println!("\nChosen {:?}\n", chosen.get(0));
68
69        // set it as a write_char for the current device
70        // you can set different write characteristics for different
71        // devices, as one controller support devices with different communication
72        // protocols
73        light.set_write_char(&chosen.get(0).unwrap());
74
75        /////////////////////////////////
76        // Control the lights as usual //
77        /////////////////////////////////
78
79        // Control the lights
80        println!("Turning light on...");
81        light.turn_on(&protocol).await?;
82
83        // Set color
84        println!("Setting color...");
85        light.color(&protocol, 255, 0, 0).await?;
86        time::sleep(Duration::from_millis(800)).await;
87        light.color(&protocol, 0, 255, 0).await?;
88        time::sleep(Duration::from_millis(800)).await;
89        light.color(&protocol, 0, 0, 255).await?;
90        time::sleep(Duration::from_millis(800)).await;
91
92        println!("Turning light off...");
93        light.turn_off(&protocol).await?;
94    }
95
96    Ok(())
97}
examples/control_light.rs (line 17)
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}
Source

pub async fn new_with_prefix( prefix: &str, ) -> Result<Controller<D>, BluetoothError>

Creates a new Device controller with Prefix. The prefix is used to automatically filter the devices found during device_discovery(); The filter looks if the prefix id contained in the device name.

§Examples
use ble_ledly::device::LedDevice;
use ble_ledly::Controller;
use std::error::Error;

 async fn test() -> Result<(), Box<dyn Error>> {
    let mut controller = Controller::<LedDevice>::new_with_prefix("QHM-").await?;
    Ok(())
}
Examples found in repository?
examples/minimal.rs (line 13)
10async fn main() -> Result<(), Box<dyn Error>> {
11    // Create a new Light controller with prefix
12    // Auto-filter devices that contain the prefix
13    let mut controller = Controller::<LedDevice>::new_with_prefix("QHM-").await?;
14
15    // Connect
16    controller.connect().await?;
17
18    // Choose your communication protocol
19    let protocol = GenericRGB::default();
20
21    // set default write characteristic for all connected
22    // devices
23    controller.set_all_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
24
25    // Setting first found light color to red
26    let first_light = controller.list().get(0).unwrap();
27    first_light.color(&protocol, 255, 0, 0).await?;
28
29    Ok(())
30}
Source

pub fn set_all_char( &mut self, char_kind: &CharKind, uuid_kind: &UuidKind, ) -> Result<(), BluetoothError>

Sets all the devices default Characteristic (Write or Read) to the provided value. Global shortcut, instead of setting, per-device Characteristic configuration

§Examples
controller.set_all_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
Examples found in repository?
examples/minimal.rs (line 23)
10async fn main() -> Result<(), Box<dyn Error>> {
11    // Create a new Light controller with prefix
12    // Auto-filter devices that contain the prefix
13    let mut controller = Controller::<LedDevice>::new_with_prefix("QHM-").await?;
14
15    // Connect
16    controller.connect().await?;
17
18    // Choose your communication protocol
19    let protocol = GenericRGB::default();
20
21    // set default write characteristic for all connected
22    // devices
23    controller.set_all_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
24
25    // Setting first found light color to red
26    let first_light = controller.list().get(0).unwrap();
27    first_light.color(&protocol, 255, 0, 0).await?;
28
29    Ok(())
30}
More examples
Hide additional examples
examples/control_light_idiomatic.rs (line 40)
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}
examples/control_light.rs (line 41)
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}
Source

pub fn ble_manager(&self) -> &Manager

Source

pub fn list(&mut self) -> &mut Vec<D>

Return a list (Vec) of all the connected devices. The list is empty until devices are connected to the controller.

§Examples
use ble_ledly::device::LedDevice;
use ble_ledly::Controller;
use std::error::Error;

 async fn test() -> Result<(), Box<dyn Error>> {
    let mut controller = Controller::<LedDevice>::new_with_prefix("QHM-").await?;
    let connected_lights = controller.list();
    Ok(())
}
Examples found in repository?
examples/minimal.rs (line 26)
10async fn main() -> Result<(), Box<dyn Error>> {
11    // Create a new Light controller with prefix
12    // Auto-filter devices that contain the prefix
13    let mut controller = Controller::<LedDevice>::new_with_prefix("QHM-").await?;
14
15    // Connect
16    controller.connect().await?;
17
18    // Choose your communication protocol
19    let protocol = GenericRGB::default();
20
21    // set default write characteristic for all connected
22    // devices
23    controller.set_all_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
24
25    // Setting first found light color to red
26    let first_light = controller.list().get(0).unwrap();
27    first_light.color(&protocol, 255, 0, 0).await?;
28
29    Ok(())
30}
More examples
Hide additional examples
examples/known_characteristic.rs (line 38)
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    // list all connected devices
38    let connected_lights = controller.list();
39    for light in connected_lights.iter_mut() {
40        println!("-- Manipulating light {} --", light);
41
42        // set the write_char for the current device
43        // you can set different write characteristics for different
44        // devices, as one controller support devices with different communication
45        // protocols
46        // Set it with an Uuid, an u32, or u16
47        light.set_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
48
49        /////////////////////////////////
50        // Control the lights as usual //
51        /////////////////////////////////
52
53        // Control the lights
54        println!("Turning light on...");
55        light.turn_on(&protocol).await?;
56
57        // Set color
58        println!("Setting color...");
59        light.color(&protocol, 255, 0, 0).await?;
60        time::sleep(Duration::from_millis(800)).await;
61        light.color(&protocol, 0, 255, 0).await?;
62        time::sleep(Duration::from_millis(800)).await;
63        light.color(&protocol, 0, 0, 255).await?;
64        time::sleep(Duration::from_millis(800)).await;
65
66        println!("Turning light off...");
67        light.turn_off(&protocol).await?;
68    }
69
70    Ok(())
71}
examples/control_light_idiomatic.rs (line 43)
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}
examples/find_characteristic.rs (line 37)
13async fn main() -> Result<(), Box<dyn Error>> {
14    // Create a new Light controller
15    let mut controller = Controller::<LedDevice>::new().await?;
16
17    // Discover devices (scan)
18    let led_devices = controller.device_discovery().await?;
19
20    // inspect all found devices
21    for device in led_devices.iter() {
22        println!("Found device: {}", device);
23    }
24    // filter devices
25    let lights: Vec<LedDevice> = led_devices
26        .into_iter()
27        .filter(|device| device.name.contains("QHM-"))
28        .collect();
29
30    // Connect
31    controller.connect_with_devices(lights).await?;
32
33    // Choose your communication protocol
34    let protocol = GenericRGB::default();
35
36    // list all connected devices
37    let connected_lights = controller.list();
38    for light in connected_lights.iter_mut() {
39        println!("--- Found characteristics for device {}: ---", light);
40
41        // inspect all characteristic for every device
42        for characteristic in light.characteristics().unwrap().iter() {
43            println!(
44                "\tUuid: {:?}, Type: {:?}",
45                characteristic.uuid, characteristic.properties
46            );
47        }
48
49        println!("--- Filtered characteristics for device {}: ---", light);
50
51        // otherwise inspect all characteristic by supported operation kind
52        let char_kind_filter = OpKind::Write | OpKind::WriteWithoutResponse;
53
54        for characteristic in light
55            .characteristics_by_type(char_kind_filter)
56            .unwrap()
57            .iter()
58        {
59            println!(
60                "\tUuid: {:?}, Type: {:?}",
61                characteristic.uuid, characteristic.properties
62            );
63        }
64
65        // choose the characteristic to use to write to the device
66        let chosen = light.characteristics_by_type(char_kind_filter).unwrap();
67        println!("\nChosen {:?}\n", chosen.get(0));
68
69        // set it as a write_char for the current device
70        // you can set different write characteristics for different
71        // devices, as one controller support devices with different communication
72        // protocols
73        light.set_write_char(&chosen.get(0).unwrap());
74
75        /////////////////////////////////
76        // Control the lights as usual //
77        /////////////////////////////////
78
79        // Control the lights
80        println!("Turning light on...");
81        light.turn_on(&protocol).await?;
82
83        // Set color
84        println!("Setting color...");
85        light.color(&protocol, 255, 0, 0).await?;
86        time::sleep(Duration::from_millis(800)).await;
87        light.color(&protocol, 0, 255, 0).await?;
88        time::sleep(Duration::from_millis(800)).await;
89        light.color(&protocol, 0, 0, 255).await?;
90        time::sleep(Duration::from_millis(800)).await;
91
92        println!("Turning light off...");
93        light.turn_off(&protocol).await?;
94    }
95
96    Ok(())
97}
examples/control_light.rs (line 44)
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}
Source

pub async fn device_discovery(&self) -> Result<Vec<D>, BluetoothError>

Discover ble devices by running a scan op. on the default adapter and returns the found devices_.

§Examples
use ble_ledly::device::LedDevice;
use ble_ledly::Controller;
use std::error::Error;

 async fn test() -> Result<(), Box<dyn Error>> {
    let mut controller = Controller::<LedDevice>::new_with_prefix("QHM-").await?;
    let led_devices = controller.device_discovery().await?;
    Ok(())
}
Examples found in repository?
examples/known_characteristic.rs (line 19)
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    // list all connected devices
38    let connected_lights = controller.list();
39    for light in connected_lights.iter_mut() {
40        println!("-- Manipulating light {} --", light);
41
42        // set the write_char for the current device
43        // you can set different write characteristics for different
44        // devices, as one controller support devices with different communication
45        // protocols
46        // Set it with an Uuid, an u32, or u16
47        light.set_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
48
49        /////////////////////////////////
50        // Control the lights as usual //
51        /////////////////////////////////
52
53        // Control the lights
54        println!("Turning light on...");
55        light.turn_on(&protocol).await?;
56
57        // Set color
58        println!("Setting color...");
59        light.color(&protocol, 255, 0, 0).await?;
60        time::sleep(Duration::from_millis(800)).await;
61        light.color(&protocol, 0, 255, 0).await?;
62        time::sleep(Duration::from_millis(800)).await;
63        light.color(&protocol, 0, 0, 255).await?;
64        time::sleep(Duration::from_millis(800)).await;
65
66        println!("Turning light off...");
67        light.turn_off(&protocol).await?;
68    }
69
70    Ok(())
71}
More examples
Hide additional examples
examples/control_light_idiomatic.rs (line 19)
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}
examples/find_characteristic.rs (line 18)
13async fn main() -> Result<(), Box<dyn Error>> {
14    // Create a new Light controller
15    let mut controller = Controller::<LedDevice>::new().await?;
16
17    // Discover devices (scan)
18    let led_devices = controller.device_discovery().await?;
19
20    // inspect all found devices
21    for device in led_devices.iter() {
22        println!("Found device: {}", device);
23    }
24    // filter devices
25    let lights: Vec<LedDevice> = led_devices
26        .into_iter()
27        .filter(|device| device.name.contains("QHM-"))
28        .collect();
29
30    // Connect
31    controller.connect_with_devices(lights).await?;
32
33    // Choose your communication protocol
34    let protocol = GenericRGB::default();
35
36    // list all connected devices
37    let connected_lights = controller.list();
38    for light in connected_lights.iter_mut() {
39        println!("--- Found characteristics for device {}: ---", light);
40
41        // inspect all characteristic for every device
42        for characteristic in light.characteristics().unwrap().iter() {
43            println!(
44                "\tUuid: {:?}, Type: {:?}",
45                characteristic.uuid, characteristic.properties
46            );
47        }
48
49        println!("--- Filtered characteristics for device {}: ---", light);
50
51        // otherwise inspect all characteristic by supported operation kind
52        let char_kind_filter = OpKind::Write | OpKind::WriteWithoutResponse;
53
54        for characteristic in light
55            .characteristics_by_type(char_kind_filter)
56            .unwrap()
57            .iter()
58        {
59            println!(
60                "\tUuid: {:?}, Type: {:?}",
61                characteristic.uuid, characteristic.properties
62            );
63        }
64
65        // choose the characteristic to use to write to the device
66        let chosen = light.characteristics_by_type(char_kind_filter).unwrap();
67        println!("\nChosen {:?}\n", chosen.get(0));
68
69        // set it as a write_char for the current device
70        // you can set different write characteristics for different
71        // devices, as one controller support devices with different communication
72        // protocols
73        light.set_write_char(&chosen.get(0).unwrap());
74
75        /////////////////////////////////
76        // Control the lights as usual //
77        /////////////////////////////////
78
79        // Control the lights
80        println!("Turning light on...");
81        light.turn_on(&protocol).await?;
82
83        // Set color
84        println!("Setting color...");
85        light.color(&protocol, 255, 0, 0).await?;
86        time::sleep(Duration::from_millis(800)).await;
87        light.color(&protocol, 0, 255, 0).await?;
88        time::sleep(Duration::from_millis(800)).await;
89        light.color(&protocol, 0, 0, 255).await?;
90        time::sleep(Duration::from_millis(800)).await;
91
92        println!("Turning light off...");
93        light.turn_off(&protocol).await?;
94    }
95
96    Ok(())
97}
examples/control_light.rs (line 20)
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}
Source

pub async fn connect(&mut self) -> Result<(), BluetoothError>

Standalone connect() that can be used in conjunction with Controller::new_with_prefix(prfix); it automatically runs a device_discovery() and connects to all devices that match prefix. If no prefix is provided it attemps to connect to all available devices.

§Examples
use ble_ledly::device::LedDevice;
use ble_ledly::Controller;
use std::error::Error;

 async fn test() -> Result<(), Box<dyn Error>> {
    let mut controller = Controller::<LedDevice>::new_with_prefix("QHM-").await?;
    controller.connect().await?;
    Ok(())
}
Examples found in repository?
examples/minimal.rs (line 16)
10async fn main() -> Result<(), Box<dyn Error>> {
11    // Create a new Light controller with prefix
12    // Auto-filter devices that contain the prefix
13    let mut controller = Controller::<LedDevice>::new_with_prefix("QHM-").await?;
14
15    // Connect
16    controller.connect().await?;
17
18    // Choose your communication protocol
19    let protocol = GenericRGB::default();
20
21    // set default write characteristic for all connected
22    // devices
23    controller.set_all_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
24
25    // Setting first found light color to red
26    let first_light = controller.list().get(0).unwrap();
27    first_light.color(&protocol, 255, 0, 0).await?;
28
29    Ok(())
30}
Source

pub async fn connect_with_devices( &mut self, devices: Vec<D>, ) -> Result<(), BluetoothError>

Connect to the devices passed as function’s argument.

§Examples
let led_devices = controller.device_discovery().await?;
filter devices
let lights: Vec<LedDevice> = led_devices
   .into_iter()
  .filter(|device| device.name.contains("QHM-"))
   .collect();
controller.connect_with_devices(lights).await?;
Examples found in repository?
examples/known_characteristic.rs (line 32)
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    // list all connected devices
38    let connected_lights = controller.list();
39    for light in connected_lights.iter_mut() {
40        println!("-- Manipulating light {} --", light);
41
42        // set the write_char for the current device
43        // you can set different write characteristics for different
44        // devices, as one controller support devices with different communication
45        // protocols
46        // Set it with an Uuid, an u32, or u16
47        light.set_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
48
49        /////////////////////////////////
50        // Control the lights as usual //
51        /////////////////////////////////
52
53        // Control the lights
54        println!("Turning light on...");
55        light.turn_on(&protocol).await?;
56
57        // Set color
58        println!("Setting color...");
59        light.color(&protocol, 255, 0, 0).await?;
60        time::sleep(Duration::from_millis(800)).await;
61        light.color(&protocol, 0, 255, 0).await?;
62        time::sleep(Duration::from_millis(800)).await;
63        light.color(&protocol, 0, 0, 255).await?;
64        time::sleep(Duration::from_millis(800)).await;
65
66        println!("Turning light off...");
67        light.turn_off(&protocol).await?;
68    }
69
70    Ok(())
71}
More examples
Hide additional examples
examples/control_light_idiomatic.rs (line 32)
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}
examples/find_characteristic.rs (line 31)
13async fn main() -> Result<(), Box<dyn Error>> {
14    // Create a new Light controller
15    let mut controller = Controller::<LedDevice>::new().await?;
16
17    // Discover devices (scan)
18    let led_devices = controller.device_discovery().await?;
19
20    // inspect all found devices
21    for device in led_devices.iter() {
22        println!("Found device: {}", device);
23    }
24    // filter devices
25    let lights: Vec<LedDevice> = led_devices
26        .into_iter()
27        .filter(|device| device.name.contains("QHM-"))
28        .collect();
29
30    // Connect
31    controller.connect_with_devices(lights).await?;
32
33    // Choose your communication protocol
34    let protocol = GenericRGB::default();
35
36    // list all connected devices
37    let connected_lights = controller.list();
38    for light in connected_lights.iter_mut() {
39        println!("--- Found characteristics for device {}: ---", light);
40
41        // inspect all characteristic for every device
42        for characteristic in light.characteristics().unwrap().iter() {
43            println!(
44                "\tUuid: {:?}, Type: {:?}",
45                characteristic.uuid, characteristic.properties
46            );
47        }
48
49        println!("--- Filtered characteristics for device {}: ---", light);
50
51        // otherwise inspect all characteristic by supported operation kind
52        let char_kind_filter = OpKind::Write | OpKind::WriteWithoutResponse;
53
54        for characteristic in light
55            .characteristics_by_type(char_kind_filter)
56            .unwrap()
57            .iter()
58        {
59            println!(
60                "\tUuid: {:?}, Type: {:?}",
61                characteristic.uuid, characteristic.properties
62            );
63        }
64
65        // choose the characteristic to use to write to the device
66        let chosen = light.characteristics_by_type(char_kind_filter).unwrap();
67        println!("\nChosen {:?}\n", chosen.get(0));
68
69        // set it as a write_char for the current device
70        // you can set different write characteristics for different
71        // devices, as one controller support devices with different communication
72        // protocols
73        light.set_write_char(&chosen.get(0).unwrap());
74
75        /////////////////////////////////
76        // Control the lights as usual //
77        /////////////////////////////////
78
79        // Control the lights
80        println!("Turning light on...");
81        light.turn_on(&protocol).await?;
82
83        // Set color
84        println!("Setting color...");
85        light.color(&protocol, 255, 0, 0).await?;
86        time::sleep(Duration::from_millis(800)).await;
87        light.color(&protocol, 0, 255, 0).await?;
88        time::sleep(Duration::from_millis(800)).await;
89        light.color(&protocol, 0, 0, 255).await?;
90        time::sleep(Duration::from_millis(800)).await;
91
92        println!("Turning light off...");
93        light.turn_off(&protocol).await?;
94    }
95
96    Ok(())
97}
examples/control_light.rs (line 33)
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}

Auto Trait Implementations§

§

impl<D> Freeze for Controller<D>

§

impl<D> !RefUnwindSafe for Controller<D>

§

impl<D> Send for Controller<D>
where D: Send,

§

impl<D> Sync for Controller<D>
where D: Sync,

§

impl<D> Unpin for Controller<D>
where D: Unpin,

§

impl<D> !UnwindSafe for Controller<D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.