summaryrefslogtreecommitdiff
path: root/Radio/Utils/RtlSdrIQ/main.swift
blob: 20573a97b452df2609abdcbcde686767026800bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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()