我在Python中编写一些代码,以便在按下按钮时显示LCD信息。这是我的代码部分:在Python中暂停主线程
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def action1(self):
temp = "15"
lcd.clear()
lcd.message(" Temp. Est.: \n" + " " + temp + " " + chr(223) + "C")
time.sleep(5.0)
lcd.clear
GPIO.add_event_detect(18, GPIO.RISING,callback=action1, bouncetime=800)
while True:
date = datetime.now().strftime('%m-%d-%Y %H:%M')
lcd.message(date)
time.sleep(5.0)
lcd.clear()
此代码的工作,但是当我按下按钮,显示我的温度,然后一次又一次的温度(这取决于当我按下按钮)。我读过“GPIO.add_event_detect”为回调函数运行第二个线程,它不会暂停主线程。我希望按下按钮后,只要按下按钮,它就会保持在LCD上,然后从底部开始编码,在我的情况下是时间。 我该如何实现它?谢谢!
您可以通过捕获按钮按下的时间并忽略按下按钮来快速“消除”它自己。这个问题的答案http://raspberrypi.stackexchange.com/questions/8544/gpio-interrupt-debounce可能会有所帮助。 –
显示值切换有多快?你的按钮可能是弹跳。你可以阅读这个:https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches/ –
也许你让我错了,或者我弄错了,但这里的问题不是重复的按钮按下。问题是我总是想要显示的代码是在while循环中,并且显示在主线程内的代码和由GPIO打开的另一个线程之间交替.add_event_detect – antonioag