【DigiKey“智造万物,快乐不停”创意大赛】MAIX BIT KIT 的按键处理
[复制链接]
给MAIX BIT 配的Grove Dual Button 按起来不太稳定,于是调整了一下代码:
class main_obj():
def event(self,name,*args,**kw):
print(name,*args,**kw)
def call_object_method(obj,method_name,*args,**kv):
print('call_object_method',obj,'method',method_name,'args',args,'kv',kv)
if method_name in dir(obj):
getattr(obj,method_name)(method_name,*args,**kv)
import utime
from Maix import GPIO
from fpioa_manager import fm
root = main_obj()
last_ticks=0
def test_irq22(pin_num):
global last_ticks
disable_buttons_irq()
utime.sleep_ms(10)
tmp_last_ticks = utime.ticks()
print("key22?", pin_num,'val22',key22.value(),'val23',key23.value(), 'at',tmp_last_ticks,last_ticks,tmp_last_ticks-last_ticks,"\n")
if tmp_last_ticks - last_ticks > 250 and key22.value()==0 and key23.value()==1 :
call_object_method(root,'event','btn1_pressed',pin_num)
last_ticks = tmp_last_ticks
utime.sleep_ms(200)
enable_buttons_irq()
def test_irq23(pin_num):
global last_ticks
disable_buttons_irq()
utime.sleep_ms(10)
tmp_last_ticks = utime.ticks()
print("key23?", pin_num,'val22',key22.value(),'val23',key23.value(), 'at',tmp_last_ticks,last_ticks,tmp_last_ticks-last_ticks,"\n")
if tmp_last_ticks - last_ticks > 250 and key22.value()==1 and key23.value()==0 :
call_object_method(root,'event','btn2_pressed',pin_num)
last_ticks = tmp_last_ticks
utime.sleep_ms(200)
enable_buttons_irq()
fm.register(22, fm.fpioa.GPIOHS0)
key22 = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_NONE)
fm.register(23, fm.fpioa.GPIOHS1)
key23 = GPIO(GPIO.GPIOHS1, GPIO.IN, GPIO.PULL_NONE)
print(dir(key22))
utime.sleep_ms(100)
def enable_buttons_irq():
key22.irq(test_irq22, GPIO.IRQ_FALLING, GPIO.WAKEUP_NOT_SUPPORT,7)
key23.irq(test_irq23, GPIO.IRQ_FALLING, GPIO.WAKEUP_NOT_SUPPORT,7)
def disable_buttons_irq():
key22.disirq() # 禁用中断
key23.disirq() # 禁用中断
enable_buttons_irq()
print("0\n")
while True:
pass
utime.sleep_ms(300) # 在 0.33 秒内等待触发
#key.disirq() # 禁用中断
#fm.unregister(16)
主要的处理技巧也很简单:先停用中断,延时 10ms消抖,然后检查是否还是低电平、是否超过了250ms。如果都满足,就认为正常触发。在恢复中断退出程序前再延时200ms。这样只要是一下下按就不会有漏,也不会因为抖动重复。如果需要减小连击间隔,同时减少250ms、200ms就可以了。
|