Greg Erskine wrote: 
> Hi kolossos4730, I was hoping you would ask at Pink Fish Media, they are
> more DIY than here. ;)
> 
> Good start, but I think you need to go back to the original script and
> simplify it not add FOLLOW_PLAYBACK_STATE.
> 
> The GPIO would follow the status -> mode if you set the DELAYOFF=0 (or
> maybe 1), or you could remove the delay code. With the original script
> as it was actually turning the amp on and off you didn't want the power
> be cut immediately.
> 
> Scripts like this need to run with the "while" loop forever. You should
> not add code that allows the script to exit, otherwise it will not
> monitor squeezelite's status anymore.
> 
> Issues that you need to account for, active high or active low, initial
> state of GPIO, does the amp expect a pulse to change the mute state or
> does the signal need to be held high or low.
> 
> I would start with something like this. (UNTESTED)
> 
> > 
Code:
--------------------
  >   > get_mode() {
  >     RESULT=`( echo "$MAC_ADDR $COMMAND"; echo exit ) | nc $LMS_IP 9090`
  >     echo $RESULT | grep "mode%3Aplay" > /dev/null 2>&1
  >     if [ $? == 0 ]; then
  >             echo "Playing."
  >             echo "1" > /sys/class/gpio/gpio$GPIO/value
  >     else
  >             echo "Stopped."
  >             echo "0" > /sys/class/gpio/gpio$GPIO/value
  >     fi
  > }
--------------------
> > 
> 
> regards
> Greg

Greg,

Thanks for your reply. This was a quick hack and my first attempt at
shell scripting. As for the DIY part. there is no soldering iron
involved, the Pi-AMP is just plug and play :D

I made a second version and this time tested it _with_ speakers
attached. As the signal change is pulse based there is no need to hold
the signal. This new version checks the state of GPIO 22 and only change
it when needed.

I use this script together with the new Squeezelite 1.8 alsamixer volume
control option (-V). With this option Squeezelite is now able to control
the hardware volume of the Pi-DAC.

If someone wants to test this, plaese be careful with the volume!
Everything below alsamixer PCM value 60-65 will probably be OK.


Code:
--------------------
    #!/bin/sh
  
  # Version: 0.01 2014-11-28 GE
  #               2015-02-21 Adapted for Pi-AMP
  #
  #===========================================================================
  # This script polls LMS at a regular interval to determine the current
  # status of the Squeezelite process on piCorePlayer. i.e. play or stop.
  #
  # GPIO 22 is then set/reset depending on the result of this status check.
  # This will unmute/mute the Pi-AMP accordingly.
  #---------------------------------------------------------------------------
  
  #===========================================================================
  # Possible issues
  #---------------------------------------------------------------------------
  # 1. If setting MAC address manually, pay close attention to the physical,
  #    wireless and fake MAC addresses.
  # 2. This script hasn't been tested on synced players.
  #---------------------------------------------------------------------------
  
  #===========================================================================
  # Save script to /home/tc/piamp.sh
  # Make script executable
  #
  ##    chmod +x /home/tc/piamp.sh
  #
  # Edit /opt/bootlocal.sh and add this script
  #
  ##    #!/bin/sh
  ##    #put other system startup commands here
  ##    #amixer cset numid=3 2
  ##
  ##    #run Pi-AMP script
  ##    sudo /home/tc/piamp.sh > /dev/null &
  ##
  ##    #mnt/mmcblk0p2/tce/OliWeb-master/ivySox/myweb/do_rebootstuff.sh
  ##    /home/tc/www/cgi-bin/do_rebootstuff.sh
  #
  # Save changes to piCorePlayer
  #
  ##    sudo filetool.sh -b
  #---------------------------------------------------------------------------
  
  #===========================================================================
  # Set the following according to your setup
  #---------------------------------------------------------------------------
  GPIO=22                                       # GPIO 22 controls Pi-AMP mute 
state
  PI_MAC_ADDRESS=b8:27:eb:1c:63:39      # Raspberry Pi MAC address
  LMS_IP_ADDRESS=192.168.0.220          # LMS IP address
  LMS_CLI_PORT_NUMBER=9090              # LMS CLI port number
  COMMAND="status 0 0"                  # LMS player status command
  INTERVAL=0.5                          # Set Poll interval
  #---------------------------------------------------------------------------
  
  get_mode() {
        GPIO_VALUE=`cat /sys/class/gpio/gpio$GPIO/value`
        RESULT=`( echo "$PI_MAC_ADDRESS $COMMAND"; echo exit ) | nc 
$LMS_IP_ADDRESS $LMS_CLI_PORT_NUMBER`
        echo $RESULT | grep "mode%3Aplay" > /dev/null 2>&1
        if [ $? == 0 ]; then
                # playing
                if [ $GPIO_VALUE == 0 ]; then
                        echo "Unmute!"
                        # unmute Pi-AMP
                        echo "1" > /sys/class/gpio/gpio$GPIO/value
                fi
        else
                # stopped
                if [ $GPIO_VALUE != 0 ]; then
                        echo "Mute!"
                        # mute Pi-AMP
                        echo "0" > /sys/class/gpio/gpio$GPIO/value
                fi
        fi
  }
  
  #===========================================================================
  # Initial GPIO setup
  #
  # Pi-AMP starts up in a mute state.
  # echo "1" > /sys/class/jpio/gpio22/value unmutes Pi-MAP
  # echo "0" > /sys/class/jpio/gpio22/value mutes Pi-MAP
  #---------------------------------------------------------------------------
  # create a user interface for pin GPIO
  sudo sh -c 'echo '"$GPIO"' > /sys/class/gpio/export'
  # configure GPIO as output with a starting value of 0
  sudo sh -c 'echo "low" > /sys/class/gpio/gpio'"$GPIO"'/direction'
  # configure GPIO for active low logic
  sudo sh -c 'echo 0 > /sys/class/gpio/gpio'"$GPIO"'/active_low'
  #---------------------------------------------------------------------------
  
  #===========================================================================
  # main()
  #---------------------------------------------------------------------------
  # Loop forever. This uses less then 1% CPU, so it should be OK.
  while true
  do
        get_mode
        sleep $INTERVAL
  done
  #---------------------------------------------------------------------------
--------------------



Regards,
Arie


------------------------------------------------------------------------
kolossos4730's Profile: http://forums.slimdevices.com/member.php?userid=63988
View this thread: http://forums.slimdevices.com/showthread.php?t=97803

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

Reply via email to