使用Python检测和比较Beaglebone中的GPIO事件
我试图在Beaglebone Black中的两个不同GPIO中检测两个事件,然后确定哪一个先发生。我使用Adafrubbio.GPIO来编写用Python编写的代码。它不能正常工作,也不知道为什么。代码如下:使用Python检测和比较Beaglebone中的GPIO事件,python,multithreading,beagleboneblack,Python,Multithreading,Beagleboneblack,我试图在Beaglebone Black中的两个不同GPIO中检测两个事件,然后确定哪一个先发生。我使用Adafrubbio.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_ti
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
导入系统
导入线程
导入时间
从日期时间导入日期时间
导入位数组
将Adafruitbbio.GPIO导入为GPIO
gpio_状态=[0,0]
gpio_时间=[0,0]
ir_recv=['GPIO0_26','GPIO1_12']
def checkEvent(索引):
尽管如此:
如果检测到GPIO.event(红外接收[索引]):
如果(gpio_状态[索引]==0):
gpio_状态[索引]=1
gpio_时间[index]=datetime.now()
打印ir_recv[索引]
时间。睡觉(5)#是时候避免再次约会了
对于ir_recv中的gpio:
GPIO.setup(GPIO,GPIO.IN)
GPIO.add\u事件检测(GPIO,GPIO.RISING)
尝试:
线程。启动新线程(检查事件,(0,))
线程。启动新线程(checkEvent,(1,))
除:
打印“错误:无法启动线程”
尽管如此:
如果(gpio_状态[0]==1)和(gpio_状态[1]==1):
如果gpio_时间[0]>gpio_时间[1]:
打印“1”
如果gpio_时间[0]# pip install -U PyBBIO
你有什么错误吗。或未预测预期结果,如果是,请提供输入和预期结果output@sundarnatarajССааа:添加了我遇到的问题,如果没有GPIO数据,则两个线程都处于快速忙等待循环中。考虑添加<代码>时间。睡眠(0.01),或者找到在代码> GPIO.EngEnTebug()/代码>上阻塞的方法,考虑使用内核创建(时间戳)GPIO输入事件的能力。看见
# pip install -U PyBBIO