优化 POWERKEY, 使用 mcu.ticks() 计算按下时间

This commit is contained in:
Mizore 2023-01-31 17:18:55 +08:00
parent 8021a7c885
commit ec0d16a894

View File

@ -24,30 +24,34 @@ socket.setDNS(nil, 2, "223.5.5.5")
mobile.setAuto(1000 * 10, 1000 * 60, 1000 * 5) mobile.setAuto(1000 * 10, 1000 * 60, 1000 * 5)
-- POWERKEY -- POWERKEY
local powerkey_timer = 0 local button_last_press_time, button_last_release_time = 0, 0
gpio.setup( gpio.setup(
35, 35,
function() function()
local powerkey_state = gpio.get(35) local current_time = mcu.ticks()
if powerkey_state == 0 then -- 按下
powerkey_timer = os.time() if gpio.get(35) == 0 then
else button_last_press_time = current_time -- 记录最后一次按下时间
if powerkey_timer == 0 then return
return end
end -- 释放
local time = os.time() - powerkey_timer if button_last_press_time == 0 then -- 开机前已经按下, 开机后释放
if time >= 2 then return
log.info("POWERKEY_LONG_PRESS", time) end
sys.publish("POWERKEY_LONG_PRESS") if current_time - button_last_release_time < 250 then -- 防止连按
else return
log.info("POWERKEY_SHORT_PRESS", time) end
sys.publish("POWERKEY_SHORT_PRESS") local duration = current_time - button_last_press_time -- 按键持续时间
end button_last_release_time = current_time -- 记录最后一次释放时间
powerkey_timer = 0 if duration > 2000 then
log.debug("EVENT.POWERKEY_LONG_PRESS", duration)
sys.publish("POWERKEY_LONG_PRESS", duration)
elseif duration > 50 then
log.debug("EVENT.POWERKEY_SHORT_PRESS", duration)
sys.publish("POWERKEY_SHORT_PRESS", duration)
end end
end, end,
gpio.PULLUP, gpio.PULLUP
gpio.FALLING
) )
config = require "config" config = require "config"