Skip to main content

neoradio2/
settings.rs

1use crate::{check, ffi, CommandStatus, Device, LedMode, Result, StatusType};
2use std::os::raw::c_int;
3
4impl Device {
5    /// Toggle LEDs on `device`/`bank`.
6    pub fn toggle_led(
7        &self,
8        device: i32,
9        bank: i32,
10        mode: LedMode,
11        led_enables: i32,
12        ms: i32,
13    ) -> Result<()> {
14        let mut h = self.handle;
15        check(unsafe {
16            ffi::neoradio2_toggle_led(&mut h, device, bank, mode.as_raw(), led_enables, ms)
17        })
18    }
19
20    /// Whether the last [`toggle_led`](Device::toggle_led) completed.
21    pub fn toggle_led_successful(&self, device: i32, bank: i32) -> Result<()> {
22        let mut h = self.handle;
23        check(unsafe { ffi::neoradio2_toggle_led_successful(&mut h, device, bank) })
24    }
25
26    /// Request the device settings (call before [`read_settings`](Device::read_settings)).
27    pub fn request_settings(&self, device: i32, bank: i32) -> Result<()> {
28        let mut h = self.handle;
29        check(unsafe { ffi::neoradio2_request_settings(&mut h, device, bank) })
30    }
31
32    /// Read the raw device settings struct.
33    pub fn read_settings(&self, device: i32, bank: i32) -> Result<ffi::neoRADIO2_settings> {
34        let mut s: ffi::neoRADIO2_settings = unsafe { std::mem::zeroed() };
35        let mut h = self.handle;
36        check(unsafe { ffi::neoradio2_read_settings(&mut h, device, bank, &mut s) })?;
37        Ok(s)
38    }
39
40    /// Write the raw device settings struct.
41    pub fn write_settings(
42        &self,
43        device: i32,
44        bank: i32,
45        settings: &ffi::neoRADIO2_settings,
46    ) -> Result<()> {
47        let mut s = *settings;
48        let mut h = self.handle;
49        check(unsafe { ffi::neoradio2_write_settings(&mut h, device, bank, &mut s) })
50    }
51
52    /// Whether the last [`write_settings`](Device::write_settings) completed.
53    pub fn write_settings_successful(&self, device: i32, bank: i32) -> Result<()> {
54        let mut h = self.handle;
55        check(unsafe { ffi::neoradio2_write_settings_successful(&mut h, device, bank) })
56    }
57
58    /// Restore default settings on `device`/`bank`.
59    pub fn write_default_settings(&self, device: i32, bank: i32) -> Result<()> {
60        let mut h = self.handle;
61        check(unsafe { ffi::neoradio2_write_default_settings(&mut h, device, bank) })
62    }
63
64    /// Request performance statistics.
65    pub fn request_statistics(&self, device: i32, bank: i32) -> Result<()> {
66        let mut h = self.handle;
67        check(unsafe { ffi::neoradio2_request_statistics(&mut h, device, bank) })
68    }
69
70    /// Read raw performance statistics.
71    pub fn read_statistics(&self, device: i32, bank: i32) -> Result<ffi::neoRADIO2_PerfStatistics> {
72        let mut s: ffi::neoRADIO2_PerfStatistics = unsafe { std::mem::zeroed() };
73        let mut h = self.handle;
74        check(unsafe { ffi::neoradio2_read_statistics(&mut h, device, bank, &mut s) })?;
75        Ok(s)
76    }
77
78    /// Query a command status.
79    pub fn status(
80        &self,
81        device: i32,
82        bank: i32,
83        bitfield: bool,
84        ty: StatusType,
85    ) -> Result<CommandStatus> {
86        let mut out: ffi::CommandStatus = 0;
87        let mut h = self.handle;
88        check(unsafe {
89            ffi::neoradio2_get_status(
90                &mut h,
91                device,
92                bank,
93                bitfield as c_int,
94                ty.as_raw() as ffi::StatusType,
95                &mut out,
96            )
97        })?;
98        Ok(CommandStatus::from_raw(out as i32).unwrap_or(CommandStatus::Error))
99    }
100}