ble_ledly/capability/
color.rs

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