Ok, my first (b)ash script, actually my first work on linux :) For
newbees like me (working with Windows normaly):
- use MobaXTerm to ssh to your piCorePlayer, create a new file in
/home/tc/ps2gpio.sh
- double click ps2gpio.sh in the MobaXTerm explorer view to edit the
file
- copy and paste the script below, save and accept request with "all"
- In terminal Window - provided you will use GPIO4 set to 3V when
squeezelite ist playing:
chmod +xX ps2gpio.sh
sudo ./ps2gpio.sh -g 4
If you think thats fine, you can put it to Autostart:
- Web browser to your picoreplayer
- Tweaks - User commands:
sudo /home/tc/ps2gpio.sh -g 4 -d
Sorry for the noisy explenations. Here is the script:
------------------------------------------------------------------------------------------
Code:
--------------------
#!/bin/sh
BASE_GPIO_PATH=/sys/class/gpio
function Usage () {
echo ""
echo "PlayState to GPIO (ps2gpio) sets a GPIO when squeezelite is playing."
echo "--------------------------------------------------------------------"
echo "Usage: sudo $0 -g GPIO [-s n] [-d] [-h] [-v]"
echo ""
echo "-g : GPIO-number"
echo "-s : Set value for \"play\" state: 0,1. Default=1 sets GPIO to 3V when
playing"
echo "-d : Daemonize: Start running in backgrund and exit"
echo "-v : Verbose output when daemonized"
echo "-h : Shows this help"
echo ""
echo "Examples:"
echo "---------"
echo " sudo $0 -g 27 : Sets GPIO27 to 3V when playing, stop
running with Ctrl-c"
echo " sudo $0 -g 4 -s 0 -d : Sets GPIO4 to 0V when playing, running in
background"
echo " sudo $0 -g 4 -s 1 -d -v : Sets GPIO4 to 3V when playing, running in
background with status output"
echo ""
exit 1
} # function Usage
function err () {
echo "$1" >&2; Usage
} # function err
# Parameter
# Defaults
Daemonize=false
PlayVal=1
Verbose=false
if [ ! $1 ]; then Usage; fi
while getopts g:s:dhv opt
do
case $opt in
g ) # Check for valid GPIO
if [ "$OPTARG" -ge 2 ] && [ "$OPTARG" -le 27 ]; then
myGPIO=$OPTARG
else
err "\"$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 "\"$OPTARG\" is not a valid GPIO state, please use 0 or 1"
fi
else # no parameter
PlayVal=1 # default
fi # Check parameter for setting GPIO to 0 or 1 when playing
;;
d ) Daemonize=true ;;
v ) Verbose=true;;
h ) Usage;;
esac
done
if [ ! $myGPIO ]; then err "Mandatory parameter -g \"GPIO\" is missing"; fi
echo "$0 -g $myGPIO -s $PlayVal -d $Daemonize -v $Verbose"
# Init
if [ ! -e $BASE_GPIO_PATH/gpio$myGPIO ]; then # Init GPIO
echo "Export GPIO $myGPIO to userspace"
echo "$myGPIO" > $BASE_GPIO_PATH/export
fi # Init GPIO
echo Set GPIO $myGPIO as an output
echo "out" > $BASE_GPIO_PATH/gpio$myGPIO/direction
# wait for squeeze to come online
wt=0
until pids=$(pidof squeezelite squeezelite-dsd)
do
sleep 1; wt=$(($wt+1))
echo "Waited $wt seconds for for squeezelite ..."
if [ "$wt" -ge 60 ]; then err "Waited too long for squeezelite"; fi
done
# read actual GPIO value - gives 0 ?
# oldGPIOval=`cat /sys/class/gpio/gpio$myGPIO/value`
# echo "oldGPIOval is $oldGPIOval"
function Loop () {
while true
do
STATE=`(pcp mode)`
if [ ! "$STATE" = "$OldState" ]; then # STATE change
echo pcp State is $STATE
if [ "$STATE" = "play" ]; then GPIOval=$PlayVal; else GPIOval=$((!$PlayVal));
fi # Set GPIO value
if [ ! "$GPIOval" = "$oldGPIOval" ]; then
echo Set GPIO $myGPIO to $GPIOval
echo "$GPIOval" > $BASE_GPIO_PATH/gpio$myGPIO/value
oldGPIOval=$GPIOval
fi # GPIO val change
OldState=$STATE
fi # STATE change
sleep 0.5
done # while true
} # function Loop
if $Daemonize; then # daemonize
if $Verbose; then
echo "Daemonizing with output."
Loop &
else
echo "Daemonizing."
Loop > /dev/null 2>&1 &
fi
else # run in foreground:
Loop
fi
exit 0
--------------------
------------------------------------------------------------------------------------------
Not very well tested (On a fresh pcp 6.1 ok). Should work nevertheless.
have fun
Aki
------------------------------------------------------------------------
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