ble_ledly/capability/
hw_animate.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// animate //
9//---------//
10pub enum HWAnimateOption<'e> {
11    Pulsating(&'e HWStaticColorOption, &'e HWAnimationSpeedSetting),
12}
13
14pub enum HWStaticColorOption {
15    Red,
16    Green,
17    Blue,
18}
19// TODO: more meaningf&ul name
20pub enum HWAnimationSpeedSetting {
21    Speed1,
22    Speed2,
23    Speed3,
24    Speed4,
25    Speed5,
26    Speed6,
27    Speed7,
28    Speed8,
29    Speed9,
30}
31
32#[async_trait]
33pub trait HWAnimate {
34    async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
35        device: &Self,
36        protocol: &'e P,
37        option: &'e HWAnimateOption,
38    ) -> Result<(), BluetoothError>;
39
40    // -------------------------------//
41    // Syntactic sugar /////////////////
42    // more idiomatic syntactic sugar //
43    // -------------------------------//
44    async fn hw_anim_pulsating<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
45        &self,
46        protocol: &'e P,
47        color: &'e HWStaticColorOption,
48        speed: &'e HWAnimationSpeedSetting
49    ) -> Result<(), BluetoothError>;
50}
51
52//-------------------------//
53// Blanket implementations //
54//-------------------------//
55#[async_trait]
56impl<D: Device + std::marker::Sync> HWAnimate for D {
57    // bound type to be transferred across threads
58    async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
59        device: &Self,
60        protocol: &'e P,
61        option: &'e HWAnimateOption,
62    ) -> Result<(), BluetoothError> {
63        device.push(&protocol.hw_animate(option)[..]).await?;
64        Ok(())
65    }
66
67    // -------------------------------//
68    // Syntactic sugar /////////////////
69    // more idiomatic syntactic sugar //
70    // -------------------------------//
71    async fn hw_anim_pulsating<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
72        &self,
73        protocol: &'e P,
74        color: &'e HWStaticColorOption,
75        speed: &'e HWAnimationSpeedSetting
76    ) -> Result<(), BluetoothError> {
77        self.push(&protocol.hw_animate(&HWAnimateOption::Pulsating(color, speed))[..]).await?;
78        Ok(())
79    }
80}