#!/usr/bin/python
# ========================================================
# Python script for controlling a push button switch
# Version 1.2 - by Thomas Schoch - www.retas.de
# ========================================================
from time import sleep
from os import system
from sys import exit
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# use button at GPIO 7 (P1 header pin 26)
gpio = 7
# state of some application, starts with "0"
state = 0
# main loop
while True:
# setup GPIO "gpio" as input with pull-up
GPIO.setup(gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# waiting for interrupt from button press
GPIO.wait_for_edge(gpio, GPIO.FALLING)
# waiting for button release
sec = 0
while (GPIO.input(gpio) == GPIO.LOW):
# delay for debouncing
sleep(0.2)
sec += 0.2
# pressed longer than 2 seconds? Shutdown!
if (sec > 2):
GPIO.cleanup()
system("/home/pi/io/led0 50 50")
system("/sbin/shutdown -h now")
exit(0)
# button released: toggle state
if (state == 0):
state = 1
system("/home/pi/io/led0 1900 100")
# switch application on here ...
else:
state = 0
system("/home/pi/io/led0 100 1900")
# switch application off here ...
# reset interrupt
GPIO.cleanup()