我正在用Python编写液晶显示屏的代码,我希望它能显示闪烁的“按下选择”信息,直到按下液晶显示上的一个按钮。
问题是,当程序处于休眠状态时,它不会注册用户按下按钮,这意味着我必须在打印消息时准确地按下该按钮。
我怎么能不睡觉就这么做呢?
这是我使用的代码:
#lcd.is_pressed(LCD.SELECT) - checks if button is pressed
#lcd.message - prints text on LCD
#lcd.clear - removes all text from LCD
while not lcd.is_pressed(LCD.SELECT):
lcd.message ('Press Select')
sleep(2)
lcd.clear()
sleep(2)发布于 2015-01-21 23:50:06
将您的时间分成更小的部分;这将每百分之一秒检查一次按钮:
i = 0
while not lcd.is_pressed(LCD.SELECT):
i = (i + 1) % 400
if i == 0:
lcd.message("Press Select")
elif i == 200:
lcd.clear()
sleep(0.01)发布于 2015-01-22 03:54:48
试试这个:
import time
delayTime=2 #In seconds
dummyVariable=is_pressed(LCD.Select)
while not dummyVariable:
lcd.message('Press Select')
dummyVariable=softWait(delayTime)
lcd.clear()
dummyVariable=softWait(delayTime)
def softWait(delayTime)
t0 = time.time()
t1 = time.time()
while t0-t1<delayTime:
dummyVariable=is_pressed(LCD.Select)
t1=time.time()
if dummyVariable:
return True
return False如果不知道is_pressed是如何操作的,我就不能真正测试它。但是,最有可能的问题是,如果is_pressed只在当前单击按钮时注册True,如果单击按钮比系统返回time.time()或lcd.message()调用的时间更快,就会失败。真快啊。
https://stackoverflow.com/questions/28078929
复制相似问题