Help with python code

balanga

Daemon

Thanks: 95
Messages: 2,339

#1
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

Daemon

Thanks: 767
Messages: 1,292

#2
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?
 
OP
OP
B

balanga

Daemon

Thanks: 95
Messages: 2,339

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

Code:
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...
 
Top