我有一个覆盆子pi和一个gpio引脚我发送脉冲,因此我有
python代码来检测该引脚上的中断,它们每秒最多2个中断.现在我想将这个值(总中断数)传递给pygame应用程序.
目前用于检测中断的python代码写入总数为.检测到中断时检测到中断,然后pygame应用程序从文件中读取该数字.因此我的问题是如何使用线程在pygame中集成中断检测代码,因为我希望pygame应用程序和中断检测代码并行运行.我在某处读到pygame不是线程安全的.
我的中断检测代码:
GPIO.setmode(GPIO.BCM)
count = 0
GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(channel):
file = open('hello.txt','w')
global count
count += 1
file.write(str(count))
GPIO.add_event_detect(2,GPIO.BOTH, callback=my_callback)
while True:
print "Waiting for input."
sleep(60)
GPIO.cleanup()
pygame应用程序代码:
pygame.init()
size=[640,640]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Test")
done=False
clock=py