From 77d1cfc27fb72e442b8d8e5cbe2be143723724e5 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Thu, 28 Nov 2024 11:37:28 +0000 Subject: RtlSdrIQ: read samples from device. Using command line arguments --- Radio/Utils/RtlSdrIQ/main.swift | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Radio/Utils/RtlSdrIQ/main.swift (limited to 'Radio/Utils/RtlSdrIQ/main.swift') diff --git a/Radio/Utils/RtlSdrIQ/main.swift b/Radio/Utils/RtlSdrIQ/main.swift new file mode 100644 index 0000000..20573a9 --- /dev/null +++ b/Radio/Utils/RtlSdrIQ/main.swift @@ -0,0 +1,62 @@ +// +// 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..