-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
621 lines (524 loc) · 21.7 KB
/
utils.py
File metadata and controls
621 lines (524 loc) · 21.7 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
############################################################################
#
# utils.py - Rev 1.0
# Copyright (C) 2021-5 by Joseph B. Attili, joe DOT aa2il AT gmail DOT com
#
# Support routines for pySDR.
#
############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
############################################################################
import sys
try:
import SoapySDR
from SoapySDR import * # SOAPY_SDR_ constants
except ImportError:
print('UTIL: Unable to import SoapySDR - be sure to use -FAKE flag')
SOAPY_SDR_RX=1
from sig_proc import ring_buffer2,ring_buffer3
import numpy as np
import time
from Tables import MODES,SDRplaysrates,RTLsrates
import io
from threading import enumerate
from rig_io import bands,CONNECTIONS,RIGS
from multiprocessing import active_children
if True:
# Dynamic importing - this works!
from widgets_qt import QTLIB
exec('from '+QTLIB+'.QtWidgets import QMessageBox,QApplication')
exec('from '+QTLIB+'.QtGui import QIcon, QPixmap')
elif False:
from PyQt6.QtWidgets import QMessageBox,QApplication
from PyQt6.QtGui import QIcon, QPixmap
elif False:
from PySide6.QtWidgets import QMessageBox,QApplication
from PySide6.QtGui import QIcon, QPixmap
else:
from PyQt5.QtWidgets import QMessageBox,QApplication
from PyQt5.QtGui import QIcon, QPixmap
from utilities import freq2band, error_trap, whoami
from rtlsdr import RtlSdr
from sig_proc import ring_buffer2
import asyncio
import threading
############################################################################
# This is the "fake" rtl driver, probably a bad name for it but ...
# It is an alternative to Soapy and only relies on the python rtlsdr library
# which has been seen to be quite stable and reliable. It has not been
# completely vetted but seems to work.
#
# Most of the methods in this class are meant to mimic similar methods in
# Soapy. The exception is readSteam where I haven't quite figured out how
# to modify function arguments - but who cares!
class RTL_SDR_DRIVER:
def __init__(self,P):
print('======================================================Init RTL_DRIVER ...')
self.device = RtlSdr()
self.direct=None
self.ret=None
self.key='RTLSDR'
self.P=P
if P.MP_SCHEME==1:
self.rb = ring_buffer2('IO',256*1024,PREVENT_OVERFLOW=False)
elif P.MP_SCHEME==2:
self.rb = ring_buffer3('IO',256*1024)
# Using an avent is probably the right way to do this but I haven't gotten it to work ?!
#self.Enable = asyncio.Event()
#self.Enable.clear()
self.loop = asyncio.get_event_loop()
self.th = threading.Thread(target=self.Streamer2, args=(),
name='RTL Streamer')
self.th.daemon=True
#self.th.start() # Do this here if we ever get the event-drive paradigm to work
P.threads.append(self.th)
# Routine to continuously read blocks of samples from the RTL device
# and pump them into a ringbuffer.
def Streamer2(self):
print('=====================RTL_SDR_DRIVER:',whoami(),'Starting ===================================================.')
#loop = asyncio.get_event_loop()
self.loop.run_until_complete(self.streaming())
async def streaming(self):
"""
print('============================ RTL_SDR_DRIVER:',whoami(),
' ... Waiting ... ==========================')
await self.Enable.wait()
print('============================ RTL_SDR_DRIVER:',whoami(),
' ... Streaming ... ==========================')
"""
async for x in self.device.stream():
#print(x,len(x))
self.rb.push(x)
print('============================ RTL_SDR_DRIVER:',whoami(),
' ... Done ... ==========================')
def setSampleRate(self,rx, b, fs):
print('RTL_SDR_DRIVER:',whoami(),rx, b, fs)
self.device.sample_rate = fs
print(self.device.rs)
#sys.exit(0)
def getSampleRate(self,rx, b):
print('RTL_SDR_DRIVER:',whoami(),rx, b)
return self.device.sample_rate
def setFrequency(self,rx, b, tag, f=None):
print('RTL_SDR_DRIVER:',whoami(),rx, b, tag, f)
if f==None:
f=tag
self.device.center_freq = int(f)
elif tag=='RF':
self.device.center_freq = int(f)
elif tag=='CORR':
try:
# This causes a problem - not sure why?
#self.device.freq_correction = f
pass
except:
error_trap('UTILS->SET FREQUENCY - Unable to set freq correction')
else:
print(whoami(),'I dont know what I am doing here!')
print('fc=',self.device.fc,'\tdf=',self.device.freq_correction)
#sys.exit(0)
def getFrequency(self,rx, b, tag):
print('RTL_SDR_DRIVER:',whoami(),rx, b, tag)
return self.device.center_freq
def getNumChannels(self,rx):
print('RTL_SDR_DRIVER:',whoami(),rx)
return 1
def writeSetting(self,s1,s2):
print('RTL_SDR_DRIVER:',whoami(),s1,s2)
if s1=='if_mode':
pass
elif s1=='direct_samp':
self.direct=int(s2)
self.device.set_direct_sampling(int(s2))
print(int(s2))
#sys.exit(0)
else:
print('RTL_SDR_DRIVER:',whoami(),'Option not implemented yet - ignored')
def listGains(self,rx,b):
print('RTL_SDR_DRIVER:',whoami(),rx,b)
return self.device.valid_gains_db
def getGainRange(self,rx,b,stage=None):
print('RTL_SDR_DRIVER:',whoami(),rx,b,stage)
gains=self.device.valid_gains_db
return [min(gains),max(gains),1]
def hasGainMode(self,rx,b):
print('RTL_SDR_DRIVER:',whoami(),rx,b)
return True
def setGainMode(self,rx,b,tf):
print('RTL_SDR_DRIVER:',whoami(),rx,b,tf)
self.device.gain = 'auto'
def getGainMode(self,rx,b):
print('RTL_SDR_DRIVER:',whoami(),rx,b)
return self.device.gain
def getGain(self,rx,b,stage):
gain=self.device.gain
print('RTL_SDR_DRIVER:',whoami(),rx,b,stage,gain)
return gain
def setGain(self,rx,b,stage,gain):
if gain>49.6:
gain=49.6
self.device.gain = gain
print('RTL_SDR_DRIVER:',whoami(),rx,b,stage,gain)
#sys.exit(0)
def setAntenna(self,rx,b,ant):
print('RTL_SDR_DRIVER:',whoami(),rx,b,ant)
def getAntenna(self,rx,b):
print('RTL_SDR_DRIVER:',whoami(),rx,b)
return 0
def getSettingInfo(self):
print('RTL_SDR_DRIVER:',whoami())
return []
def listSampleRates(self,rx,b):
print('RTL_SDR_DRIVER:',whoami(),rx,b)
return [1.024e6,2.048e6]
def listBandwidths(self,rx,b):
print('RTL_SDR_DRIVER:',whoami())
return []
def getBandwidth(self,rx,b):
print('RTL_SDR_DRIVER:',whoami(),rx,b)
bw = self.device.bandwidth
return bw
def setupStream(self,rx,fmt):
print('RTL_SDR_DRIVER:',whoami(),rx,fmt)
self.fmt=fmt
return 0
def activateStream(self,rx):
print('RTL_SDR_DRIVER:',whoami(),rx)
#self.Enable.set()
#print('============================ RTL_SDR_DRIVER:',whoami(),
# ' ... Enabled ... ==========================',self.Enable.is_set())
self.th.start() # Do this here for now until we get the event-drive paradigm to work
def readStreamRTL(self,rx,n):
#print('RTL_SDR_DRIVER:',whoami(),n)
if self.rb.ready(n):
x=self.rb.pull(n)
#print('RTL_SDR_DRIVER:',x,n,len(x))
else:
x=[]
#print('RTL_SDR_DRIVER:',whoami(),n,len(x))
return np.array(x, np.complex64)
def deactivateStream(self,rx):
print('RTL_SDR_DRIVER:',whoami(),rx)
#self.Enable.clear()
#self.device.cancel_read_async()
self.device.stop()
def closeStream(self,rx):
print('RTL_SDR_DRIVER:',whoami(),rx)
#self.device.stop()
self.device.close()
def getDriverKey(self):
print('RTL_SDR_DRIVER:',whoami())
return self.key
def getHardwareKey(self):
print('RTL_SDR_DRIVER:',whoami())
return self.device.get_tuner_type()
def getHardwareInfo(self):
print('RTL_SDR_DRIVER:',whoami())
#return self.device.init_device_values()
return []
############################################################################
def adjust_foffset(self):
M = round( self.RB_SIZE * self.FOFFSET / self.SRATE )
fo = M*self.SRATE/self.RB_SIZE
if False:
print('\nTuning offset adjustment:')
print(' fo=',self.FOFFSET)
print(' fs=',self.SRATE)
print(' N=',self.RB_SIZE)
print(' M=',M)
print(' new fo=',fo)
print(' change=',fo-self.FOFFSET,'\n')
sys.exit(0)
self.FOFFSET=fo
# Function to apply SDR settings
def setupSDR(P):
sdr=P.sdr;
print('\n>>>>>>>>>>>>>>> Setting up SDR ...',P.SDR_TYPE)
sdr.setSampleRate(SOAPY_SDR_RX, 0, P.SRATE)
sdr.setFrequency(SOAPY_SDR_RX, 0, 'RF', P.FC[0]-P.FOFFSET)
sdr.setFrequency(SOAPY_SDR_RX, 0, 'CORR', P.PPM)
if P.LNA>=0:
s="rfgain_sel"
sdr.writeSetting(s,str(P.LNA))
# Convert IF freq to a string
s1="if_mode"
if P.IF==0:
s2 = "Zero-IF"
else:
s2=str(P.IF)+"kHz"
sdr.writeSetting(s1,s2)
if P.IFGAIN>0:
if P.SDR_TYPE=='sdrplay':
stage="IFGR"
elif P.SDR_TYPE=='rtlsdr':
stage="TUNER"
else:
print("Error - unknown SDR type - aborting")
sys.exit(0)
print("Get Gain =",sdr.getGain(SOAPY_SDR_RX, 0,stage))
sdr.setGain(SOAPY_SDR_RX, 0,stage,P.IFGAIN)
print("Get Gain =",sdr.getGain(SOAPY_SDR_RX, 0,stage))
# Select antenna
if P.ANTENNA=='A':
ant1 = 'Antenna A'
elif P.ANTENNA=='B':
ant1 = 'Antenna B'
elif P.ANTENNA=='Z':
ant1='Hi-Z'
else:
print("\n$$$$$$$$$$$$$$$$ ERROR - Unrecongized Antenna $$$$$$$$$$$$$$$",P.ANTENNA)
print("SDR should revert to default (Antenna A)\n")
sdr.setAntenna(SOAPY_SDR_RX, 0,ant1)
# Do some error checking
bw = sdr.getBandwidth(SOAPY_SDR_RX, 0)
if bw>0. and abs( P.FOFFSET ) > bw/2:
print("\n*** ERROR in setupSDR: requested RF offset outside tuner bandwidth")
print("Offset =",P.FOFFSET*1e-3, \
" KHz Max = Half Bandwidth =",bw*0.5e-3, \
" KHz\n")
sys.exit(1)
if P.IF!=0 and P.SRATE>1e6:
print('\n************************************************************************************')
print('************ WARNING - Should use Zero-IF for high sampling rate *******************')
print('************************************************************************************\n')
if P.SDR_TYPE=='rtlsdr':
P.sdr.writeSetting("direct_samp",str(P.DIRECT_SAMP))
############################################################################
# Routine to read & print current SDR settings
def check_sdr_settings(P):
print("\n------------- Current SDR Settings -------------")
if P.REPLAY_MODE:
return
sdr = P.sdr;
print('\nDriver Key: ',sdr.getDriverKey())
print('Hardware Key: ',sdr.getHardwareKey())
print('Hardware Info:',sdr.getHardwareInfo())
ant = sdr.getAntenna(SOAPY_SDR_RX, 0)
fs = sdr.getSampleRate(SOAPY_SDR_RX, 0)
#decM = sdr.getDecim(SOAPY_SDR_RX, 0)
fc = sdr.getFrequency(SOAPY_SDR_RX, 0,'RF')
ppm = sdr.getFrequency(SOAPY_SDR_RX, 0,'CORR')
bw = sdr.getBandwidth(SOAPY_SDR_RX, 0)
if P.SDR_TYPE=='sdrplay':
gain = sdr.getGain(SOAPY_SDR_RX, 0,"IFGR")
decM = sdr.getGain(SOAPY_SDR_RX, 0,"DECIM")
decEnable = sdr.getGain(SOAPY_SDR_RX, 0,"DECenable")
srate = sdr.getGain(SOAPY_SDR_RX, 0,"SRATE")
srate_req = sdr.getGain(SOAPY_SDR_RX, 0,"reqSRATE")
else:
gain = sdr.getGain(SOAPY_SDR_RX, 0,"TUNER")
decM = 1
decEnable = 0
srate = fs
srate_req = fs
print("Antenna =",ant)
print("Sample rate =",fs,' Hz =',fs*1e-3,' KHz =',fs*1e-6,' MHz')
print("Center freq =",fc,' Hz =',fc*1e-3,' KHz =',fc*1e-6,' MHz')
print("PPM =",ppm)
print("Bandwidth =",bw,' Hz =',bw*1e-3,' KHz =',bw*1e-6,' MHz')
print("Gain =",gain)
print("Decim =",decM,decEnable)
print("srate =",srate,srate_req)
if P.SDR_TYPE=='rtlsdr':
#setenv SOAPY_SDR_LOG_LEVEL 9
print('\nDriver Key: ',sdr.getDriverKey())
print('Hardware Key: ',sdr.getHardwareKey())
print('Hardware Info:',sdr.getHardwareInfo())
print('Num channels: ',sdr.getNumChannels(SOAPY_SDR_RX))
print('List Gains: ',sdr.listGains(SOAPY_SDR_RX, 0))
print('Gain Range: ',sdr.getGainRange(SOAPY_SDR_RX, 0))
T=sdr.hasGainMode(SOAPY_SDR_RX, 0)
print('Has Gain Mode:',T)
sdr.setGainMode(SOAPY_SDR_RX, 0,True)
print('Gain Mode: ',sdr.getGainMode(SOAPY_SDR_RX, 0))
print('List BWs: ',sdr.listBandwidths(SOAPY_SDR_RX, 0))
print(' ')
info=sdr.getSettingInfo()
idx=-1
keys=[];
for a in info:
idx+=1
keys.append(a.key);
for s in keys:
print("Read Setting: ",s," =",sdr.readSetting(s))
print("\n")
# Routine to list threads
def show_threads():
print('\nList of running threads:')
threads = enumerate() # enumerate is from the threading lib
for th in threads:
print(th)
print('\nList of active children:')
threads = active_children()
for th in threads:
print(th)
print(' ')
# Routine to return FT4 freqs on the same band as an arbitrary freq
def expand_ft4(frqs):
frqs2 = []
for fc in frqs:
#b = convert_freq2band(fc,True)
b = freq2band(fc*1e-3)
f = float(bands[b]['FT4'])
# frqs2.append(fc)
# frqs2.append(f)
# return np.array( frqs2 )
frqs2.append(f)
#print 'EXPAND_FT4:',frqs,frqs2,list(frqs)+frqs2
return np.array( list(frqs)+frqs2 )
# Function to determine SDR type
def find_sdr_device(self,args):
# Check to replay a data file - no SDR needed for this
if len( args.replay )>0:
self.REPLAY_MODE = True
self.REPLAY = args.replay[0]
if len( args.replay )>1:
self.REPLAY_START = float( args.replay[1] )
else:
self.REPLAY_START = 0
self.SDR_TYPE = 'replay'
self.DIRECT_SAMP = 0
return None
Done=False
while not Done:
if self.USE_FAKE_RTL:
print('Howdy Ho!')
dev='driver=rtlsdr,rtl=0'
dev='driver=rtlsdr,serial=00000001'
print('dev=',dev)
self.SDR_TYPE = 'rtlsdr'
self.REPLAY_MODE=False
self.DIRECT_SAMP = 2
sdr = RTL_SDR_DRIVER(self)
return sdr
#sdr = SoapySDR.Device( dict(driver=dev) )
#sdrkey = sdr.getDriverKey()
#sys.exit(0)
# Get list of devices attached to the computer
# For some reason, the new driver sometimes fails the first time so do it twice
for ntry in range(1,3):
print('Looking for SDR devices ...',ntry)
devices = SoapySDR.Device.enumerate()
if len(devices)==0:
time.sleep(1)
else:
break
# If we use the pre-built Soapy libs, this seems to find an
# "audio" device, at least on the RPi.
# Sift through the list of found devices and only keep the ones
# we can work with.
if len(devices)>1 or False:
print('\nFIND_SDR_DEVICE: Found',len(devices),'SDR devices ...')
print('devices=',devices)
print( type( devices ) )
valid_devices=[]
for dev in devices:
print('\ndev=',dev)
print('driver=',dev['driver'])
if dev['driver'] in ['rtlsdr','sdrplay']:
print('*** Recognized SDR Device ***')
valid_devices.append(dev)
#print( type( dev['driver'] ) )
print('valid_devices=',valid_devices)
devices=valid_devices
#sys.exit(0)
# For now, we only can handle one device
if len(devices)==0:
print('\n******************************************')
print('*** -- No SDR Device Found -- ***')
print('*** Make sure it is plugged in ***')
print('*** ***')
print('*** If problem persists, kill driver: ***')
print('*** ps -aux | fgrep sdrplay ***')
print('*** kill -9 <proc number> ***')
print('******************************************')
if True:
app = QApplication(sys.argv)
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Information)
msgBox.setText("Could not find SDR !!!\n\nMake sure it is plugged in and click OK to try again ...")
msgBox.setWindowTitle("No SDR Found")
#msgBox.setStandardButtons(QMessageBox.Ok)
msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
#msgBox.buttonClicked.connect(msgButtonClick)
returnValue = msgBox.exec()
if returnValue == QMessageBox.Ok:
print('OK clicked')
#return False,None
elif returnValue == QMessageBox.Cancel:
print('Cancel clicked')
#return True,None
sys.exit(0)
elif len(devices)>1:
print('FIND_SDR_DEVICE: ERROR - Need some more code to handle multiple SDR devices')
print('devices=',devices)
sys.exit(0)
else:
# If we get here, we found the device - check what it is
Done=True
print('devices=',devices)
dev=devices[0]['driver']
print('dev=',dev)
try:
sdr = SoapySDR.Device( dict(driver=dev) )
sdrkey = sdr.getDriverKey()
except:
error_trap('UTILS->FIND SDR DEVICE',1)
print('\n*******************************************')
print('* Unable to open driver - dev=',dev,' *')
print('* Perhaps you need to restart the driver? *')
print('* *')
print('* For the SDRplay: *')
print('* psa | fgrep -i sdrplay *')
print('* sudo pkill -f sdrplay_apiService *')
print('* /usr/local/bin/sdrplay_apiService & *')
print('* *')
print('*******************************************')
sys.exit(0)
# Is it the SDRplay RSP?
if sdrkey=='SDRplay':
if not args.rtl and not args.rtlhf:
self.SDR_TYPE = 'sdrplay' # Yeah, this is redundant
self.REPLAY_MODE=False
self.DIRECT_SAMP = 0
else:
print('\n***************************************')
print('*** -- User Device Spec Conflict -- ***')
print('*** RTL asked for but SDRplay Found ***')
print('***************************************')
sys.exit(0)
# Is it the RTL-SDR?
elif sdrkey=='RTLSDR':
self.SDR_TYPE = 'rtlsdr'
self.REPLAY_MODE=False
if not args.sdrplay:
if args.rtl and not args.rtlhf:
self.DIRECT_SAMP = 0
elif not args.rtl and args.rtlhf:
self.DIRECT_SAMP = 2
else:
print("********* Ambiguous mode for RTL - assuming HF ******")
self.DIRECT_SAMP = 2
else:
print('\n***************************************')
print('*** -- User Device Spec Conflict -- ***')
print('*** RTL asked for but SDRplay Found ***')
print('***************************************')
sys.exit(0)
else:
print('\n***************************************')
print('*** --- Unknown SDR Device --- ***')
print('*** sdrkey=',sdrkey,' ***')
print('***************************************')
sys.exit(0)
return sdr