# -*- coding: utf-8 -*-
# -*- coding: Latin-1 -*-

http://genelaix.free.fr/spip.php?article60



import time
import RPi.GPIO as GPIO


# handle the button event
def buttonEventHandler (pin):
    print "handling button event"

    # turn the green LED on
    GPIO.output(25,True)

    time.sleep(1)

    # turn the green LED off
    GPIO.output(25,False)



# main function
def main():

    # tell the GPIO module that we want to use 
    # the chip's pin numbering scheme
    GPIO.setmode(GPIO.BCM)

    # setup pin 23 as an input
    # and set up pins 24 and 25 as outputs
    GPIO.setup(23,GPIO.IN)
    GPIO.setup(24,GPIO.OUT)
    GPIO.setup(25,GPIO.OUT)

    # tell the GPIO library to look out for an 
    # event on pin 23 and deal with it by calling 
    # the buttonEventHandler function
    GPIO.add_event_detect(23,GPIO.FALLING)
    GPIO.add_event_callback(23,buttonEventHandler,100)

    # turn off both LEDs
    GPIO.output(25,False)
    GPIO.output(24,True)

    # make the red LED flash
    while True:
        GPIO.output(24,True)
        time.sleep(1)
        GPIO.output(24,False)
        time.sleep(1)


    GPIO.cleanup()



if __name__=="__main__":
    main()
	
	
	
	/*******************************
	
	
	
	import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(25, GPIO.OUT, initial=GPIO.LOW)
GPIO.add_event_detect(4, GPIO.BOTH)
def my_callback():
    GPIO.output(25, GPIO.input(4))
GPIO.add_event_callback(4, my_callback)


/*********************************


    #!/usr/bin/env python2.7  
    # script by Alex Eames http://RasPi.tv  
    # http://RasPi.tv/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3  
    import RPi.GPIO as GPIO  
    GPIO.setmode(GPIO.BCM)  
      
    # GPIO 23 & 17 set up as inputs, pulled up to avoid false detection.  
    # Both ports are wired to connect to GND on button press.  
    # So we'll be setting up falling edge detection for both  
    GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
    GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
      
    # GPIO 24 set up as an input, pulled down, connected to 3V3 on button press  
    GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  
      
    # now we'll define two threaded callback functions  
    # these will run in another thread when our events are detected  
    def my_callback(channel):  
        print "falling edge detected on 17"  
      
    def my_callback2(channel):  
        print "falling edge detected on 23"  
      
    print "Make sure you have a button connected so that when pressed"  
    print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n"  
    print "You will also need a second button connected so that when pressed"  
    print "it will connect GPIO port 24 (pin 18) to 3V3 (pin 1)\n"  
    print "You will also need a third button connected so that when pressed"  
    print "it will connect GPIO port 17 (pin 11) to GND (pin 14)"  
    raw_input("Press Enter when ready\n>")  
      
    # when a falling edge is detected on port 17, regardless of whatever   
    # else is happening in the program, the function my_callback will be run  
    GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300)  
      
    # when a falling edge is detected on port 23, regardless of whatever   
    # else is happening in the program, the function my_callback2 will be run  
    # 'bouncetime=300' includes the bounce control written into interrupts2a.py  
    GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback2, bouncetime=300)  
      
    try:  
        print "Waiting for rising edge on port 24"  
        GPIO.wait_for_edge(24, GPIO.RISING)  
        print "Rising edge detected on port 24. Here endeth the third lesson."  
      
    except KeyboardInterrupt:  
        GPIO.cleanup()       # clean up GPIO on CTRL+C exit  
    GPIO.cleanup()           # clean up GPIO on normal exit  
	
	
	
	/**********************************************
	
	 from datetime import datetime
import RPi.GPIO as GPIO  
import os
import mpd

GPIO.setmode(GPIO.BCM)  
  
# GPIO 23 & 24 set up as inputs. One pulled up, the other down.  
# 23 will go to GND when button pressed and 24 will go to 3V3 (3.3V)  
# this enables us to demonstrate both rising and falling edge detection  
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
# dummy !!
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  
  
# now we'll define the threaded callback function  
# this will run in another thread when our event is detected  
def my_callback(channel):
    print str(datetime.now())
    client = mpd.MPDClient(use_unicode=True)
    client.connect("localhost", 6600)
    #print client.status()
    if client.status()['state'] in ('play', 'pause'):
        client.stop()
    else:
        client.play()

# The GPIO.add_event_detect() line below set things up so that  
# when a rising edge is detected on port 23, regardless of whatever   
# else is happening in the program, the function "my_callback" will be run  
# Stop the program with CTRL+C
GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback, bouncetime=300)
  
try:  
    print "Waiting for falling edge on port 24"  
    GPIO.wait_for_edge(24, GPIO.RISING)  
    print "Falling edge detected. Here endeth the second lesson."  

except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit  
GPIO.cleanup()           # clean up GPIO on normal exit   