#!/usr/bin/python

# Import the modules to send commands to the system and access GPIO pins
import RPi.GPIO as gpio
import syslog
import os
import sys
import signal
from time import sleep

def cleanup(signum, frame):
   syslog.syslog('pishutdown: Caught a signal, shutting down')
   gpio.cleanup()
   sys.exit(1)

signal.signal(signal.SIGINT, cleanup)

#Set pin numbering to board numbering
gpio.setmode(gpio.BOARD)

gpio.setup(38, gpio.OUT)
gpio.output(38, gpio.HIGH)
# Enable pull-down for safety
gpio.setup(40, gpio.IN, pull_up_down=gpio.PUD_DOWN)

while True:
  try:
    gpio.wait_for_edge(40, gpio.RISING)
    # Debounce
    for i in range(10):
	sleep(0.010)
	if not gpio.input(40): break
    if i == 9:
        # Shutdown && log
	syslog.syslog('pishutdown: Soft shutdown')
	gpio.cleanup()
    	os.system('shutdown now -h')
    	sys.exit(0)
  except KeyboardInterrupt:  
    print 'Keyboard interrupt'
    gpio.cleanup()
    sys.exit(1)    
  except Exception as e:
    syslog.syslog('pishutdown: Exception: ' + str(e))


@@ -190,128 +190,780 @@
 def 
-cleanup(signum, frame):%0A   syslog.syslog('pishutdown: Caught a signal, shutting down')%0A   gpio.cleanup()%0A   sys.exit(1)%0A
+shutdown():%0A    # Shutdown && log%0A    syslog.syslog('pishutdown: Soft shutdown')%0A    gpio.cleanup()%0A    os.system('shutdown now -h')%0A    sys.exit(0)%0A%0Adef reboot():%0A    # Reboot && log%0A    syslog.syslog('pishutdown: Soft reboot')%0A    gpio.cleanup()%0A    os.system('reboot')%0A    sys.exit(0)%0A%0Adef cleanup(signum, frame):%0A   syslog.syslog('pishutdown: Caught a signal, shutting down')%0A   gpio.cleanup()%0A   sys.exit(1)%0A%0Adef wait_for(value, timeout=-1):%0A  while True:%0A    channel = gpio.wait_for_edge(40, gpio.RISING if value else gpio.FALLING, timeout=timeout)%0A    if channel == None: return None%0A    # Debounce%0A    for i in range(10):%0A      sleep(0.001)%0A      curr_value = gpio.input(40) == gpio.HIGH%0A      if curr_value != value: break%0A    if i == 9: return channel%0A      %0A   
 %0Asig
@@ -1200,23 +1200,8 @@
 WN)%0A
-%0Awhile True:%0A  
 try:
@@ -1205,29 +1205,24 @@
 ry:%0A    
-gpio.
 wait_for
 _edge(40
@@ -1217,268 +1217,125 @@
 _for
-_edge(40, gpio.RISING)%0A    # Debounce%0A    for i in range(10):%0A%09sleep(0.010)%0A%09if not gpio.input(40): break%0A    if i == 9:%0A        # Shutdown && log%0A%09syslog.syslog('pishutdown: Soft shutdown')%0A%09gpio.cleanup()%0A    %09os.system('shutdown now -h')%0A    %09sys.exit(0)%0A  
+(True)%0A    if wait_for(False, 500) != None:%0A       if wait_for(True, 500) != None:%0A          reboot()%0A    shutdown()%0A
 exce
@@ -1358,18 +1358,16 @@
 rupt:  %0A
-  
   print 
@@ -1389,18 +1389,16 @@
 rupt'%0A  
- 
- 
 gpio.cle
@@ -1402,26 +1402,24 @@
 cleanup()%0A  
- 
- 
 sys.exit(1) 
@@ -1422,18 +1422,16 @@
 (1)    %0A
-  
 except E
@@ -1445,18 +1445,16 @@
 n as e:%0A
-  
   syslog

Support double-click to reboot