diff options
Diffstat (limited to 'Radio/HW/AirSpy/AirSpy.swift')
-rw-r--r-- | Radio/HW/AirSpy/AirSpy.swift | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/Radio/HW/AirSpy/AirSpy.swift b/Radio/HW/AirSpy/AirSpy.swift index 5b1b109..d7916b2 100644 --- a/Radio/HW/AirSpy/AirSpy.swift +++ b/Radio/HW/AirSpy/AirSpy.swift @@ -6,9 +6,67 @@ // import libairspy +enum AirspyHFError: Error { + case InvalidDevice +} + /// Wrapper for libairspy library class AirSpy { - init() { - airspy_init() + + var dev:UnsafeMutablePointer<airspy_device_t>? = .allocate(capacity: 1) + var sample_rates:[UInt32] = [] + + init(serialno: UInt64) throws { + let ret = airspy_open_sn(&dev, serialno) + if (ret != AIRSPY_SUCCESS.rawValue) { + throw AirspyHFError.InvalidDevice + } + } + + /// will open first device it can find + init() throws { + let ret = airspy_open(&dev) + if ret != AIRSPY_SUCCESS.rawValue { + print("Couldnt open airspy device") + throw AirspyHFError.InvalidDevice + } + } + + func getSampleRates() -> [UInt32] { + if sample_rates.count == 0 { + + let nscount: UnsafeMutablePointer<UInt32> = .allocate(capacity: 1) + ret = airspy_get_samplerates(dev, nscount, 0) + + let samplerates_buf:UnsafeMutableBufferPointer<UInt32> = .allocate(capacity: Int(nscount.pointee)) + let rawPointer = UnsafeMutablePointer<UInt32>?(samplerates_buf.baseAddress!) + ret = airspy_get_samplerates(dev, rawPointer, nscount.pointee) + + sample_rates = [] + for i in 0..<Int(nscount.pointee) { + sample_rates.append(samplerates_buf[i]) + } + } + return sample_rates + } + + func setSampleRate(_ samplerate: UInt32 ) -> Int32 { + return airspy_set_samplerate(dev, samplerate) + } + + func startRx(_ callback: airspy_sample_block_cb_fn) -> Int32 { + return airspy_start_rx(dev, callback, nil) + } + + func setFrequency(_ freq: UInt32) -> Int32 { + return airspy_set_freq(dev, freq) + } + + func isStreaming() -> Int32 { + return airspy_is_streaming(dev) + } + + func close() -> Int32 { + return airspy_close(dev) } } |