Uncle Deadly Image

Hi there. Welcome to blog.oddbit.com! I post articles here on a variety of technical topics. Mostly I’m posting for myself (writing things up helps me remember them in the future), but I always hope the content I put here is helpful to someone else. If you find something here useful and want to say thanks, feel free to buy me a coffee!

Interrupts on the PiFace

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()

read more →