-
Notifications
You must be signed in to change notification settings - Fork 2
/
Radio.py
254 lines (195 loc) · 8.21 KB
/
Radio.py
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
from PyQt4 import QtCore
from PyQt4.QtCore import QObject
from Helpers.perpetualTimer import perpetualTimer
from threading import Timer,Thread,Event
from Modules.Widgets.RadioWidget import *
from Modules.Objects.radioPreset import *
from Modules.Objects.AlarmState import *
import json
import os.path
import subprocess
import shlex
class Radio(QObject):
UsesAudio = True
ListenForPinEvent = True
offButton = None
onButton = None
audioRelayPin = None
contextPin = None
audioStatusDisplay =""
Enabled = True
menuOrder = 2
isPlaying = False
widgetVisible = False
radioPresetFile = "radioPresets.json"
radioPresets = []
subprocessAvailable = True
audioDeviceIdentifier = "plughw:1,0"
rtlfmCommand = "rtl_fm -M wbfm -f @FREQ"
aplayCommand ="aplay -D @DEVICE -r 32000 -f S16_LE -t raw -c 1"
alarmTypeDisplay = "RADIO"
def __init__(self):
super(Radio, self).__init__()
try:
subprocess.Popen(shlex.split("echo Checking if subprocess module is available"))
except:
print("** subprocess module unavailable **")
self.subprocessAvailable = False
def showWidget(self):
"""Required for main modules to be inserted into the menu"""
self.radio_widget.setVisible(True)
self.widgetVisible = True
self.showButtonIndicators()
def hideWidget(self):
"""Required for main modules to be inserted into the menu"""
self.radio_widget.setVisible(False)
self.widgetVisible = False
def addMenuWidget(self, parent):
"""Required for main modules to be inserted into the menu"""
self.radio_widget = RadioWidget(parent)
self.radio_widget.setGeometry(QtCore.QRect(0, 0, 320, 210))
self.radio_widget.setVisible(False)
self.connect(self.radio_widget, QtCore.SIGNAL('presetChanged'), self.presetChangedCallback)
self.connect(self.radio_widget, QtCore.SIGNAL('frequencyChanged'), self.frequencyChangedCallback)
self.initialize()
def getMenuIcon(self):
"""Required for main modules to be inserted into the menu"""
return "icons/radio-tower.svg"
def getMenuIconSelected(self):
"""Required for main modules to be inserted into the menu"""
return "icons/radio-towerSelected.svg"
def getMenuIconHeight(self):
"""Required for main modules to be inserted into the menu"""
return 65
def getMenuIconWidth(self):
"""Required for main modules to be inserted into the menu"""
return 65
def setPin(self, pinConfig):
self.offButton = pinConfig["OFF_BUTTON"]
self.onButton = pinConfig["ON_BUTTON"]
self.audioRelayPin = pinConfig["AUDIO_ONE_SWITCH"]
self.contextPin = pinConfig["CONTEXT_BUTTON"]
def processPinEvent(self, pinNum):
if(self.widgetVisible and self.radio_widget.settingPreset == True):
self.radio_widget.fillPresets(self.radioPresets)
else:
if(self.offButton == pinNum):
self.stop()
elif(self.widgetVisible and self.onButton == pinNum):
self.play()
elif(self.widgetVisible and self.contextPin == pinNum):
self.preparePresetChange()
def initialize(self):
self.loadRadioPresets()
self.radio_widget.fillPresets(self.radioPresets)
def alarmFired(self, args):
self.frequencyChangedCallback(args["details"])
self.play(True)
def getPossibleAlarmDetails(self):
result = {}
result["name"] = self.alarmTypeDisplay
result["moduleName"] = type(self).__name__
result["alarmType"] = AlarmState.RADIO
possible = []
for x in range(0, len(self.radioPresets)):
pre = self.radioPresets[x]
possible.append(str(pre.frequency))
result["options"] = possible
return result
def showButtonIndicators(self):
if(self.widgetVisible):
btns =[]
btns.append("ON")
btns.append("CONTEXT")
if(self.isPlaying):
btns.append("OFF")
self.emit(QtCore.SIGNAL('requestButtonPrompt'),btns)
def getAudioStatusDisplay(self):
self.audioStatusDisplay = str(self.radio_widget.currentFrequency)
return self.audioStatusDisplay
def getPossibleButtons(self):
self.indicator = ButtonIndicator(self)
self.indicator.setGeometry(QtCore.QRect(0, 0, 30, 30))
def frequencyChangedCallback(self, freq):
self.audioStatusDisplay = str(freq)
if(self.isPlaying):
self.play()
def preparePresetChange(self):
print("prepare preset change")
self.radio_widget.fillPresets(self.radioPresets, True)
def presetChangedCallback(self, d):
for x in range(0, len(self.radioPresets)):
pre = self.radioPresets[x]
if(int(pre.id) == int(d[0])):
pre.frequency = str(d[1])
self.saveRadioPresets()
self.radio_widget.fillPresets(self.radioPresets)
def loadRadioPresets(self):
presetData = None
buildDefaultData = False
if(os.path.isfile(self.radioPresetFile) == False):
buildDefaultData = True
try:
with open(self.radioPresetFile) as data_file:
presetData = json.load(data_file)
except:
buildDefaultData = True
if(buildDefaultData == True or presetData == None):
presetData = []
for x in range(0,3):
d = {}
d["frequency"] = "88.1"
d["id"] = str(x)
presetData.append(d)
with open(self.radioPresetFile, 'w') as outfile:
json.dump(presetData, outfile)
self.radioPresets = []
with open(self.radioPresetFile) as data_file:
data = json.load(data_file)
for x in range(0, len(data)):
pre = radioPreset(data[x])
self.radioPresets.append(pre)
def saveRadioPresets(self):
data = []
for x in range(0, len(self.radioPresets)):
pre = self.radioPresets[x]
d = {}
d["frequency"] = str(pre.frequency)
d["id"] = str(pre.id)
data.append(d)
with open(self.radioPresetFile, 'w') as outfile:
json.dump(data, outfile)
def play(self, forcePlay=False):
if((self.widgetVisible == True or forcePlay == True) and self.radio_widget.currentFrequency):
self.isPlaying = True
self.emit(QtCore.SIGNAL('audioStarted'), self)
self.emit(QtCore.SIGNAL('pinRequested'), self.audioRelayPin)
if(self.subprocessAvailable):
self.sendKillCommand()
time.sleep(1)
rtlfmStr = self.rtlfmCommand.replace("@FREQ", str(self.radio_widget.currentFrequency) + "M")
aplayStr = self.aplayCommand.replace("@DEVICE", str(self.audioDeviceIdentifier))
rtlfm = subprocess.Popen(shlex.split(rtlfmStr), stdout=subprocess.PIPE)
subprocess.Popen(shlex.split(aplayStr), stdin=rtlfm.stdout, stdout=subprocess.PIPE)
self.emit(QtCore.SIGNAL('broadcastModuleRequest'), self, "audioStatusChange", "on", None, "ScreenManager")
self.showButtonIndicators()
def stop(self):
self.radio_widget.fillPresets(self.radioPresets)
if(self.isPlaying == True):
print("stopping radio")
self.audioStatusDisplay = ""
self.isPlaying = False
self.emit(QtCore.SIGNAL('audioStopped'), self)
self.emit(QtCore.SIGNAL('broadcastModuleRequest'), self, "audioStatusChange", "off", None, "ScreenManager")
if(self.subprocessAvailable):
self.sendKillCommand()
self.showButtonIndicators()
def sendKillCommand(self):
subprocess.Popen(shlex.split("sudo killall rtl_fm"))
subprocess.Popen(shlex.split("sudo killall aplay"))
def dispose(self):
print("Disposing of Radio")
try:
self.stop()
except:
print("problem disposing of radio")