-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreqscan.py
More file actions
177 lines (147 loc) · 5.01 KB
/
freqscan.py
File metadata and controls
177 lines (147 loc) · 5.01 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
#!/usr/bin/python
#
# Script to scan for activity on frequency ranges and log unique
# frequencies with a few seconds of audio.
#
import serial
import time
import re
import threading
import alsaaudio
import sys
run = 1
doRec = 0
currentFrequency = ""
LCDSCREEN = ""
scannedFrequencies = []
def writeout(str):
print time.strftime("%Y-%m-%d %H:%M:%S"), str
rlog = open("rec_freqscan.log", 'a')
freqlog = open("frequencies.log", 'a')
class RecordThread (threading.Thread):
def run(self):
global currentFrequency
global run
global doRec
global rlog
if doRec == 1:
rlog.write( '%(time)s %(freq)s START RECORD\n' % {'time': time.strftime("%Y-%m-%d %H:%M:%S"), 'freq': currentFrequency})
filename = 'audio/freqscan_%(time)s_%(freq)s.raw' % {'time': time.strftime("%Y%m%d%H%M%S"), 'freq': currentFrequency.replace(" ", "_")}
f = open(filename, 'wb')
card = 'plughw:CARD=Intel,DEV=0'
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, card)
inp.setchannels(1)
inp.setrate(8000*2)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(160)
out = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, card=card)
out.setchannels(1)
out.setrate(8000*2)
out.setformat(alsaaudio.PCM_FORMAT_S16_LE)
out.setperiodsize(160)
while doRec:
l, data = inp.read()
if l:
f.write(data)
out.write(data)
time.sleep(.001)
# Thread finished
rlog.write( '%(time)s %(freq)s STOP RECORD\n' % {'time': time.strftime("%Y-%m-%d %H:%M:%S"), 'freq': currentFrequency})
f.close()
class InputThread (threading.Thread):
def run(self):
global ser
global run
global doRec
global scannedFrequencies
global LCDSCREEN
while run:
val = raw_input('')
if val == 'q':
run = 0
doRec = 0
writeout("Quitting")
return
if val == 's':
ser.write("KEY00\r")
ser.readline(eol="\r")
writeout("Skipped frequency")
if val == 'd':
writeout("Dumping frequency table")
print "--"
print scannedFrequencies
print "--"
if val == 'l':
writeout("Dumping LCD screen")
print LCDSCREEN
InputThread().start()
ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 19200
ser.open()
"""
LCD1 [SRCH + 5k ][#### ]
LCD2 [ 454.6750 FM ][ ]
LCD3 [Range 4 ][ ]
LCD4 [ ][ ]
LCD1 [ + 5k ][ ]
LCD2 [ 472.0400 FM ][ ]
LCD3 [Range -234------][ # ]
LCD4 [ ][ ]
"""
recordStarted = 0
openChannel = 0
try:
while run:
ser.write("LCD\r")
LCD1 = ser.readline(eol="\r")
LCD2 = ser.readline(eol="\r")
LCD3 = ser.readline(eol="\r")
LCD4 = ser.readline(eol="\r")
LCDSCREEN = LCD1 + "\n" + LCD2 + "\n" + LCD3 + "\n" + LCD4
currentFrequencyRe = re.search('LCD2\s\[\s+(\S+).*\]', LCD2)
if currentFrequencyRe != None:
currentFrequency = currentFrequencyRe.group(1).strip()
"""if lastFrequency != currentFrequency:
print "Frequency: %s" % currentFrequency
"""
scannerStatusRe = re.search('LCD1\s\[(\S+)\s.*\]', LCD1)
if scannerStatusRe != None:
scannerStatus = scannerStatusRe.group(1).strip()
if scannerStatus == 'SRCH':
openChannel = 1
if doRec == 0:
writeout("Activity on frequency: %s" % currentFrequency)
if currentFrequency not in scannedFrequencies:
writeout("New frequency: %s" % currentFrequency)
writeout("Start record for max 15 seconds")
freqlog.write( '%(time)s %(freq)s MHz\n' % {'time': time.strftime("%Y-%m-%d %H:%M:%S"), 'freq': currentFrequency})
freqlog.flush()
doRec = 1
recordStarted = time.time()
RecordThread().start()
scannedFrequencies.append(currentFrequency)
else:
writeout("Resuming scan.")
ser.write("KEY00\r")
ser.readline(eol="\r")
openChannel = 0
elif recordStarted + 15 < time.time():
doRec = 0
writeout("Stop record. Resuming scan.")
ser.write("KEY00\r")
ser.readline(eol="\r")
openChannel = 0
else:
openChannel = 0
if doRec == 1 and openChannel == 0:
writeout("Transmission ended. Stop record. Resume scan.")
doRec = 0
lastFrequency = currentFrequency
time.sleep(0.2)
ser.close()
except KeyboardInterrupt:
writeout("Got ^C. Cleaning up..")
ser.close()
doRec = 0
run = 0