Vers 0.3: When no GPIO is given, green onboard LED led0 will be used. So
will run now without parameters. No more sudo necessary.
Aki
Code:
--------------------
##!/bin/sh
#
--------------------------------------------------------------------------------------------------------------------------
# pm2gpio script polls "pcp mode" every 500 ms (1 second on Pi Zero) and sets
a GPIO when squeezelite is playing.
# Aki 9/2020 - Home:
https://forums.slimdevices.com/showthread.php?110277-Feature-request-picoreplayer-Play-state-to-GPIO
#
--------------------------------------------------------------------------------------------------------------------------
# Vers 0.2: Cleanup of GPIO on exit. Reentrance cleared. -k kills running
instance. Clear reaction on 'pcp mode' outages.
# Vers 0.3: When no GPIO is given, green onboard LED led0 will be used. So
will now run without parameters. No more sudo necessary
BASE_GPIO_PATH=/sys/class/gpio
ScriptName=$(basename -- "$0")
function UpTm () { # Returns UpTime sss.ms
set `cat -- /proc/uptime`
echo "$1"
} # function UpTm
function mySudo () {
echo $@ | sudo sh
} # function mySudo
function msg () {
echo "$(UpTm) $ScriptName: $@"; #echo "$(date +"%T") $ScriptName: $@";
} # function msg
function err () {
echo "$(UpTm) $ScriptName: Error: $@" >&2; exit 1
} # function err, not called from loop -therefore no need to cleanup?
function cleanup () {
if [ $PlayGPIO = "0" ]; then # green onboard led0
mySudo "echo mmc0 >/sys/class/leds/led0/trigger"
else # GPIO
if [ -e $BASE_GPIO_PATH/gpio$PlayGPIO ]; then # GPIO initialized
mySudo "echo $((!$PlayVal)) > $BASE_GPIO_PATH/gpio$PlayGPIO/value" # Set to
off state
mySudo "echo $PlayGPIO > $BASE_GPIO_PATH/unexport"
fi # GPIO de-initialized
fi # led0 or GPIO
exit 0
} # on Exit
function Usage () {
echo ""
echo "PlayMode to GPIO ($ScriptName) sets a GPIO when squeezelite is playing."
echo "--------------------------------------------------------------------"
echo "Usage: $0 [-g GPIO] [-s n] [-f] [-v] [-b] [-k] [-h]"
echo ""
echo "-g : GPIO-number play mode, if not given, green onboard (led0) will be
used"
echo "-s : GPIO-value for play mode [0,1], if not given [1] will be used"
echo "-f : run in foreground with verbose output"
echo "-v : verbose output from background"
echo "-b : blink mode, GPIO is blinking when playing, on on pause"
echo "-k : Kill running $ScriptName"
echo "-h : Shows this help"
echo ""
echo "Examples:"
echo "---------"
echo " $0 : Green onboard LED (led0) on when playing"
echo " $0 -b : Green onboard LED (led0) blinking when playing, on
when paused"
echo " $0 -g27 : Sets GPIO27 to 3V when playing"
echo " $0 -g27 -b : Sets GPIO27 to 3V, blinking when playing, on when
pause"
echo " $0 -g4 -s0 -f : Sets GPIO4 to 0V when playing, running in
foreground with output (debug)"
echo " $0 -g4 -s1 -v : Sets GPIO4 to 3V when playing, running in
background with output (debug)"
echo " $0 -k : Kills running $ScriptName"
echo ""
exit 1
} # function Usage
function Loop () {
trap cleanup INT TERM # cleanup GPIO on exit
while true; do
MODE=`pcp mode 2> /dev/null`
if [ ! "$MODE" = "$OldMode" ]; then # MODE change
if [ ! "$MODE" ]; then MODE="not available"; fi # no result, wget error
msg "pcp mode is \"$MODE\"."
if [ "$MODE" = "play" ] || [ "$MODE" = "pause" ] || [ "$MODE" = "stop" ] || [
"$MODE" = "not available" ]; then # valid or empty MODE
if [ "$MODE" = "play" ]; then
GPIOval=$PlayVal; # on
elif $blink && [ "$MODE" = "pause" ]; then
GPIOval=$PlayVal # on
else
GPIOval=$((!$PlayVal)) # off
fi
else # unknown MODE
msg "\"pcp mode\" results to unknown mode \"$MODE\", should be play, pause,
stop"
fi # valid/invalid MODE
OldMode=$MODE
fi # MODE change
# blink
if $blink && [ "$MODE" = "play" ]; then GPIOval="$((!$GPIOval))"; fi #
invert, blinking
if [ ! "$GPIOval" = "$oldGPIOval" ]; then
if ! $blink; then msg "Set GPIO$PlayGPIO to $GPIOval."; fi
if [ $PlayGPIO = "0" ]; then # led0
if [ $GPIOval = "1" ]; then
mySudo "echo 1 >/sys/class/leds/led0/brightness"
else
mySudo "echo 0 >/sys/class/leds/led0/brightness"
fi
else # GPIO
mySudo "echo $GPIOval > $BASE_GPIO_PATH/gpio$PlayGPIO/value"
fi
oldGPIOval=$GPIOval
fi # GPIO val change
sleep 0.5
done # while true
} # function Loop
############
### Main ###
############
# Parameter
# ---------
#if [ ! $1 ]; then Usage; fi # No Param, please give at least the GPIO
# Defaults
PlayGPIO=0 # default green onboard led0
PlayVal=1 # default active high
foreground=false # no more need to foreground
verbose=false # no output from loop when daemonized
blink=false # stay active
oldGPIOval=0
while getopts g:s:fvbkh opt
do
case $opt in
g ) # Check for valid Play GPIO
if [ "$OPTARG" -ge 2 ] && [ "$OPTARG" -le 27 ]
then PlayGPIO=$OPTARG
else err "Play GPIO \"$OPTARG\" is not a valid GPIO, please use 2 .. 27"; fi
# Check for valid GPIO
;;
s ) # Check parameter for setting GPIO to 0 or 1 when playing
if [ $OPTARG ]; then # Set GPIO to 0 or 1 when playing
if [ "$OPTARG" -eq 0 ] || [ "$OPTARG" -eq 1 ]
then PlayVal=$OPTARG
else err "Play-value \"$OPTARG\" is not a valid GPIO state, please use 0 or
1"; fi
fi # Check parameter for setting GPIO to 0 or 1 when playing
;;
f ) foreground=true;;
v ) verbose=true;;
b ) blink=true;;
k ) msg "Killing running instance."; sudo killall -15 $ScriptName;;
h ) Usage;;
[?]) Usage;;
esac
done
# Kill old instance, if exists
for pid in $(pidof $ScriptName); do
if [ ! $pid = $$ ]; then
msg "Killing old instance (pid $pid) of $ScriptName"
mySudo "kill -15 $pid"
sleep 0.3 # Give the exit routine some time. on pi 3b+ 0.1s is enough
fi # old instance found
done # Kill old instance
# Init
# ----
# Wait for squeezelite to come online
wt=0
until pids=$(pidof squeezelite squeezelite-dsd); do
sleep 1; wt=$(($wt+1))
msg "Waited $wt seconds for for squeezelite ..."
if [ "$wt" -ge 60 ]; then err "Waited too long for squeezelite"; fi
done
# Init GPIO
if [ $PlayGPIO = "0" ]; then # led0
msg "Disable mmc0 trigger on led0"
mySudo "echo none >/sys/class/leds/led0/trigger"
else # GPIO
if [ ! -e $BASE_GPIO_PATH/gpio$PlayGPIO ]; then # Init GPIO
msg "Export GPIO$PlayGPIO to userspace"
mySudo "echo $PlayGPIO > $BASE_GPIO_PATH/export"
fi # Init GPIO
msg "Set GPIO$PlayGPIO as an output"
mySudo "echo out > $BASE_GPIO_PATH/gpio$PlayGPIO/direction"
fi # led0 or GPIO
# Call Loop
# ----------
if $foreground; then # foreground:
msg "Foreground loop polling, with output. Stop with \"ctrl-c\"."
Loop
else # daemonize
if $verbose; then # Background loop polling with output
msg "Background loop polling with output. Stop with \"$0 -k\" or restart of
script."
Loop &
else # Background loop polling
msg "Background loop polling. Stop with \"sudo $0 -k\" or restart of script."
Loop > /dev/null 2>&1 &
fi # Background loop polling
fi # Start fore- or background loop polling
exit
# End of Program
--------------------
------------------------------------------------------------------------
Aki7's Profile: http://forums.slimdevices.com/member.php?userid=67596
View this thread: http://forums.slimdevices.com/showthread.php?t=110277
_______________________________________________
unix mailing list
[email protected]
http://lists.slimdevices.com/mailman/listinfo/unix