summaryrefslogtreecommitdiff
path: root/Radio/HW/AirSpyHF/AirSpyHF.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Radio/HW/AirSpyHF/AirSpyHF.swift')
-rw-r--r--Radio/HW/AirSpyHF/AirSpyHF.swift112
1 files changed, 110 insertions, 2 deletions
diff --git a/Radio/HW/AirSpyHF/AirSpyHF.swift b/Radio/HW/AirSpyHF/AirSpyHF.swift
index 3affcff..ea835af 100644
--- a/Radio/HW/AirSpyHF/AirSpyHF.swift
+++ b/Radio/HW/AirSpyHF/AirSpyHF.swift
@@ -7,9 +7,117 @@
import libairspyhf
+enum AirspyHFError: Error {
+ case InvalidDevice
+}
+
/// Wrapper for libairspyhf library
class AirSpyHF {
- init() {
- //airspyhf_
+
+ var dev:UnsafeMutablePointer<airspyhf_device_t>? = .allocate(capacity: 1)
+
+ /*
+ init() {
+
+ }*/
+
+ /// Initialise by index
+ init(idx: Int32) throws {
+ let ndev = airspyhf_list_devices(nil, 0)
+ if ((idx < 0) || (idx >= ndev)) {
+ throw AirspyHFError.InvalidDevice
+ }
+ let serialPtr: UnsafeMutablePointer<UInt64> = .allocate(capacity: 1)
+ airspyhf_list_devices(serialPtr, idx+1)
+ if serialPtr.pointee == 0 {
+ print("Cant get serial number of device index \(idx)")
+ throw AirspyHFError.InvalidDevice
+ }
+ try openSN(serial: serialPtr.pointee)
+
+ }
+
+ func openSN(serial: UInt64) throws {
+ let ret = airspyhf_open_sn(&dev, serial)
+ print("ret=\(ret)")
+ if (ret != AIRSPYHF_SUCCESS.rawValue) {
+ print("Cant open device with serial numner \(serial)")
+ throw AirspyHFError.InvalidDevice
+ }
+ }
+
+ /*
+ /// Initialise by serial number
+ init(serial: UInt64) {
+
+ }*/
+
+ func getSampleRates() -> [UInt32] {
+ var sample_rates:[UInt32] = []
+ var ret: Int32
+
+ let nsrate = UnsafeMutablePointer<UInt32>.allocate(capacity: 1)
+ ret = airspyhf_get_samplerates(dev, nsrate, 0)
+ if ret == AIRSPYHF_ERROR.rawValue {
+ print("Error uptaining the sample number")
+ return []
+ }
+
+ let sampleBuffer = UnsafeMutableBufferPointer<UInt32>.allocate(capacity: Int(nsrate.pointee))
+ let rawPointer = UnsafeMutablePointer<UInt32>?(sampleBuffer.baseAddress!)
+ ret = airspyhf_get_samplerates(dev, rawPointer, nsrate.pointee)
+ if (ret == AIRSPYHF_ERROR.rawValue) {
+ print("Error geting sample rates. Not processed")
+ }
+ print("Found \(nsrate.pointee) samplerates")
+ for idx in 0..<nsrate.pointee {
+ sample_rates.append(sampleBuffer[Int(idx)])
+ }
+
+ return sample_rates
+ }
+
+ func setSampleRate(_ samplerate:UInt32) -> Int32 {
+ let ret = airspyhf_set_samplerate(dev, samplerate)
+ return ret
+ }
+
+ func setHfAgc(_ flag: UInt8) -> Int32{
+ let ret = airspyhf_set_hf_agc(dev, flag)
+ return ret
+ }
+
+ func setHfAgcThreshold(_ flag: UInt8) -> Int32 {
+ let ret = airspyhf_set_hf_agc_threshold(dev, flag)
+ return ret
+ }
+
+ func setHfLNA(_ flag: UInt8) -> Int32 {
+ let ret = airspyhf_set_hf_lna(dev, flag)
+ return ret
+ }
+
+ func start(_ callback: airspyhf_sample_block_cb_fn) -> Int32 {
+ let ret = airspyhf_start(dev, callback, nil)
+ return ret
+ }
+
+ func stop() -> Int32 {
+ let ret = airspyhf_stop(dev)
+ return ret
+ }
+
+ func setFreq(_ frequency: UInt32) -> Int32 {
+ let ret = airspyhf_set_freq(dev, frequency)
+ return ret
+ }
+
+ func isStreaming() -> Int32 {
+ let ret = airspyhf_is_streaming(dev)
+ return ret
+ }
+
+ func close() {
+ airspyhf_close(dev)
}
}