Friday, 9 August 2013

Efficiency of infinite loop to service GPIO

Efficiency of infinite loop to service GPIO

I'm using Python on Raspbian (a type of linux) on the Raspberry Pi (an
embedded processor board) to monitor GPIO inputs.
See simplified version of my code below. I have an infinite loop in the
python script waiting for something to happen on a GPIO i/p. Is this the
correct way to do it? I.e. does this mean that the CPU is running at full
whack just going round this loop, leaving no CPU cycles for other stuff?
Especially as I need to be running other things in parallel (e.g. the
browser).
Also what happens if the CPU is busy doing something else and a GPIO i/p
changes? Does the GPIO event get stored somewhere so it is eventually
serviced, or does it just get lost?
Is there a better way of doing this?
(For your answers, please note that I'm new to linux, and v. new to python
and real-time programming)
#!/usr/bin/python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def ButtonHandler(channel):
print "Button pressed " + str(channel)
# do stuff here
GPIO.add_event_detect(16, GPIO.FALLING, callback=ButtonHandler,
bouncetime=200)
while True:
pass

No comments:

Post a Comment