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

pifacecommon.core.init()

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.

listener.activate()

Hang around until someone presses button 3.

while not quit: time.sleep(1)

print ‘you pressed button 3 (quitting)’ listener.deactivate()