Lot of confusion :-) 

I offered the code, when there is any interest. You are interested, so
here is the code with the lmsmonitor handling:


Code:
--------------------
    #!/bin/sh
  # 
--------------------------------------------------------------------------------------------------------------------------
  # 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.
  # Vers 0.4: Cosmetics.
  # Vers 0.5: LMSmonitor integration. Tested on pi 3b w. lmsmonitor (0.4.65) - 
built Dec 27 2020 19:33:28
  
  BASE_GPIO_PATH=/sys/class/gpio
  BASE_LED0_PATH=/sys/class/leds/led0
  ScriptName=$(basename -- "$0")
  
  function UpTm () { # Returns UpTime sss.ms
  set `cat -- /proc/uptime`
  echo "$1"
  } # function UpTm
  
  function mySudo () { # Aufruf aus functions oder wg. background?
  echo $@ | sudo sh
  } # function mySudo
  
  function msg () {
  if [ $foreground ] || [ $verbose ]; then
  echo "$(UpTm) $ScriptName: $@"; #echo "$(date +"%T") $ScriptName: $@";
  fi # foreground
  } # function msg
  
  function err () {
  echo "$(UpTm) $ScriptName: Error: $1" >&2; exit 1 
  } # function err, not called from loop -therefore no need to cleanup?
  
  function KillMonitor () {
  while pid=$(pidof lmsmonitor); do     
  msg "LMSmonitor stopping pid $pid"  
  sudo killall lmsmonitor #" #> /dev/null 2>&1"    
  sleep 1       
  msg "LMSmonitor stopped pid $pid"     
  done # Wait for end of kill
  oldMonitorState=0  
  } # function KillMonitor
  
  function StartMonitor () { 
  if pid=$(pidof lmsmonitor); then    
  KillMonitor      
  fi
  msg "LMSmonitor starting"             
  sudo /mnt/mmcblk0p2/tce/lmsmonitor -n $PNAME -c -z -f7 & #> /dev/null 2>&1 & 
# -z
  sleep 3        
  msg "LMSmonitor started, pid $(pidof lmsmonitor)"
  #msg "$(pidof lmsmonitor)"
  } # function StartMonitor
  
  function cleanup () { 
  if [ $PlayGPIO = "0" ]; then # green onboard led0
  mySudo "echo mmc0 >$BASE_LED0_PATH/trigger" 
  else 
  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
  if $monitor; then    
  KillMonitor
  fi
  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 "-m : lmsmonitor, handles lmsmonitor"
  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
  
  # GPIO
  if [ "$MODE" = "play" ]; then 
  GPIOval=$PlayVal; # on
  elif $blink && [ "$MODE" = "pause" ]; then 
  GPIOval=$PlayVal # on
  else
  GPIOval=$((!$PlayVal)) # off
  fi # Set static GPIOval
  
  # LMSmonitor
  if $monitor; then
  if [ "$MODE" = "play" ] || [ "$MODE" = "pause" ]; then
  MonitorState=1
  msg "MonitorState=1"
  else  
  MonitorState=0
  msg "MonitorState=0"
  fi
  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 on play when -b
  if $blink && [ "$MODE" = "play" ]; then GPIOval="$((!$GPIOval))"; fi # 
invert, blinking                       
  
  if [ ! "$GPIOval" = "$oldGPIOval" ]; then # GPIO val change
  if ! $blink; then msg "Set GPIO$PlayGPIO to $GPIOval."; fi # Debug output 
only when no GPIO change for blinking
  if [ $PlayGPIO = "0" ]; then # led0
  mySudo "echo $GPIOval > $BASE_LED0_PATH/brightness"
  else # GPIO
  mySudo "echo $GPIOval > $BASE_GPIO_PATH/gpio$PlayGPIO/value"
  fi
  oldGPIOval=$GPIOval
  fi # GPIO val change 
  
  if [ ! "$MonitorState" = "$oldMonitorState" ]; then # Change Monitorstate, 
don't ask!
  if [ "$MonitorState" = "1" ]; then   
  StartMonitor 
  if [ "$oldMonitorState" = "0" ]; then 
  StartMonitor
  StartMonitor
  fi # Kommt aus vorher gestopptem off-Status, braucht dann Nachhilfe
  else
  StartMonitor
  KillMonitor
  fi # So klappt es     
  oldMonitorState=$MonitorState
  fi # Change Monitorstate
  sleep 0.5 # Loop interval
  done # while true
  } # function Loop
  
  ############
  ### Main ###
  ############
  
  # Parameter
  # ---------
  # 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
  monitor=false # LMSmonitor nicht starten
  oldGPIOval=0
  oldMonitorState=-1 # LMSmonitor: 0=off 1=running -1=unknown/after start
  
  while getopts g:s:fvbmkh 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;;
  m ) monitor=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 >$BASE_LED0_PATH/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
  
  # This player Name (for LMSmonitor) 
  if $monitor; then
  PNAME=`cat /usr/local/sbin/config.cfg | grep "^NAME=" | cut -d'"' -f2` 
  fi
  
  # 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
--------------------


The lmsmontor part is a little crazy, slow and fragil, but found no
other way. Beta but working. Don't do power on - off too fast :-)


------------------------------------------------------------------------
Aki7's Profile: http://forums.slimdevices.com/member.php?userid=67596
View this thread: http://forums.slimdevices.com/showthread.php?t=111790

_______________________________________________
unix mailing list
unix@lists.slimdevices.com
http://lists.slimdevices.com/mailman/listinfo/unix

Reply via email to