File [scripts/magic_button.sh]

1  #!/bin/bash
2  # Define the path and any password options for MPD
3  MY_MPC="/usr/bin/mpc -h 192.168.100.1"
4  # This is the GPIO pin connected to the magic button
5  MagicButton=4
6  # Set the polarity of the switch:
7  # depends if switch is normally open or closed
8  ASSERT=0
9  NEGATE=1
10  echo "$MagicButton" > /sys/class/gpio/export
11  echo "in" > /sys/class/gpio/gpio$MagicButton/direction
12  old=$NEGATE
13  start=0
14  end=0
15  diff=0
16  while [ TRUE ]; do
17    button=$(cat /sys/class/gpio/gpio$MagicButton/value)
18    uptime=$(</proc/uptime)
19    uptime=${uptime%% *}
20    uptime=${uptime/.}
21    #echo "MagicButton: ${button} ${old} ${uptime} ${start} ${end} ${diff}"
22    # Check for button state
23    if [ ${button} -eq $ASSERT ] &;&; [ ${old} -eq $NEGATE ]; then
24      # Button pressed
25      old=$ASSERT
26      start=${uptime}
27    fi
28    if [ ${button} -eq $NEGATE ] &;&; [ ${old} -eq $ASSERT ]; then
29      # Button released
30      old=$NEGATE
31      end=${uptime}
32      diff=`expr ${end} - ${start}`
33      # Now act on the length of button press
34      if [ ${diff} -le 35 ]; then
35        # Skip track
36        echo "MagicButton: Skipping track"
37        $MY_MPC -q next
38      fi
39      if [ ${diff} -gt 35 ] &;&; [ ${diff} -le 200 ]; then
40        # Restart track
41        echo "MagicButton: Restarting track"
42        $MY_MPC -q seek 00:00:00
43      fi
44      if [ ${diff} -ge 500 ]; then
45        # Reboot
46        echo "MagicButton: Rebooting server"
47        reboot
48      fi
49    fi
50    sleep 0.1
51  done



Del.ico.us Digg Facebook Google LinkedIn LiveJournal NewsVine reddit StumbleUpon Twitter
Valid XHTML 1.0 Transitional Valid CSS! [Valid Atom 1.0] [Valid RSS 2.0]
[ Page last updated Fri 5th Apr 2024 | viewed 23741 times ]