aboutsummaryrefslogtreecommitdiffstats
path: root/src/fl2k.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/fl2k.rs')
-rw-r--r--src/fl2k.rs45
1 files changed, 26 insertions, 19 deletions
diff --git a/src/fl2k.rs b/src/fl2k.rs
index 7bade50..cfc2778 100644
--- a/src/fl2k.rs
+++ b/src/fl2k.rs
@@ -1,7 +1,8 @@
use std::ffi::c_int;
-use fl2k_ampliphase::{fl2k_dev_t, fl2k_get_device_count, fl2k_open, fl2k_close};
+use fl2k_ampliphase::{fl2k_dev_t, fl2k_get_device_count, fl2k_open, fl2k_close, fl2k_stop_tx};
-enum FL2KError {
+#[derive(Debug)]
+pub enum FL2KError {
InvalidParam,
NoDevice,
NotFound,
@@ -12,25 +13,22 @@ enum FL2KError {
}
fn handle_return_value(val : c_int) -> Result<(), FL2KError> {
- match val {
- #![allow(non_snake_case)]
- fl2k_error_FL2K_SUCCESS => Ok(()),
- fl2k_error_FL2K_TRUE => Ok(()),
- fl2k_error_FL2K_ERROR_INVALID_PARAM => Err(FL2KError::InvalidParam),
- fl2k_error_FL2K_ERROR_NO_DEVICE => Err(FL2KError::NoDevice),
- fl2k_error_FL2K_ERROR_NOT_FOUND => Err(FL2KError::NotFound),
- fl2k_error_FL2K_ERROR_BUSY => Err(FL2KError::Busy),
- fl2k_error_FL2K_ERROR_TIMEOUT => Err(FL2KError::Timeout),
- fl2k_error_FL2K_ERROR_NO_MEM => Err(FL2KError::NoMem),
- v => Err(FL2KError::Unknown(v)),
- }
+ if val == fl2k_ampliphase::fl2k_error_FL2K_SUCCESS { Ok(()) }
+ else if val == fl2k_ampliphase::fl2k_error_FL2K_TRUE { Ok(()) }
+ else if val == fl2k_ampliphase::fl2k_error_FL2K_ERROR_INVALID_PARAM { Err(FL2KError::InvalidParam) }
+ else if val == fl2k_ampliphase::fl2k_error_FL2K_ERROR_NO_DEVICE { Err(FL2KError::NoDevice) }
+ else if val == fl2k_ampliphase::fl2k_error_FL2K_ERROR_NOT_FOUND { Err(FL2KError::NotFound) }
+ else if val == fl2k_ampliphase::fl2k_error_FL2K_ERROR_BUSY { Err(FL2KError::Busy) }
+ else if val == fl2k_ampliphase::fl2k_error_FL2K_ERROR_TIMEOUT { Err(FL2KError::Timeout) }
+ else if val == fl2k_ampliphase::fl2k_error_FL2K_ERROR_NO_MEM { Err(FL2KError::NoMem) }
+ else { Err(FL2KError::Unknown(val)) }
}
pub fn get_device_count() -> u32 {
unsafe { fl2k_get_device_count() }
}
-struct FL2K {
+pub struct FL2K {
device : *mut fl2k_dev_t,
}
@@ -43,10 +41,19 @@ impl FL2K {
}
}
- pub fn close(self) -> Result<(), FL2KError> {
- unsafe {
- handle_return_value(fl2k_close(self.device))?;
- }
+ pub fn stop_tx(&self) -> Result<(), FL2KError> {
+ handle_return_value( unsafe { fl2k_stop_tx(self.device) } )?;
Ok(())
}
}
+
+impl Drop for FL2K {
+ fn drop(&mut self) {
+ match unsafe {
+ handle_return_value(fl2k_close(self.device))
+ } {
+ Ok(_) => (),
+ Err(e) => eprintln!("Failed to close FL2K: {:?}", e),
+ }
+ }
+}