[Tutor] Listen for input while performing other tasks

Sean Carolan scarolan at gmail.com
Sun Nov 25 13:32:44 CET 2012


> If you show us how you check whether the button is pressed, we may be able to
> show you how to run that asynchronously.

Apologies for the previous email; I think I sent it in HTML format.
Gmail changed their user interface again...

This is how I'm checking for a button press:

modes = (weather, alarmclock, moodlight)
currmode = 0
mode = 10  ## mode button is on pin #10

def checkMode():
    '''Returns the current mode and whether a mode change was detected.'''
    global currmode
    modechange = False
    GPIO.setup(mode, GPIO.IN)
    if GPIO.input(mode) == False:
        print "Mode button pressed."
        if currmode < len(modes) - 1:
            currmode += 1
        else:
            currmode = 0
        modechange = True
    #print modes[currmode]
    return (modes[currmode], modechange)

It's pretty simple, really.  If GPIO.input(mode) returns False, then
the button is pressed.  My function above returns the current mode
from my modes tuple, and whether a change was detected.


More information about the Tutor mailing list