8 Star 26 Fork 14

wibim / luat-jt808

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
key.lua 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
wibim 提交于 2020-02-15 14:44 . 第一次上代码
--定义模块,导入依赖库
local base = _G
local sys = require"sys"
local rtos = require"rtos"
local chg = require"chg"
module(...)
--[[
sta:按键状态,IDLE表示空闲状态,PRESSED表示已按下状态,LONGPRESSED表示已经长按下状态
longprd:长按键判断时长,默认3秒;按下大于等于3秒再弹起判定为长按键;按下后,在3秒内弹起,判定为短按键
longcb:长按键处理函数
shortcb:短按键处理函数
]]
local sta,longprd,longcb,shortcb = "IDLE",3000
local function print(...)
base.print("--zbb--keypad",...)
end
local function longtimercb()
print("longtimercb",chg.getcharger())
if not chg.getcharger() then
sta = "LONGPRESSED"
end
end
local function keymsg(msg)
print("keymsg",msg.key_matrix_row,msg.key_matrix_col,msg.pressed,sta)
if msg.pressed then
sta = "PRESSED"
if not chg.getcharger() then
sys.timerStart(longtimercb,longprd)
end
else
sys.timerStop(longtimercb)
if sta=="PRESSED" then
if shortcb then
shortcb()
end
elseif sta=="LONGPRESSED" then
if longcb then
longcb()
else
print("rtos.poweroff")
rtos.poweroff()
end
end
sta = "IDLE"
end
end
--[[
函数名:setup
功能 :配置power key按键功能
参数 :
keylongprd:number类型或者nil,长按键判断时长,单位毫秒,如果是nil,默认3000毫秒
keylongcb:function类型或者nil,长按弹起时的回调函数,如果为nil,使用默认的处理函数,会自动关机
keyshortcb:function类型或者nil,短按弹起时的回调函数
返回值:无
]]
function setup(keylongprd,keylongcb,keyshortcb)
longprd,longcb,shortcb = keylongprd,keylongcb,keyshortcb
end
local function chgind(e,val)
-- print("chgind",e,val)
if e == "CHARGER" and val == 1 then
sys.timerStop(longtimercb)
sta = "IDLE"
end
return true
end
sys.subscribe("DEV_CHG_IND",chgind)
rtos.on(rtos.MSG_KEYPAD,keymsg)
rtos.init_module(rtos.MOD_KEYPAD,0,0,0)
Lua
1
https://gitee.com/wibim/luat-jt808.git
git@gitee.com:wibim/luat-jt808.git
wibim
luat-jt808
luat-jt808
master

搜索帮助