Button press with python & GPIO on raspberry pi

the code sample is taken from here.
i just use one button with the interrupt version, but i’m not quite satisfied with my dummy-button work around 😦

every button press mpd is started or stopped (see my_callback).

#!/usr/bin/env python2.7  
# script by Alex Eames http://RasPi.tv  
  
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   

i used this mpd library for python (see doc for the library)
to install it, just do
  sudo pip install python-mpd2
to start the python scritp at system start add a line to /etc/rc.local
  sudo nano /etc/rc.local At the bottom, just above exit 0 we’ll add a call to our script (taken from here
  python ../myscript.py
playlists cleanup with python
  client.clear()
add new playlists (or songs with .add) to mpc
  client.load(„bbc6.m3u“)
the file bbc6.m3u contains just

Advertisements

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s