From abbe14bebb2935b017b980e6fc2bfc5e94052049 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Wed, 4 Dec 2024 11:07:17 +0000 Subject: AirSpyHFIQ: read samples from airspyhf and dump those to fc32 file --- Radio/HW/AirSpyHF/AirSpyHF.swift | 112 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) (limited to 'Radio/HW/AirSpyHF/AirSpyHF.swift') 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? = .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 = .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.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.allocate(capacity: Int(nsrate.pointee)) + let rawPointer = UnsafeMutablePointer?(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.. 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) } } -- cgit v1.2.3