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
use super::CapabilityBase;
use crate::Error;
use dbus_message_parser::Value;

/// Controls a device's brightness.
#[derive(Debug, Clone)]
pub struct Brightness(pub(crate) CapabilityBase);

impl Brightness {
    pub(crate) const INTERFACE: &'static str = "razer.device.lighting.brightness";
    pub(crate) const GET_METHOD: &'static str = "getBrightness";
    const SET_METHOD: &'static str = "setBrightness";

    /// Gets the device's lighting brightness.
    pub async fn get(&self) -> Result<f64, Error> {
        self.0
            .call_get_method(Self::INTERFACE, Self::GET_METHOD)
            .await
            .and_then(|response| match response {
                Value::Double(brightness) => Ok(brightness),
                _ => Err(Error::InvalidResponse),
            })
    }

    /// Sets the device's lighting brightness.
    ///
    /// # Panics
    ///
    /// Panics if `brightness` is not in range `0.0..=100.0`.
    pub async fn set(&self, brightness: f64) -> Result<(), Error> {
        assert!(0.0 <= brightness && brightness <= 100.0);

        self.0
            .call_set_method(
                Self::INTERFACE,
                Self::SET_METHOD,
                vec![Value::Double(brightness)],
            )
            .await
    }

    /// Disables lighting, i.e. sets the brightness to `0.0`.
    pub async fn turn_off(&self) -> Result<(), Error> {
        self.set(0.0).await
    }

    /// Sets maximum brightness, `100.0`.
    pub async fn full(&self) -> Result<(), Error> {
        self.set(100.0).await
    }
}