Instantly share code, notes, and snippets.

What would you like to do?
Reading buttons on the PiFace via interrupts.
import pifacecommon.core
import pifacecommon.interrupts
import os
import time
quit = False
def print_flag(event):
print 'You pressed button', event.pin_num, '.'
def stop_listening(event):
global quit
quit = True
# GPIOB is the input ports, including the four buttons.
port = pifacecommon.core.GPIOB
listener = pifacecommon.interrupts.PortEventListener(port)
# set up listeners for all buttons
listener.register(0, pifacecommon.interrupts.IODIR_ON, print_flag)
listener.register(1, pifacecommon.interrupts.IODIR_ON, print_flag)
listener.register(2, pifacecommon.interrupts.IODIR_ON, print_flag)
listener.register(3, pifacecommon.interrupts.IODIR_ON, stop_listening)
# Start listening for events. This spawns a new thread.
# Hang around until someone presses button 3.
while not quit:
print 'you pressed button 3 (quitting)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment