⚡ 优化 POWERKEY, 使用 mcu.ticks()
计算按下时间
This commit is contained in:
parent
8021a7c885
commit
ec0d16a894
@ -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 time >= 2 then
|
if button_last_press_time == 0 then -- 开机前已经按下, 开机后释放
|
||||||
log.info("POWERKEY_LONG_PRESS", time)
|
return
|
||||||
sys.publish("POWERKEY_LONG_PRESS")
|
|
||||||
else
|
|
||||||
log.info("POWERKEY_SHORT_PRESS", time)
|
|
||||||
sys.publish("POWERKEY_SHORT_PRESS")
|
|
||||||
end
|
end
|
||||||
powerkey_timer = 0
|
if current_time - button_last_release_time < 250 then -- 防止连按
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local duration = current_time - button_last_press_time -- 按键持续时间
|
||||||
|
button_last_release_time = current_time -- 记录最后一次释放时间
|
||||||
|
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"
|
||||||
|
Loading…
Reference in New Issue
Block a user