Files
BR_YKC/4G/源代码/lib/powerKey.lua
2026-05-21 13:24:05 +08:00

58 lines
1.8 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--- 模块功能:开机键功能配置
-- @module powerKey
-- @author openLuat
-- @license MIT
-- @copyright openLuat
-- @release 2018.06.13
require"sys"
module(..., package.seeall)
--[[
sta按键状态IDLE表示空闲状态PRESSED表示已按下状态LONGPRESSED表示已经长按下状态
longprd长按键判断时长默认3秒按下大于等于3秒再弹起判定为长按键按下后在3秒内弹起判定为短按键
longcb长按键处理函数
shortcb短按键处理函数
]]
local sta,longprd,longcb,shortcb = "IDLE",3000
local function longtimercb()
log.info("keypad.longtimercb")
sta = "LONGPRESSED"
end
local function keyMsg(msg)
log.info("keyMsg",msg.key_matrix_row,msg.key_matrix_col,msg.pressed)
if msg.pressed then
sta = "PRESSED"
sys.timerStart(longtimercb,longprd)
else
sys.timerStop(longtimercb)
if sta=="PRESSED" then
if shortcb then shortcb() end
elseif sta=="LONGPRESSED" then
(longcb or rtos.poweroff)()
end
sta = "IDLE"
end
end
--- 配置开机键长按弹起和短按弹起的功能.
-- 如何定义长按键和短按键例如长按键判断时长为3秒
-- 按下大于等于3秒再弹起判定为长按键
-- 按下后在3秒内弹起判定为短按键
-- @number[opt=3000] longPrd 长按键判断时长,单位毫秒
-- @function[opt=nil] longCb 长按弹起时的回调函数如果为nil使用默认的处理函数会自动关机
-- @function[opt=nil] shortCb 短按弹起时的回调函数
-- @return nil
-- @usage
-- powerKey.setup(nil,longCb,shortCb)
-- powerKey.setup(5000,longCb)
-- powerKey.setup()
function setup(longPrd,longCb,shortCb)
longprd,longcb,shortcb = longPrd or 3000,longCb,shortCb
end
rtos.on(rtos.MSG_KEYPAD,keyMsg)
rtos.init_module(rtos.MOD_KEYPAD,0,0,0)