ble_ledly/capability/
brightness.rs

1use crate::capability::color::ColorOption;
2use crate::communication_protocol::Protocol;
3use crate::device::Device;
4use crate::device::Write;
5use crate::error::{BluetoothError};
6use async_trait::async_trait;
7
8//------------//
9// Brightness //
10//------------//
11pub enum BrightnessOption<'e> {
12    Level(f32),
13    LevelWithColor(f32, &'e ColorOption),
14}
15#[async_trait]
16pub trait Brightness {
17    async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
18        device: &Self,
19        protocol: &'e P,
20        option: &'e BrightnessOption,
21    ) -> Result<(), BluetoothError>;
22
23    // -------------------------------//
24    // Syntactic sugar /////////////////
25    // more idiomatic syntactic sugar //
26    // -------------------------------//
27    async fn brightness<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
28        &self,
29        protocol: &'e P,
30        r: u8,
31        g: u8,
32        b: u8,
33        level: f32,
34    ) -> Result<(), BluetoothError>;
35}
36
37//-------------------------//
38// Blanket implementations //
39//-------------------------//
40#[async_trait]
41impl<D: Device + std::marker::Sync> Brightness for D {
42    // bound type to be transferred across threads
43    async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
44        device: &Self,
45        protocol: &'e P,
46        option: &'e BrightnessOption,
47    ) -> Result<(), BluetoothError> {
48        device.push(&protocol.brightness(option)[..]).await?;
49        Ok(())
50    }
51
52    // -------------------------------//
53    // Syntactic sugar /////////////////
54    // more idiomatic syntactic sugar //
55    // -------------------------------//
56    async fn brightness<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
57        &self,
58        protocol: &'e P,
59        r: u8,
60        g: u8,
61        b: u8,
62        level: f32,
63    ) -> Result<(), BluetoothError> {
64        self
65            .push(
66                &protocol.brightness(&BrightnessOption::LevelWithColor(
67                    level,
68                    &ColorOption::RGB(r, g, b),
69                ))[..],
70            )
71            .await?;
72        Ok(())
73    }
74}