# Help with python code



## balanga (Feb 7, 2018)

I could with some help with some python code...

If I have GPIO board with four on/off switches how do I check for any of them being pressed?

Do I start four separate threads to.poll for a press?


----------



## ralphbsz (Feb 7, 2018)

Look at the documentation of the GPIO library you are using.  Is there any way to get interrupts, or program the inputs to be latches or counters?

If not, you'll have to poll.  It's not clear that you need threads for that though.  How long does it take to poll a GPIO input?  And do you need your python program to do something else during that time?


----------



## balanga (Feb 8, 2018)

I found some sample code here which I thought I may be able to adapt...


```
import sys
import threading
import pifacedigitalio


exit_barrier = threading.Barrier(2)


def deactivate_listener_and_exit(event):
   global exit_barrier
   exit_barrier.wait()


pifacedigital = pifacedigitalio.PiFaceDigital()
listener = pifacedigitalio.InputEventListener(chip=pifacedigital)
listener.register(0,
                 pifacedigitalio.IODIR_FALLING_EDGE,
                 deactivate_listener_and_exit)
listener.activate()
exit_barrier.wait() # program will wait here until exit_barrier releases
listener.deactivate()
sys.exit()
```

Not sure I understand any of it, but thought I could set up a wait on every switch and carry on normal processing until there was an interrupt...
I may be getting out of my depth here with terminology. Must study a bit more about wait states and interupts...


----------

