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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use std::marker::PhantomData;
use crate::{Controller, Direction, ModeData, ModeFlag, OpenRgbError, OpenRgbResult};
pub use flagset::FlagSet;
/// Kinds of modes that exist. `Direct` is usually the mode you want to use.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum ControllerModeKind<'c> {
/// Direct mode, leds can be controlled directly. This is the mode you probably want.
Direct,
/// Static mode, l
Static,
/// Some device specific custom mode, like a gradient effect.
Custom(&'c str),
}
impl<'c> ControllerModeKind<'c> {
pub(crate) fn new(mode: &'c ModeData) -> Self {
match mode.name() {
"Direct" | "direct" => Self::Direct,
"Static" | "static" => Self::Static,
_ => Self::Custom(mode.name()),
}
}
}
/// Mode of a controller.
pub struct ControllerMode<'c> {
data: &'c ModeData,
kind: ControllerModeKind<'c>,
is_active: bool,
}
impl<'c> ControllerMode<'c> {
pub(crate) fn new(data: &'c ModeData, is_active: bool) -> Self {
let kind = ControllerModeKind::new(data);
Self {
data,
kind,
is_active,
}
}
pub(crate) fn into_data(self) -> &'c ModeData {
self.data
}
/// The kind of mode this is.
pub fn kind(&self) -> ControllerModeKind<'c> {
self.kind
}
/// Whether this mode is currently active.
pub fn is_active(&self) -> bool {
self.is_active
}
/// Creates a [`ControllerModeBuilder`] for this mode.
///
/// This lets you configure the mode and change it on the controller.
pub fn builder(&self) -> ControllerModeBuilder<'c> {
ControllerModeBuilder::new(self.data)
}
delegate::delegate! {
to self.data {
/// The name of this mode.
pub fn name(&self) -> &str;
/// The flags of this mode.
pub(crate) fn flags(&self) -> FlagSet<ModeFlag>;
/// The speed of this mode, if available.
pub fn speed(&self) -> Option<u32>;
/// The minimum speed of this mode, if available.
pub fn speed_min(&self) -> Option<u32>;
/// The maximum speed of this mode, if available.
pub fn speed_max(&self) -> Option<u32>;
/// The brightness of this mode, if available.
pub fn brightness(&self) -> Option<u32>;
/// The minimum brightness of this mode, if available.
pub fn brightness_min(&self) -> Option<u32>;
/// The maximum brightness of this mode, if available.
pub fn brightness_max(&self) -> Option<u32>;
/// The direction of this mode, if available.
pub fn direction(&self) -> Option<Direction>;
}
}
}
/// Builder for a controller mode.
///
/// This lets you configure the mode, call `execute()` to apply the changes.
pub struct ControllerModeBuilder<'c> {
data: ModeData,
_phantom: PhantomData<&'c ()>,
}
impl ControllerModeBuilder<'_> {
pub(crate) fn new(data: &ModeData) -> Self {
Self {
data: data.clone(),
_phantom: PhantomData,
}
}
/// Applies the changes made in this builder to the controller.
///
/// Remember to call [`Controller::sync_controller_data()`] to sync the changes back.
pub async fn execute(&self, controller: &Controller) -> OpenRgbResult<()> {
controller.update_mode(&self.data).await?;
Ok(())
}
/// Sets the speed of this mode.
///
/// # Errors
///
/// Returns an error if the speed is out of bounds, or if this mode does not support speed.
pub fn set_speed(&mut self, speed: u32) -> OpenRgbResult<&mut Self> {
if !self.data.flags().contains(ModeFlag::HasSpeed) {
return Err(OpenRgbError::CommandError(
"Mode does not support speed".to_owned(),
));
}
let (Some(speed_min), Some(speed_max)) = (self.data.speed_min(), self.data.speed_max())
else {
return Err(OpenRgbError::CommandError(
"Mode does not support speed".to_owned(),
));
};
if speed < speed_min || speed > speed_max {
return Err(OpenRgbError::CommandError(format!(
"Speed must be between {speed_min} and {speed_max}, got: {speed}",
)));
}
self.data.set_speed(speed);
Ok(self)
}
/// Sets the speed of this mode to the minimum speed.
///
/// # Errors
///
/// Returns an error if this mode does not support speed.
pub fn set_min_speed(&mut self) -> OpenRgbResult<&mut Self> {
match self.data.speed_min() {
Some(max_speed) => self.set_speed(max_speed),
None => Err(OpenRgbError::CommandError(
"Mode does not support speed".to_owned(),
)),
}
}
/// Sets the speed of this mode to the maximum speed.
///
/// # Errors
///
/// Returns an error if this mode does not support speed.
pub fn set_max_speed(&mut self) -> OpenRgbResult<&mut Self> {
match self.data.speed_max() {
Some(max_speed) => self.set_speed(max_speed),
None => Err(OpenRgbError::CommandError(
"Mode does not support speed".to_owned(),
)),
}
}
/// Sets the brightness of this mode.
///
/// # Errors
///
/// Returns an error if the brightness is out of bounds, or if this mode does not support brightness.
pub fn set_brightness(&mut self, brightness: u32) -> OpenRgbResult<&mut Self> {
if !self.data.flags().contains(ModeFlag::HasBrightness) {
return Err(OpenRgbError::CommandError(
"Mode does not support brightness".to_owned(),
));
}
let (Some(brightness_min), Some(brightness_max)) =
(self.data.brightness_min(), self.data.brightness_max())
else {
return Err(OpenRgbError::CommandError(
"Mode does not support brightness".to_owned(),
));
};
if brightness < brightness_min || brightness > brightness_max {
return Err(OpenRgbError::CommandError(format!(
"Brightness must be between {brightness_min} and {brightness_max}, got: {brightness}",
)));
}
self.data.set_brightness(brightness);
Ok(self)
}
/// Sets the brightness of this mode to the minimum brightness.
///
/// # Errors
///
/// Returns an error if this mode does not support brightness.
pub fn set_max_brightness(&mut self) -> OpenRgbResult<&mut Self> {
match self.data.brightness_max() {
Some(max_brightness) => self.set_brightness(max_brightness),
None => Err(OpenRgbError::CommandError(
"Mode does not support brightness".to_owned(),
)),
}
}
/// Sets the brightness of this mode to the minimum brightness.
///
/// # Errors
///
/// Returns an error if this mode does not support brightness.
pub fn set_min_brightness(&mut self) -> OpenRgbResult<&mut Self> {
match self.data.brightness_min() {
Some(min_brightness) => self.set_brightness(min_brightness),
None => Err(OpenRgbError::CommandError(
"Mode does not support brightness".to_owned(),
)),
}
}
/// Sets the direction of this mode.
///
/// # Errors
///
/// Returns an error if this mode does not support a direction.
pub fn set_direction(&mut self, dir: Direction) -> OpenRgbResult<&mut Self> {
if !self.data.flags().contains(ModeFlag::HasDirection) {
return Err(OpenRgbError::CommandError(
"Mode does not support direction".to_owned(),
));
}
self.data.set_direction(dir);
Ok(self)
}
}