Я пытаюсь обнаружить два события в двух разных GPIO в Beaglebone Black, а затем решить, какое из них произошло первым. Я использую Adafruit_BBIO.GPIO для кода, написанного на Python. Он не работает должным образом, и я понятия не имею, почему. Вот код:
import sys
import thread
import time
from datetime import datetime
import bitarray
import Adafruit_BBIO.GPIO as GPIO
gpio_state = [0, 0]
gpio_time = [0, 0]
ir_recv = ['GPIO0_26', 'GPIO1_12']
def checkEvent(index):
while True:
if GPIO.event_detected(ir_recv[index]):
if (gpio_state[index] == 0):
gpio_state[index] = 1
gpio_time[index] = datetime.now()
print ir_recv[index]
time.sleep(5) # time to avoid rebounces
for gpio in ir_recv:
GPIO.setup(gpio, GPIO.IN)
GPIO.add_event_detect(gpio, GPIO.RISING)
try:
thread.start_new_thread(checkEvent, (0, ) )
thread.start_new_thread(checkEvent, (1, ) )
except:
print "Error: unable to start thread"
while True:
if (gpio_state[0] == 1) and (gpio_state[1] == 1):
if gpio_time[0] > gpio_time[1]:
print "1"
if gpio_time[0] < gpio_time[1]:
print "2"
if gpio_time[0] == gpio_time[1]:
print "???"
gpio_state[0] = 0
gpio_state[1] = 0
gpio_time[0] = 0
gpio_time[1] = 0
Я не получаю никакой ошибки. Основная проблема заключается в том, что события не сравниваются правильно, например. хотя событие в GPIO0_26 происходит раньше, чем в GPIO1_12 (т. е. gpio_time[0] меньше, чем gpio_time[1]), вывод в последнем цикле While не выводит «2». Также иногда код дважды выводит вывод GPIO из потоков.
Заранее спасибо за любое предложение найти решение.