I'm developing a project in python with raspberry pi and two infrared sensors.
The infrared emitter is on an Arduino and is continuously sending a code every 50ms and this rate cannot be changed.
I need the raspberrypi python script to check the two infrared sensors (left and right) every 500ms. If the left sensor received a code within this 500ms time interval, show the received code, otherwise, show that the left sensor did not receive a code. The process is repeated with the right sensor.
I created a simple python script for this. However, I always need to clear the buffer so that old values that have been read do not disturb the check. I'm doing this with a "while" reading the buffer values one by one.
Does EVDEV have a flush function to clear the buffer or is there a better way to do this?
#!/usr/bin/python2.7
from evdev import InputDevice
import time
device1 = InputDevice('/dev/input/event4')
device2 = InputDevice('/dev/input/event3')
while(True):
dataSensor1 = device1.read_one()
dataSensor2 = device2.read_one()
if(dataSensor1!=None):
print("Left: ", dataSensor1.value)
#delete old readings from the Left sensor queue
while device1.read_one()!=None:
pass
else:
print("no reading on the left sensor")
if(dataSensor2!=None):
print("Right: ", dataSensor2.value)
#delete old readings from the Right sensor queue
while device2.read_one()!=None:
pass
else:
print("no reading on the right sensor")
time.sleep(0.5)