Skip to main content

hidpp/feature/
report_rate.rs

1//! Implements the legacy `ReportRate` feature (ID `0x8060`).
2
3use openlogi_hidpp_derive::Feature;
4
5use crate::{feature::FeatureEndpoint, protocol::v20::Hidpp20Error};
6
7bitflags::bitflags! {
8    /// Report-rate values supported by a `0x8060` device, encoded as milliseconds.
9    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
10    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
11    pub struct ReportRateList: u8 {
12        /// 1 ms report interval.
13        const MS_1 = 1 << 0;
14        /// 2 ms report interval.
15        const MS_2 = 1 << 1;
16        /// 3 ms report interval.
17        const MS_3 = 1 << 2;
18        /// 4 ms report interval.
19        const MS_4 = 1 << 3;
20        /// 5 ms report interval.
21        const MS_5 = 1 << 4;
22        /// 6 ms report interval.
23        const MS_6 = 1 << 5;
24        /// 7 ms report interval.
25        const MS_7 = 1 << 6;
26        /// 8 ms report interval.
27        const MS_8 = 1 << 7;
28    }
29}
30
31/// Implements the `ReportRate` / `0x8060` feature.
32#[derive(Clone, Feature)]
33#[creatable(id = 0x8060, version = 0)]
34pub struct ReportRateFeature {
35    /// The endpoint this feature talks to.
36    endpoint: FeatureEndpoint,
37}
38
39impl ReportRateFeature {
40    /// Retrieves the supported report intervals in milliseconds.
41    pub async fn get_report_rate_list(&self) -> Result<ReportRateList, Hidpp20Error> {
42        let payload = self.endpoint.call(0, [0; 3]).await?.extend_payload();
43        Ok(ReportRateList::from_bits_retain(payload[0]))
44    }
45
46    /// Retrieves the active report interval in milliseconds.
47    pub async fn get_report_rate(&self) -> Result<u8, Hidpp20Error> {
48        Ok(self.endpoint.call(1, [0; 3]).await?.extend_payload()[0])
49    }
50
51    /// Sets the active report interval in milliseconds.
52    ///
53    /// Devices reject unsupported intervals with `InvalidArgument`.
54    pub async fn set_report_rate(&self, report_rate_ms: u8) -> Result<(), Hidpp20Error> {
55        self.endpoint.call(2, [report_rate_ms, 0, 0]).await?;
56        Ok(())
57    }
58}