01-23-2019, 10:54 AM
I've borrrowed / adapted a bash script I found on the internet to control a relay based on the contents of the /proc/asound/card0/pcm0p/sub0/status file.
It sits and checks that file every n seconds, and if 'RUNNING' is found, changes the state of y GPIO.
If 'RUNNING' is then not found it waits x seconds before changing the state of the y GPIO back again.
I run this as a service and it works well with mpd / shairport etc. However, it would be great to incorporate this in worker.php or somewhere more suitable.
The feature could be generalised - so if 'RUNNING' is found run 'up.sh' / not found run 'down.sh' - just like network configuration, as I'm sure there are other things people would like to run if they hit play.
The variables could be:
Check interval (seconds): n
State change delay (seconds): x
Set-up script (optional): allows initialisation of a specified GPIO pin on boot
Play script: user set field like the LCD update script
Stop script: user set field like the LCD update script
And in these two files the user could implement anything they want.
Or it could be made 'amplifier relay specific' as someone on the forum has done - with a dedicated page for amplifier control.
It sits and checks that file every n seconds, and if 'RUNNING' is found, changes the state of y GPIO.
If 'RUNNING' is then not found it waits x seconds before changing the state of the y GPIO back again.
I run this as a service and it works well with mpd / shairport etc. However, it would be great to incorporate this in worker.php or somewhere more suitable.
The feature could be generalised - so if 'RUNNING' is found run 'up.sh' / not found run 'down.sh' - just like network configuration, as I'm sure there are other things people would like to run if they hit play.
The variables could be:
Check interval (seconds): n
State change delay (seconds): x
Set-up script (optional): allows initialisation of a specified GPIO pin on boot
Play script: user set field like the LCD update script
Stop script: user set field like the LCD update script
And in these two files the user could implement anything they want.
Or it could be made 'amplifier relay specific' as someone on the forum has done - with a dedicated page for amplifier control.
Code:
#!/bin/bash
set -e
# Exports pin to userspace
echo "23" > /sys/class/gpio/export
# Sets pin 23 as an output
echo "out" > /sys/class/gpio/gpio23/direction
#time to wait until the amplifier is turned off in seconds
off_timer_time=60
#set variables
last_is_playing=0
off_timer=0
while true
do
if cat /proc/asound/card0/pcm0p/sub0/status | grep -i RUNNING > /dev/null
then
is_playing=1
else
is_playing=0
fi
#0->1
if [[ "$last_is_playing" -eq "0" && "$is_playing" -eq "1" ]]
then
off_timer=0
echo "Turning amplifier on"
echo "1" > /sys/class/gpio/gpio23/value
fi
#1->0
if [[ "$last_is_playing" -eq "1" && "$is_playing" -eq "0" ]]
then
off_timer=1
fi
if [ "$off_timer" -ne "0" ]
then
if [ "$off_timer" -eq "$off_timer_time" ]
then
off_timer=0
echo "Turning amplifier off"
echo "0" > /sys/class/gpio/gpio23/value
else
((off_timer++))
#echo $off_timer
fi
fi
last_is_playing=$is_playing
sleep 1
done
Code:
[Unit]
Description=Automatic Amplifier Control
[Service]
Type=simple
ExecStart=/home/pi/autoamp.sh
User=root
[Install]
WantedBy=multi-user.target
#systemctl enable autoamp.service
#systemctl daemon-reload
#systemctl start autoamp.service