diff options
Diffstat (limited to 'Radio')
-rw-r--r-- | Radio/HW/AirSpy/AirSpy.swift | 62 | ||||
-rw-r--r-- | Radio/Utils/AirSpyIQ/main.swift | 58 |
2 files changed, 76 insertions, 44 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) } } diff --git a/Radio/Utils/AirSpyIQ/main.swift b/Radio/Utils/AirSpyIQ/main.swift index 1fa6b04..7fea329 100644 --- a/Radio/Utils/AirSpyIQ/main.swift +++ b/Radio/Utils/AirSpyIQ/main.swift @@ -12,7 +12,7 @@ import libairspy //set the command line arguments struct CommandLineArgs: ParsableCommand { @Argument var file:String = "" - @Option(name:.shortAndLong) var device_idx: Int = 0 + @Option(name:.shortAndLong) var serialno: UInt64 = 0 @Option(name:.shortAndLong) var samplerate: Int = 3000000 @Option(name:.shortAndLong) var gain: Int = 0 @Option(name:.shortAndLong) var frequency: Int = 100000000 @@ -33,11 +33,7 @@ var libersion:airspy_lib_version_t = airspy_lib_version_t() airspy_lib_version(&libersion) print("libairspy \(libersion.major_version).\(libersion.minor_version).\(libersion.revision)") - //detect number of devices -//let AIRSPY_MAX_DEVICES=32 -//var device:airspy_device_t = airspy_device_t() -//var device:UnsafeMutablePointer<OpaquePointer?>? var dev:UnsafeMutablePointer<airspy_device_t>? = .allocate(capacity: 1) var ret:Int32 @@ -47,59 +43,37 @@ if (ret != AIRSPY_SUCCESS.rawValue) { exit(1) } -print("Here") - -ret = airspy_open(&dev) - -let sample_rate_count: UnsafeMutablePointer<UInt32> = .allocate(capacity: 1) -print("Here 2 \(sample_rate_count)") -ret = airspy_get_samplerates(dev, sample_rate_count, 0) -print("ret = \(ret)") -print("Here 3") -print("Number of supported sample rates \(sample_rate_count.pointee)") - -var samplerates:UnsafeMutableBufferPointer<UInt32> = .allocate(capacity: Int(sample_rate_count.pointee)) -let rawPointer = UnsafeMutablePointer<UInt32>?(samplerates.baseAddress!) -ret = airspy_get_samplerates(dev, rawPointer, sample_rate_count.pointee) -print("ret=\(ret)") -for i in 0..<sample_rate_count.pointee { - print("sample rate \(i): \(samplerates[Int(i)])") +var device:AirSpy? +do { + device = try AirSpy() +} catch { + print("Cant attach to device") } -print("Here 4") -ret = airspy_set_samplerate(dev, 3000000) //3M -if (ret != AIRSPY_SUCCESS.rawValue) { - print("Couldnt set samplerate") -} +let sample_rates = device?.getSampleRates() + +let _ = device?.setSampleRate(sample_rates![0]) print("Here 5") func rf_callback(_ transffer: UnsafeMutablePointer<airspy_transfer_t>?) -> Int32 { - print("Got some samples") + let t = transffer!.pointee + print("Got \(t.sample_count) samples") return 0 } -print("Here 6") -ret = airspy_start_rx(dev, rf_callback, nil) -if (ret != AIRSPY_SUCCESS.rawValue) { - print("Couldn't start rf thread") -} +let _ = device?.startRx(rf_callback) -print("Here 7") -ret = airspy_set_freq(dev, 100000000) //100M -if (ret != AIRSPY_SUCCESS.rawValue) { - print("Couldn't set frequency") -} +let _ = device?.setFrequency(UInt32(args.frequency)) -print("Here 8") sleep(1) var count = 10 -while((airspy_is_streaming(dev) != AIRSPY_TRUE.rawValue) +while ((device?.isStreaming() != AIRSPY_TRUE.rawValue) && (count>0)) { print("Streaming") sleep(1) count -= 1 } -print("Here 9") -airspy_close(dev) +let _ = device?.close() + airspy_exit() |