1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use super::brightness::BrightnessOption;
use crate::capability::color::ColorOption;
use crate::communication_protocol::Protocol;
use crate::device::Device;
use crate::device::Write;
use crate::error::{BluetoothError};
use async_trait::async_trait;
use std::time::Duration;
use tokio::time;
pub enum SWAnimateOption<'e> {
Breathing(&'e ColorOption, &'e SWAnimationRepeat, &'e SWAnimationSpeed),
}
pub enum SWAnimationRepeat {
FiniteCount(i32),
InfiniteCount,
}
pub enum SWAnimationSpeed {
Slowest,
Slower,
Slow,
Normal,
Fast,
Faster,
Fastest,
}
fn sw_animation_speed(speed: &SWAnimationSpeed) -> u64 {
match speed {
SWAnimationSpeed::Fastest => 5,
SWAnimationSpeed::Faster => 20,
SWAnimationSpeed::Fast => 50,
SWAnimationSpeed::Normal => 200,
SWAnimationSpeed::Slow => 300,
SWAnimationSpeed::Slower => 400,
SWAnimationSpeed::Slowest => 600,
}
}
#[async_trait]
pub trait SWAnimate {
async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
&self,
protocol: &'e P,
option: &'e SWAnimateOption,
) -> Result<(), BluetoothError>;
async fn _breathing<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
&self,
protocol: &'e P,
color: &'e ColorOption,
interval: u64,
) -> Result<(), BluetoothError>;
async fn breathing<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
&self,
protocol: &'e P,
color: &'e ColorOption,
repeat: &'e SWAnimationRepeat,
speed: &'e SWAnimationSpeed,
) -> Result<(), BluetoothError>;
}
#[async_trait]
impl<D: Device + std::marker::Sync> SWAnimate for D {
async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
&self,
protocol: &'e P,
option: &'e SWAnimateOption,
) -> Result<(), BluetoothError> {
match option {
SWAnimateOption::Breathing(color, repeat, speed) => {
self.breathing(protocol, color, repeat, speed).await?;
}
}
Ok(())
}
async fn _breathing<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
&self,
protocol: &'e P,
color: &'e ColorOption,
interval: u64,
) -> Result<(), BluetoothError> {
for i in 0..=100 {
let e_bytes = protocol.brightness(&BrightnessOption::LevelWithColor(
i as f32 / 100 as f32,
color,
));
self.push(&(e_bytes)[..]).await?;
time::sleep(Duration::from_millis(interval)).await;
}
for i in (0..=100).rev() {
let e_bytes = protocol.brightness(&BrightnessOption::LevelWithColor(
i as f32 / 100 as f32,
color,
));
self.push(&(e_bytes)[..]).await?;
time::sleep(Duration::from_millis(interval)).await;
}
Ok(())
}
async fn breathing<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
&self,
protocol: &'e P,
color: &'e ColorOption,
repeat: &'e SWAnimationRepeat,
speed: &'e SWAnimationSpeed,
) -> Result<(), BluetoothError> {
match repeat {
SWAnimationRepeat::FiniteCount(count) => {
let mut i = 0;
while i < *count {
self._breathing(protocol, &color, sw_animation_speed(speed))
.await?;
i += 1;
}
}
SWAnimationRepeat::InfiniteCount => loop {
self._breathing(protocol, &color, sw_animation_speed(speed))
.await?;
},
}
Ok(())
}
}