我刚刚在我的RPi 3中添加了一个Adafruit2.2英寸的PiTFT,我发现它包含的四个按钮有问题。我开始使用GPIO,但是在重复触发回调或者什么都没有按下的时候,我遇到了很多问题,但是我得到的印象是四个按钮都可以工作,因为我每个按钮都收到了中断。
我切换到了PIGPIO,并在网上遵循了示例,这对于GPIO17上的按钮工作得很好,但对其他三个按钮都不起作用。我认为可能只有第一个“接受”,所以我尝试以不同的顺序分配回调,或者删除一些回调,但都没有成功。我的代码(从其他在线示例中复制)有没有什么问题值得你们注意?
按17键可以打印出预期的文本,其他任何人都不会执行任何操作。
最终,这将在捕获温度和湿度读数并记录它们的循环中运行,我的计划是使用按钮关闭程序,关闭程序,并关闭Pi和其他几个辅助任务。
import pigpio
import time
class Buttons:
def __init__(self):
self.pi = pigpio.pi()
the_pins = [
{"pin":17, "handler":self.btnHandler17},
{"pin":22, "handler":self.btnHandler22},
{"pin":23, "handler":self.btnHandler23},
{"pin":27, "handler":self.btnHandler27}]
for x in the_pins:
pin = x["pin"]
self.pi.set_glitch_filter(pin, 20000)
self.pi.set_mode(pin, pigpio.INPUT)
self.pi.callback(pin, pigpio.FALLING_EDGE, x["handler"])
while True:
time.sleep(1)
def btnHandler17(self,pin,level,tick):
print("Button 17 Pressed",pin,level,tick)
def btnHandler22(self,pin,level,tick):
print("Button 22 Pressed",pin,level,tick)
def btnHandler23(self,pin,level,tick):
print("Button 23 Pressed",pin,level,tick)
def btnHandler27(self,pin,level,tick):
print("Button 27 Pressed",pin,level,tick)
myButtons = Buttons()https://stackoverflow.com/questions/51348963
复制相似问题