diff options
author | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-11-28 11:37:28 +0000 |
---|---|---|
committer | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-11-28 11:37:28 +0000 |
commit | 77d1cfc27fb72e442b8d8e5cbe2be143723724e5 (patch) | |
tree | c42913e5fa3f2c0c23c9eeadd61c39f2a317a206 /Radio/Utils/RtlSdrIQ/main.swift | |
parent | cf4444e7390365df43ecbd3d130015c1e06ef88f (diff) | |
download | PrySDR-77d1cfc27fb72e442b8d8e5cbe2be143723724e5.tar.gz PrySDR-77d1cfc27fb72e442b8d8e5cbe2be143723724e5.zip |
RtlSdrIQ: read samples from device. Using command line arguments
Diffstat (limited to 'Radio/Utils/RtlSdrIQ/main.swift')
-rw-r--r-- | Radio/Utils/RtlSdrIQ/main.swift | 62 |
1 files changed, 62 insertions, 0 deletions
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..<args.nsamples { + let offsetByte = buf_ptr + i + print("\(String(format:"%02hhX ",offsetByte.load(as: UInt8.self)))",terminator: "") +} +device.close() + +buf_ptr.deallocate() + |