Skip to main content

neoradio2/
calibration.rs

1use crate::{check, ffi, Device, Result};
2use std::os::raw::c_int;
3
4type CalHeader = ffi::neoRADIO2frame_calHeader;
5
6impl Device {
7    /// Request calibration data (fills nothing; pair with `read_calibration_array`).
8    pub fn request_calibration(
9        &self,
10        device: i32,
11        bank: i32,
12        header: &mut CalHeader,
13    ) -> Result<()> {
14        let mut h = self.handle;
15        check(unsafe { ffi::neoradio2_request_calibration(&mut h, device, bank, header) })
16    }
17
18    /// Read calibration values into a `Vec<f32>`.
19    pub fn read_calibration_array(
20        &self,
21        device: i32,
22        bank: i32,
23        header: &mut CalHeader,
24    ) -> Result<Vec<f32>> {
25        let mut arr = [0f32; 64];
26        let mut len: c_int = arr.len() as c_int;
27        let mut h = self.handle;
28        check(unsafe {
29            ffi::neoradio2_read_calibration_array(
30                &mut h,
31                device,
32                bank,
33                header,
34                arr.as_mut_ptr(),
35                &mut len,
36            )
37        })?;
38        let n = (len.max(0) as usize).min(arr.len());
39        Ok(arr[..n].to_vec())
40    }
41
42    /// Request calibration points.
43    pub fn request_calibration_points(
44        &self,
45        device: i32,
46        bank: i32,
47        header: &mut CalHeader,
48    ) -> Result<()> {
49        let mut h = self.handle;
50        check(unsafe { ffi::neoradio2_request_calibration_points(&mut h, device, bank, header) })
51    }
52
53    /// Read calibration points into a `Vec<f32>`.
54    pub fn read_calibration_points_array(
55        &self,
56        device: i32,
57        bank: i32,
58        header: &mut CalHeader,
59    ) -> Result<Vec<f32>> {
60        let mut arr = [0f32; 64];
61        let mut len: c_int = arr.len() as c_int;
62        let mut h = self.handle;
63        check(unsafe {
64            ffi::neoradio2_read_calibration_points_array(
65                &mut h,
66                device,
67                bank,
68                header,
69                arr.as_mut_ptr(),
70                &mut len,
71            )
72        })?;
73        let n = (len.max(0) as usize).min(arr.len());
74        Ok(arr[..n].to_vec())
75    }
76
77    /// Write calibration values.
78    pub fn write_calibration(
79        &self,
80        device: i32,
81        bank: i32,
82        header: &mut CalHeader,
83        values: &[f32],
84    ) -> Result<()> {
85        let mut h = self.handle;
86        check(unsafe {
87            ffi::neoradio2_write_calibration(
88                &mut h,
89                device,
90                bank,
91                header,
92                values.as_ptr() as *mut f32,
93                values.len() as c_int,
94            )
95        })
96    }
97
98    /// Whether the last `write_calibration` completed.
99    pub fn write_calibration_successful(&self, device: i32, bank: i32) -> Result<()> {
100        let mut h = self.handle;
101        check(unsafe { ffi::neoradio2_write_calibration_successful(&mut h, device, bank) })
102    }
103
104    /// Write calibration points.
105    pub fn write_calibration_points(
106        &self,
107        device: i32,
108        bank: i32,
109        header: &mut CalHeader,
110        values: &[f32],
111    ) -> Result<()> {
112        let mut h = self.handle;
113        check(unsafe {
114            ffi::neoradio2_write_calibration_points(
115                &mut h,
116                device,
117                bank,
118                header,
119                values.as_ptr() as *mut f32,
120                values.len() as c_int,
121            )
122        })
123    }
124
125    /// Whether the last `write_calibration_points` completed.
126    pub fn write_calibration_points_successful(&self, device: i32, bank: i32) -> Result<()> {
127        let mut h = self.handle;
128        check(unsafe { ffi::neoradio2_write_calibration_points_successful(&mut h, device, bank) })
129    }
130
131    /// Store calibration to flash.
132    pub fn store_calibration(&self, device: i32, bank: i32) -> Result<()> {
133        let mut h = self.handle;
134        check(unsafe { ffi::neoradio2_store_calibration(&mut h, device, bank) })
135    }
136
137    /// Whether calibration is stored.
138    pub fn is_calibration_stored(&self, device: i32, bank: i32) -> Result<bool> {
139        let mut out: c_int = 0;
140        let mut h = self.handle;
141        check(unsafe { ffi::neoradio2_is_calibration_stored(&mut h, device, bank, &mut out) })?;
142        Ok(out != 0)
143    }
144
145    /// Whether calibration is valid.
146    pub fn calibration_is_valid(&self, device: i32, bank: i32) -> Result<bool> {
147        let mut out: c_int = 0;
148        let mut h = self.handle;
149        check(unsafe { ffi::neoradio2_get_calibration_is_valid(&mut h, device, bank, &mut out) })?;
150        Ok(out != 0)
151    }
152
153    /// Clear stored calibration.
154    pub fn clear_calibration(&self, device: i32, bank: i32) -> Result<()> {
155        let mut h = self.handle;
156        check(unsafe { ffi::neoradio2_clear_calibration(&mut h, device, bank) })
157    }
158
159    /// Request calibration info.
160    pub fn request_calibration_info(&self, device: i32, bank: i32) -> Result<()> {
161        let mut h = self.handle;
162        check(unsafe { ffi::neoradio2_request_calibration_info(&mut h, device, bank) })
163    }
164
165    /// Read calibration info header.
166    pub fn read_calibration_info(&self, device: i32, bank: i32) -> Result<CalHeader> {
167        let mut header: CalHeader = unsafe { std::mem::zeroed() };
168        let mut h = self.handle;
169        check(unsafe { ffi::neoradio2_read_calibration_info(&mut h, device, bank, &mut header) })?;
170        Ok(header)
171    }
172}