diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-12-31 14:36:18 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2022-12-31 14:36:18 +0100 |
commit | ba0aeee005d5fdaaab59cd7c099a237f51eddc86 (patch) | |
tree | a4819820cfb6fc9d2d126ab6050ba798bf925359 /src/fl2k.rs | |
download | fl2k_ampliphase-ba0aeee005d5fdaaab59cd7c099a237f51eddc86.tar.gz fl2k_ampliphase-ba0aeee005d5fdaaab59cd7c099a237f51eddc86.tar.bz2 fl2k_ampliphase-ba0aeee005d5fdaaab59cd7c099a237f51eddc86.zip |
Create project
Diffstat (limited to 'src/fl2k.rs')
-rw-r--r-- | src/fl2k.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/fl2k.rs b/src/fl2k.rs new file mode 100644 index 0000000..7bade50 --- /dev/null +++ b/src/fl2k.rs @@ -0,0 +1,52 @@ +use std::ffi::c_int; +use fl2k_ampliphase::{fl2k_dev_t, fl2k_get_device_count, fl2k_open, fl2k_close}; + +enum FL2KError { + InvalidParam, + NoDevice, + NotFound, + Busy, + Timeout, + NoMem, + Unknown(c_int) +} + +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)), + } +} + +pub fn get_device_count() -> u32 { + unsafe { fl2k_get_device_count() } +} + +struct FL2K { + device : *mut fl2k_dev_t, +} + +impl FL2K { + pub fn open(device_index : u32) -> Result<Self, FL2KError> { + unsafe { + let mut fl2k = FL2K { device: std::mem::zeroed() }; + handle_return_value(fl2k_open(&mut fl2k.device, device_index))?; + Ok(fl2k) + } + } + + pub fn close(self) -> Result<(), FL2KError> { + unsafe { + handle_return_value(fl2k_close(self.device))?; + } + Ok(()) + } +} |