one push switch for shutdown and play/pause toggle

Hi,

I installed a push switch (momentary on) and an LED on the GPIO of my raspberry pi, and implemented two functions to the switch: shutdown and play/pause toggle.

When you press the switch over 5 s, the system shutdowns. An LED turns on informing you that the system enters the shutdown process and you can release the switch.
When you press for a moment (1 s or so), mpd starts/pauses the music – toggle.

Install a push switch between Pins 15 and 17 of the GPIO port.
Install an LED between Pins 20 and 22. Pin 20 is for the cathode (K). No register is required (but a 50 - 200 ohm register in series is prefered :slight_smile: )

The script may be named as you like, and the permission is 755. Run it as daemon by adding the following line at the end of /etc/rc.local (just before ‘exit 0’ line).
When the script name is ‘daemon.sh’ and it is located at /home/volumio,

su -c /home/volumio/daemon.sh &

Watch the video:
https://www.youtube.com/watch?v=bFi11qZX6fM

The script works well – but your suggestion for further improvement is welcome.
Thanks.

yjo

# Push switch daemon for shutting-down and play/pause toggle
# Copyright (C)2015 by yjo
#
# Run as "su -c daemon.sh &" at the end of /etc/rc.local 
# (before "exit 0" line)

# short-push : play/pause toggle
# long-push till LED on :  shutdown

# push switch : between pins #15 & #17
# LED : between pins #20(K) & #22(A)

#! /bin/sh
# constants
GPIO_SW=22          # GPIO port for SW
GPIO_LED=25         # GPIO port for LED
SLEEP_INTERVAL=0.2  # interval for sleep command
PUSHTIME_SD=5       # push-and-hold time for shutdown
PUSHTIME_PP=1       # push-and-hold time for play/pause toggle

# initialization of GPIO
# port 22 for input w/ pull down
echo "$GPIO_SW" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$GPIO_SW/direction
echo "low" > /sys/class/gpio/gpio$GPIO_SW/direction   # pull-down
# port 25 for output
echo "$GPIO_LED" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$GPIO_LED/direction

# main loop
cnt=0
while :
do

  data=`cat /sys/class/gpio/gpio$GPIO_SW/value`

  if [ "$data" -eq "1" ] ; then
    cnt=`expr $cnt + 1`
  else
    cnt=0
  fi

  if [ $cnt -gt $PUSHTIME_PP ] ; then
     sleep $SLEEP_INTERVAL
     sleep $SLEEP_INTERVAL
     data=`cat /sys/class/gpio/gpio$GPIO_SW/value`
     if [ "$data" -eq "0" ] ; then
       mpc toggle
     fi
  fi

  if [ $cnt -gt $PUSHTIME_SD ] ; then
    break
  fi

  sleep $SLEEP_INTERVAL

done

# turning on GPIO for LED (25)
echo 1 > /sys/class/gpio/gpio$GPIO_LED/value

# executing shutdown process
shutdown -h now
1 Like

This is excellent and I may implement it myself. I hope you’re still around to discuss it further…

How about a multicolour LED to show the different states?

For example:

Green - Ready to play
Red - Shutting down/Booting up
Blue - Ready to power off

Or variations on that theme.

Is this possible? We already have the start-up sound, perhaps the Green LED could be synchronised with that?

Some info about RGB LEDs and the Raspberry Pi: henryleach.com/2013/05/contr … ry-pi.html