1use crate::ffi;
2use std::time::Duration;
3
4macro_rules! int_enum {
5 ($(#[$m:meta])* $name:ident { $($(#[$vm:meta])* $variant:ident = $val:expr),* $(,)? }) => {
6 $(#[$m])*
7 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
8 pub enum $name { $($(#[$vm])* $variant = $val),* }
9 impl $name {
10 pub fn from_raw(v: i32) -> Option<Self> {
12 match v { $($val => Some($name::$variant),)* _ => None }
13 }
14 pub fn as_raw(self) -> i32 { match self { $($name::$variant => $val),* } }
16 }
17 };
18}
19
20int_enum!(
21 DeviceType {
23 Tc = 0,
25 Dio = 1,
27 PwrRly = 2,
29 Ain = 3,
31 Aout = 4,
33 CanHub = 5,
35 Badge = 6,
37 Host = 0xFF
39 }
40);
41int_enum!(
42 CalType {
44 Enabled = 0,
46 NoCal = 1,
48 NoCalEnhanced = 2
50 }
51);
52int_enum!(
53 LedMode {
55 Off = 0,
57 On = 1,
59 BlinkOnce = 2,
61 BlinkDurationMs = 3
63 }
64);
65int_enum!(
66 StatusType {
68 Chain = 0,
70 AppStart = 1,
72 Pcbsn = 2,
74 SensorRead = 3,
76 SensorWrite = 4,
78 SettingsRead = 5,
80 SettingsWrite = 6,
82 Calibration = 7,
84 CalibrationPoints = 8,
86 CalibrationStored = 9,
88 CalibrationInfo = 10,
90 LedToggle = 11,
92 }
93);
94int_enum!(
95 CommandStatus {
97 InProgress = 0,
99 Finished = 1,
101 Error = 2
103 }
104);
105
106pub fn set_blocking(blocking: bool, timeout: Duration) {
109 let ms = timeout.as_millis().min(i64::MAX as u128) as std::os::raw::c_longlong;
110 unsafe { ffi::neoradio2_set_blocking(if blocking { 1 } else { 0 }, ms) };
111}
112
113pub fn is_blocking() -> bool {
115 unsafe { ffi::neoradio2_is_blocking() != 0 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121 #[test]
122 fn device_type_round_trip() {
123 for (v, t) in [
124 (0, DeviceType::Tc),
125 (3, DeviceType::Ain),
126 (6, DeviceType::Badge),
127 ] {
128 assert_eq!(DeviceType::from_raw(v), Some(t));
129 assert_eq!(t.as_raw(), v);
130 }
131 assert_eq!(DeviceType::from_raw(42), None);
132 assert_eq!(DeviceType::from_raw(0xFF), Some(DeviceType::Host));
135 assert_eq!(DeviceType::Host.as_raw(), 0xFF);
136 assert_eq!(DeviceType::Host as i32, 0xFF);
137 }
138 #[test]
139 fn blocking_round_trips() {
140 set_blocking(true, std::time::Duration::from_millis(500));
141 assert!(is_blocking());
142 set_blocking(false, std::time::Duration::from_millis(500));
143 assert!(!is_blocking());
144 }
145 #[test]
146 fn caltype_values() {
147 assert_eq!(CalType::Enabled.as_raw(), 0);
148 assert_eq!(CalType::NoCal.as_raw(), 1);
149 }
150}