-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmustang_control.py
executable file
·430 lines (324 loc) · 8.91 KB
/
mustang_control.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
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
#!/usr/bin/env python3
###############################
# Mustang streamer controller #
###############################
import requests
import os
import time
import RPi.GPIO as GPIO
import json
import subprocess
import configparser
#### External config file
basedir = '/home/volumio/mustang_control/'
fileConfig = basedir + 'config.ini'
cfg_file = configparser.ConfigParser()
cfg_file.read(fileConfig)
# RGB Led pins (BCM)
pinR = int(cfg_file['mustang streamer config']['ledRed']) # default:23
pinG = int(cfg_file['mustang streamer config']['ledGreen']) # default:24
pinB = int(cfg_file['mustang streamer config']['ledBlue']) # default:25
# Button
# 3.3v -> switch ---> 10k -> GND
# |_ 1k -> gpio_pin (pinRESET)
pinRESET = int(cfg_file['mustang streamer config']['button']) # default:17
button_action = str(cfg_file['mustang streamer config']['long_press']) # default:poweroff
LONG_PRESS_TIME = 3.0 # in seconds
# Display poweroff timer
time_off = int(cfg_file['mustang streamer config']['timeout_display']) # Timeout display (in seconds) default:1200sec
### SETUP
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Button functions
def button(channel):
print("Button pressed")
start_time = time.time()
while GPIO.input(channel) == GPIO.HIGH:
# Check if is a long press or normal button press
if time.time() - start_time > LONG_PRESS_TIME:
print("- Long press! (more than "+str(LONG_PRESS_TIME)+" seconds)")
os.system("volumio stop")
led_Red()
os.system("sudo systemctl " + button_action )
return
else:
# Code for short press
os.system('volumio toggle')
set_timer_display()
time.sleep(1)
# GPIO: Setup button
GPIO.setup(pinRESET, GPIO.IN, GPIO.PUD_DOWN)
GPIO.add_event_detect(pinRESET, GPIO.RISING, callback=button, bouncetime=4000)
# GPIO: Setup RGB led pins
GPIO.setup(pinR,GPIO.OUT)
GPIO.setup(pinG,GPIO.OUT)
GPIO.setup(pinB,GPIO.OUT)
### General config
quality_txt = "-" # Placeholder
rainbow_timer = 0.35 # Startup led rainbow wait time
##### FUNCTIONS #####
# Volumio status
def status_volumio():
# Volumio status
response = requests.get("http://localhost:3000/api/v1/getState")
volumio = response.json()
if "samplerate" in volumio:
raw_samplerate = str(volumio["samplerate"]).split()
raw_bit = str(volumio["bitdepth"]).split()
audio_samplerate = float(raw_samplerate[0])
audio_bitrate = int(raw_bit[0])
else:
audio_samplerate = 0
audio_bitrate = 0
if "status" in volumio:
status = volumio["status"]
if status == "play":
print("In riproduzione")
# Reset display sleep timer
display_poweron()
reset_timer_display()
### AUDIO QUALITY ###
# Audio DSD/DSF
if audio_bitrate == 1:
if audio_samplerate < 5.6:
quality_txt = "DSD64"
led_Yellow()
elif audio_samplerate < 11.2:
quality_txt = "DSD128"
led_Blue()
elif audio_samplerate < 22.5:
quality_txt = "DSD256"
led_Purple()
elif audio_samplerate < 45.1:
quality_txt = "DSD512"
led_Cyan()
elif audio_samplerate >= 45.1:
quality_txt = "DSD1024"
led_White()
else:
quality_txt = "DSD format: unknown!"
led_Red()
# Audio 16bit
elif audio_bitrate == 16:
# Standard bitrate
if audio_samplerate < 44:
# Low quality
quality_txt = "LOW"
led_Red()
elif 44 < audio_samplerate < 48:
# 16bit - 44kHz
quality_txt = "CD"
led_Green()
elif 48 <= audio_samplerate < 88:
# 16bit - 48kHz
quality_txt = "SACD"
led_Yellow()
elif 88 <= audio_samplerate < 96:
# 16bit - 88kHz
quality_txt = ""
led_Blue()
elif 96 <= audio_samplerate < 192:
# 16bit - 96kHz
quality_txt = ""
led_Purple()
elif audio_samplerate == 192:
# 16bit - 192kHz
quality_txt = ""
led_Cyan()
else:
# Unknown sample rate
quality_txt = "Sample rate error"
print("### Undefined: "+volumio["samplerate"]+" "+volumio["bitdepth"])
# Audio 24bit
elif audio_bitrate == 24:
# High bitrate
if 44 < audio_samplerate < 48:
# 24bit - 44kHz
quality_txt = ""
led_Yellow()
elif 48 <= audio_samplerate < 88:
# 24bit - 48kHz
quality_txt = ""
led_Blue()
elif 88 <= audio_samplerate < 96:
# 24bit - 88kHz
quality_txt = ""
led_Purple()
elif 96 <= audio_samplerate < 192:
# 24bit - 96kHz
quality_txt = ""
led_Cyan()
elif audio_samplerate == 192:
# 24bit - 192kHz
quality_txt = ""
led_White()
else:
# Unknown sample rate
quality_txt = "Sample rate error"
print("### Undefined: "+volumio["samplerate"]+" "+volumio["bitdepth"])
led_Off()
elif audio_bitrate == 0:
# Transaction between local and streaming service
quality_txt = "Switching music service..."
led_Off()
# Unknown format
else:
quality_txt = "Unknown format"
led_Off()
# Print audio quality on terminal
print("Audio quality: "+quality_txt+" "+str(volumio["bitdepth"])+" "+str(volumio["samplerate"]) )
elif status == "pause":
print("Paused")
led_Off()
set_timer_display()
else:
print("Stopped")
led_Off()
set_timer_display()
else:
# Unknown status
print("### Status not found! ###")
led_Off()
# Volumio play/pause
def volumio_playpausa():
checkplay = requests.get("http://localhost:3000/api/v1/getState")
playstatus = checkplay.json()
if "status" in playstatus:
stato = playstatus["status"]
if stato == "play":
os.system('volumio pause')
set_timer_display()
elif (stato == "pause" or stato == "stop"):
display_poweron()
os.system('volumio play')
reset_timer_display()
else:
print("Unknown player status!")
else:
print("No status! Is Volumio running?")
# Display power off
def display_poweroff():
output = subprocess.getoutput("sudo vcgencmd display_power")
rawOut = output.split('=')
verifica = int(rawOut[1])
if verifica > 0:
print('Turning off display!')
os.system('sudo vcgencmd display_power 0')
reset_timer_display()
# Display power on
def display_poweron():
output = subprocess.getoutput("sudo vcgencmd display_power")
rawOut = output.split('=')
verifica = int(rawOut[1])
if verifica == 0:
print('WAKEUP display!')
os.system('sudo vcgencmd display_power 1')
# Power off display at sleep timer
def sleep_display():
rawTimer = open("/tmp/timer_display.dat", "r")
for line in rawTimer.readlines():
last_timer = int(line)
rawTimer.close()
if last_timer > 0:
tsNow = int( time.time() )
# Found a timestamp
ts_limite = last_timer + time_off
test = ts_limite - tsNow
if tsNow > ts_limite:
display_poweroff()
# Reset display sleep timer
def reset_timer_display():
rawTimer = open("/tmp/timer_display.dat", "r")
for line in rawTimer.readlines():
last_timer = int(line)
rawTimer.close()
if last_timer > 0:
timerfile = open("/tmp/timer_display.dat", "w")
timerfile.write("0")
timerfile.close()
# Set display sleep timer
def set_timer_display():
rawTimer = open("/tmp/timer_display.dat", "r")
for line in rawTimer.readlines():
last_timer = int(line)
rawTimer.close()
if last_timer == 0:
print('- Setting up display sleep timer')
tsNow = str ( int( time.time() ) )
timerfile = open("/tmp/timer_display.dat", "w")
timerfile.write(tsNow)
timerfile.close()
## RGB Led definitions
def led_Off():
GPIO.output(pinR,GPIO.HIGH)
GPIO.output(pinG,GPIO.HIGH)
GPIO.output(pinB,GPIO.HIGH)
def led_Green():
GPIO.output(pinR,GPIO.HIGH)
GPIO.output(pinG,GPIO.LOW)
GPIO.output(pinB,GPIO.HIGH)
def led_Red():
GPIO.output(pinR,GPIO.LOW)
GPIO.output(pinG,GPIO.HIGH)
GPIO.output(pinB,GPIO.HIGH)
def led_Blue():
GPIO.output(pinR,GPIO.HIGH)
GPIO.output(pinG,GPIO.HIGH)
GPIO.output(pinB,GPIO.LOW)
def led_White():
GPIO.output(pinR,GPIO.LOW)
GPIO.output(pinG,GPIO.LOW)
GPIO.output(pinB,GPIO.LOW)
def led_Yellow():
GPIO.output(pinR,GPIO.LOW)
GPIO.output(pinG,GPIO.LOW)
GPIO.output(pinB,GPIO.HIGH)
def led_Purple():
GPIO.output(pinR,GPIO.LOW)
GPIO.output(pinG,GPIO.HIGH)
GPIO.output(pinB,GPIO.LOW)
def led_Cyan():
GPIO.output(pinR,GPIO.HIGH)
GPIO.output(pinG,GPIO.LOW)
GPIO.output(pinB,GPIO.LOW)
# Rainbow
def led_rainbow(quanti):
for i in range(quanti):
print("Rainbow!")
led_White()
time.sleep(rainbow_timer)
led_Purple()
time.sleep(rainbow_timer)
led_Blue()
time.sleep(rainbow_timer)
led_Cyan()
time.sleep(rainbow_timer)
led_Green()
time.sleep(rainbow_timer)
led_Yellow()
time.sleep(rainbow_timer)
led_Red()
time.sleep(rainbow_timer)
led_Off()
###### END FUNCTIONS #######
### INIT ###
led_Off()
print("##### Mustang Streamer starting.. #####\n")
led_rainbow(1)
# Set first display timer
timerfile = open("/tmp/timer_display.dat", "w")
timerfile.write("0")
timerfile.close()
###### Main loop
try:
while True:
status_volumio()
sleep_display()
time.sleep(0.3)
except KeyboardInterrupt:
print("\nUser interrupt..")
led_Off()
GPIO.cleanup()
## Last
led_Off()
GPIO.cleanup()