-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradiocontrols.py
126 lines (100 loc) · 3.09 KB
/
radiocontrols.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
#!/usr/bin/env python3
##### Imports #################################################################
# Also requires external programs/scripts: mpc, piradio, station, snooze
# See https://github.com/ednl/piradio
from dothat import touch, lcd, backlight
import re, signal, time, subprocess, threading
##### Globals #################################################################
# To extract numbers from "mpc volume" command output
findnum = re.compile(r"\d+")
# Read station IDs from plain text file with no empty lines,
# every line structured as: <unique-station-ID><whitespace><stream-URL>
# Same file as used by external "piradio" and "station" scripts
index = 0
stations = []
with open("/home/pi/radio.txt") as file:
for line in file:
stations.append(line.split()[0])
##### Functions ###############################################################
# Write to LCD
def line(row, txt):
lcd.set_cursor_position(0, row)
lcd.write("{:16}".format(txt))
# Avoid dealing with Timer function arguments
def leds_off():
backlight.set_graph(0)
# Update LCD text and LED graph
def show():
global index
# Turn on LCD backlight, set timer to turn off
backlight.rgb(255, 255, 255)
t1 = threading.Timer(4.0, backlight.off)
t1.start()
# Get radio info
station = subprocess.check_output("station", universal_newlines=True).strip()
volume = subprocess.check_output("mpc volume", shell=True, universal_newlines=True).strip().capitalize()
snooze = subprocess.check_output("snooze", universal_newlines=True).strip()
# Update LCD text
line(0, "Stream: " + station)
line(1, volume)
line(2, "Snooze: " + snooze)
# Turn on LED graph, set timer to turn it off
percent = float(findnum.search(volume).group()) / 100
backlight.set_graph(percent)
t2 = threading.Timer(1.5, leds_off)
t2.start()
# Update station index
try:
index = stations.index(station)
except ValueError:
index = 0
# Tune radio to station, update info
def tune():
if 0 <= index < len(stations):
subprocess.call("piradio " + stations[index], shell=True)
show()
##### Hooks ###################################################################
# Volume up
@touch.on(touch.UP)
def press_up(channel, event):
subprocess.call("piradio vol +", shell=True)
show()
# Volume down
@touch.on(touch.DOWN)
def press_down(channel, event):
subprocess.call("piradio vol -", shell=True)
show()
# Radio off
@touch.on(touch.CANCEL)
def press_cancel(channel, event):
backlight.rgb(255, 255, 255)
t = threading.Timer(2.0, backlight.off)
t.start()
subprocess.call("piradio off", shell=True)
lcd.clear()
backlight.set_graph(0)
# Radio on
@touch.on(touch.BUTTON)
def press_button(channel, event):
tune()
# Previous station
@touch.on(touch.LEFT)
def press_left(channel, event):
global index
index -= 1
if index < 0:
index = len(stations) - 1
tune()
# Next station
@touch.on(touch.RIGHT)
def press_right(channel, event):
global index
index += 1
if index >= len(stations):
index = 0
tune()
##### Main ####################################################################
# Initial update
show()
# Suspend program while keeping touch controls active
signal.pause()