-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttonhandler.sh
executable file
·370 lines (272 loc) · 8.57 KB
/
buttonhandler.sh
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
#!/bin/bash
##################################################################
# Written by V. Loschiavo
# Feb 12 2017
# Pandora player (vloradio) button handler
# Don't forget your pull up resistors (or use the internal pullups per the below:
# Initial idea taken from: https://github.com/AyMac/Pandoras-Box
#
# Adapted to use clock delay versus sleep from: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
#
# I put the following commands in the pianobar startup script for each button pin
# gpio -g mode 6 in # Sets pin mode to input
# gpio -g mode 6 up # Sets internal pull-up resistor
# gpio-watch 6:switch # Sets up an interrupt on pin 6 with the "switch" type (switch uses a 100ms (100000us) software debounce)
#
# Broadcom GPIO |
# Pin number number | Function
# ----------------------------------------------------------
# 4 | Tap=Redraw Now Playing / Hold=Shutdown
# 12 | Tap=Display Temp / Hold=Skip Track
# 22 | Tap=Love song / Hold=Ban song
# 18 | Tap=Mute/Unmute / Hold=Return to sane volume level
# 23 | Tap=Play/Pause / Hold=Tired of Song
# 24 | Tap=Menu/Select / Hold=Go back
# -----------------------------------------------------------
#
# $1 = the GPIO pin triggered
# $2 = the value of the pin (0 || 1)
#
##################################################################
TRIGGEREDPIN=$1
# User defined Variables:
# Interval, in seconds, which the button needs to be held
if [ $TRIGGEREDPIN = 4 ]
then
# Make the interval longer for the shutdown button
INTERVAL=3
else
INTERVAL=1
fi
# Ensure there are some values if they weren't set elsewhere.
if [ -z $SCRIPTSBASEDIR ]
then
export SCRIPTSBASEDIR="/home/vloschiavo/src/vloradio"
fi
. ${SCRIPTSBASEDIR}/config.sh
##################################################################
# Functions
##################################################################
# Get Song Title
GETSONGTITLE() {
read -r songtitle < ${EPHEMERAL}/pandoraout
}
# Effect some song based action on pianobar (i.e. ban, love, skip, etc)
# $1=pianobar command letter ; $2=Message to display on LCD line 1. Line 2 will display the song title
SONGACTION() {
echo -n "$1" >> $CTLFILE
GETSONGTITLE
$DISPLAYMESSAGE "$2" "$songtitle"
}
# Change Station
CHANGESTATION() {
# Change stations - $1 = Station Name to grep
STATIONNUM=`grep -i "$1" ${EPHEMERAL}/stationList | awk -F ":" '{print $1}'`
STATIONNAME=`grep ^${STATIONNUM} ${EPHEMERAL}/stationList | awk -F ":" '{print $2}'`
echo -n 's' >> $CTLFILE
sleep .5
echo ${STATIONNUM} >> $CTLFILE
$DISPLAYMESSAGE "New Station:" "$STATIONNAME"
}
# Reset pianobar and alsa's volume levels
RESETAUDIO() {
# Reset pianobar's volume
echo -n '^' >> $CTLFILE
# Reset amixer volume
amixer sset 'PCM' 95% > /dev/null
$DISPLAYMESSAGE "Resetting Volume" ""
(sleep 2 && $PARSEANDWRITE2LCD) &
}
# Stop all daemons and poweroff
POWEROFF() {
$DISPLAYMESSAGE "Shutting down" "system"
sudo -iu \#1000 /usr/local/bin/pbstop
sudo poweroff
}
##################################################################
# Begin Main
##################################################################
# Determine if the button was a short or long press
shortlong=""
# Get the current time in seconds since Epoch
PREVIOUSSECONDS=`date +%s`
# While the pin is pulled low (you are pressing the button), do some stuff
while [ $(gpio -g read $TRIGGEREDPIN) = 0 ]
do
# Get the current time in seconds at the beginning of the loop
CURRENTSECONDS=`date +%s`
# If current time minus previous time through the loop is less then the interval then sleep
if (("$CURRENTSECONDS" - "$PREVIOUSSECONDS" <= "$INTERVAL"))
then
sleep .1 # Added the sleep for 1/10th of a second to reduce clock cycles spent monitoring this pin
# If the user has been holding the button for the interval (or longer) then it was a long press
else
shortlong="long"
break
fi
done
# Check if the user let go of the button before reaching the interval
# if the shortlong variable is empty, then set it to "short"
if [ -z "$shortlong" ]; then
shortlong="short"
fi
##################################################################
case "$TRIGGEREDPIN" in
4) # Redraw / Shutdown Pin Pressed
if [ $shortlong == "short" ]; then
# Redraw the "Now Playing Screen"
$PARSEANDWRITE2LCD
fi
if [ $shortlong == "long" ]; then
# Stop everything
POWEROFF
fi
;;
12) # Display Temperature / Next Song Button
if [ $shortlong == "short" ]; then
$SCRIPTSBASEDIR/TempHumidDisplay.sh
sleep 8
$PARSEANDWRITE2LCD
fi
if [ $shortlong == "long" ]; then
echo -n 'n' >> $CTLFILE
$DISPLAYMESSAGE "Skipping song" ""
fi
;;
22) # Love / Ban Song
if [ $shortlong == "short" ]; then
SONGACTION "+" "Loving song"
fi
if [ $shortlong == "long" ]; then
SONGACTION "-" "Banning song"
fi
;;
18) # Mute/Unmute toggle / Return to default volume level
if [ $shortlong == "short" ]; then
# Toggle the mute button and store the output in variable $mute
mute=`amixer set PCM toggle`
if [[ $mute =~ \[on\] ]]
then
($DISPLAYMESSAGE "Mute is" "Off"; sleep 1.5; $PARSEANDWRITE2LCD) &
fi
if [[ $mute =~ \[off\] ]]
then
($DISPLAYMESSAGE "Mute is" "On") &
fi
fi
if [ $shortlong == "long" ]; then
# Reset all audio to default levels
RESETAUDIO
fi
;;
23) # Play/Pause / Tired of Song
if [ $shortlong == "short" ]; then
SONGACTION "p" "Play/Pause"
# Indicate the button was pressed then redraw the Now Playing screen
(sleep 2; $PARSEANDWRITE2LCD) &
fi
if [ $shortlong == "long" ]; then
SONGACTION "t" "Ban for 1 month:"
fi
;;
24) # Not implemented yet: Menu/Select / Go back
if [ $shortlong == "short" ]; then
$DISPLAYMESSAGE "Menu" "Select"
fi
if [ $shortlong == "long" ]; then
$DISPLAYMESSAGE "Go" "Back"
fi
;;
100) # (BTN_0) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: ZZ Top
CHANGESTATION "zz top"
;;
101) # (BTN_1) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: Stevie Ray Vaughan
CHANGESTATION "Stevie Ray Vaughan"
;;
102) # (BTN_2) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: Jazz Radio
CHANGESTATION "Jazz Radio"
;;
103) # (BTN_3) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: Van Halen
CHANGESTATION "Van Halen"
;;
104) # (BTN_4) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: Louisiana Blues
CHANGESTATION "Louisiana Blues"
;;
105) # (BTN_5) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: John Coltrane
CHANGESTATION "John Coltrane"
;;
106) # (BTN_6) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: Soundgarden Radio
CHANGESTATION "Soundgarden Radio"
;;
107) # (BTN_7) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: Rock Guitar
CHANGESTATION "Rock Guitar"
;;
108) # (BTN_8) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: Miles Davis
CHANGESTATION "Miles Davis"
;;
109) # (BTN_9) Favorite Stations Shortcuts - Implemented for IR remote control
# Favorite station: Master Of Puppets Radio
CHANGESTATION "Master Of Puppets Radio"
;;
plus) # (BTN_PREVIOUSSONG) Love song
SONGACTION "+" "Love song:"
(sleep 3 && $PARSEANDWRITE2LCD) &
;;
n) # (BTN_NEXTSONG) Skip song
SONGACTION "n" "Skip song:"
;;
P) # (BTN_PLAYPAUSE) Play
SONGACTION "P" "Play:"
(sleep 3 && $PARSEANDWRITE2LCD) &
;;
S) # (BTN_PLAYPAUSE) Pause
SONGACTION "S" "Paused:"
;;
audio) # (BTN_AUDIO) Reset Volume
RESETAUDIO
;;
ban) # Bansong
SONGACTION "-" "Ban song:"
;;
t) # (KEY_FASTFORWARD) >> button
# Tired of song - pianobar to skip for 30 days
SONGACTION "t" "Ban for 1 month:"
;;
pbstart) # (PBC) + (SD/USB) button
# Startup vloradio
sudo -iu \#1000 /usr/local/bin/pbstart
;;
pbstop) # (PBC) + (Setup) button
# Stop vloradio
$DISPLAYMESSAGE "Stopping" "Pianobar"
sudo -iu \#1000 /usr/local/bin/pbstop
;;
poweroff) # (PBC) + (Power) button
POWEROFF
;;
INPUT) # (Input) button
# Display CPU and GPU Temps
# Get the GPU Temp
GPUTEMP=`vcgencmd measure_temp | cut -d"=" -f2 | cut -f1 -d"'"`
# Get the CPU Temp and format it
CPUTEMP=$(</sys/class/thermal/thermal_zone0/temp)
CPUTEMP=$(echo ${CPUTEMP::-3}.${CPUTEMP:2:1})
# Check for high temp
if [ `echo $GPUTEMP | cut -d"." -f1` -gt 45 ] || [ `echo $CPUTEMP | cut -d"." -f1` -gt 45 ]
then
$DISPLAYMESSAGE "Temp High!!!" "C:${CPUTEMP}C G:${GPUTEMP}C"
else
$DISPLAYMESSAGE "CPU Temp: ${CPUTEMP}C" "GPU Temp: ${GPUTEMP}C"
(sleep 6; $PARSEANDWRITE2LCD) &
fi
;;
esac