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
use crate::{Color, Command, Controller, OpenRgbResult};
/// A single LED of a controller
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Led<'c> {
id: usize,
controller: &'c Controller,
name: &'c str,
color: Color,
}
impl<'c> Led<'c> {
pub(crate) fn new(id: usize, parent: &'c Controller) -> Self {
let name = parent
.led_data()
.get(id)
.map(|ld| ld.name())
.expect("Led::new() called with invalid parameters");
let color = parent
.colors()
.get(id)
.copied()
.expect("Led::new() called with invalid parameters");
Self {
id,
controller: parent,
name,
color,
}
}
/// Returns the ID of this LED. This is equal to the index of this LED in the controller's color array
pub fn id(&self) -> usize {
self.id
}
/// Returns the name of this LED.
///
/// It depends on the controller what kind of name is here,
/// for keyboards this is usually the name of the keys.
pub fn name(&self) -> &str {
self.name
}
/// Returns color of this LED after the last [`crate::Controller::sync_controller_data()`] call.
pub fn color(&self) -> Color {
self.color
}
/// Creates a command with the given `color`
pub fn cmd_with_color<C: Into<Color>>(&self, color: C) -> Command<'_> {
let mut cmd = self.controller.cmd();
cmd.set_led(self.id, color.into())
.expect("Failed to set LED color");
cmd
}
/// Sets this LED to the given `color`.
///
/// It's recommended to use the `Command` API (See `[Controller::cmd()]`) instead
/// when doing many successive writes to many leds.
pub async fn set_led<C: Into<Color>>(&self, color: C) -> OpenRgbResult<()> {
self.controller.set_led(self.id, color).await
}
}