// // main.swift // RtlSdrIQ // // Created by Jacky Jack on 27/11/2024. // import Foundation import libr820 import ArgumentParser //set the command line arguments struct CommandLineArgs: ParsableCommand { @Argument var file:String = "" @Option(name:.shortAndLong) var device_idx: Int = 0 @Option(name:.shortAndLong) var samplerate: Int = 2048000 @Option(name:.shortAndLong) var gain: Int = 0 @Option(name: .shortAndLong) var frequency: Int = 100000000 @Option(name:.shortAndLong) var nsamples: Int = 1024 @Flag(name: .shortAndLong) var verbose: Bool = false @Flag(name: .shortAndLong) var async: Bool = false } let args = CommandLineArgs.parseOrExit() //check that there is any devices let count = getDeviceCount() if count < 1 { print("There is not R820 tunner found on this machine") exit(0) } //check that argument index is withing range if (args.device_idx < 0) || (args.device_idx > count-1) { print("Index is out of range, devices found \(count)") exit(0) } //initialise structure without a connected driver let device = R820Tuner() device.open(index: UInt32(args.device_idx)) let _ = device.setSampleRate(samplerate: UInt32(args.samplerate)) let _ = device.setCenterFreq(freq: UInt32(args.frequency)) if args.gain == 0 { let _ = device.setAgcMode(on: 1) } else { let _ = device.setTunerGain(gain: Int32(args.gain)) } let _ = device.resetBuffer() let buf_ptr = UnsafeMutableRawPointer.allocate(byteCount: args.nsamples, alignment: 1) var nbytes:Int32 = 0 let r = device.readSync(buf: buf_ptr, len: Int32(args.nsamples), n_read: &nbytes) print("Reading samples return code (\(r)) read \(nbytes) bytes\n") for i in 0..