开关机

# 简介 Air722模块LuatOS-Air固件默认上电自动开机。 # API说明 | API接口 | 描述 | | ------------------------- | -------------------------------------- | | powerKey.setup(longPrd, longCb, shortCb) | 开机键功能配置 | 详细的API介绍见[powerKey API章节](https://doc.openluat.com/wiki/21?wiki_page_id=2288) # 开关机使用实例 开关机的具体使用方式可以参考如下示例代码,示例代码的主要步骤如下: 1.开机一般默认是按住开机键1.2s可以开机,开机后usb会打印log. 2.关机时,是调用powerKey.lua 库中接口,设置longprd 可以修改关机按下的时间,代码如下: * 按键消息处理函数 ````lua --[[ 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 log.info("关机",sta); (longcb or rtos.poweroff)() end sta = "IDLE" end end ```` * 配置开机键长按弹起和短按弹起的功能. ````lua -- 如何定义长按键和短按键,例如长按键判断时长为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(20000,longCb) -- powerKey.setup() function setup(longPrd,longCb,shortCb) longprd,longcb,shortcb = longPrd or 3000,longCb,shortCb end ```` * 注册按键消息,初始化模块键盘引脚 ````lua rtos.on(rtos.MSG_KEYPAD,keyMsg) rtos.init_module(rtos.MOD_KEYPAD,0,0,0) ```` --- 3.上面是对powerKey库中代码介绍,新建一个空白工程, 加载require "powerKey",设定按下按键4s关机。 ```lua --加载powerKey测试模块 require "powerKey" --定义长按4s 关机 powerKey.setup(4000, function() rtos.poweroff() end) ``` 下面是创建空白工程后加入上段代码,烧录代码后,按开机键,关机演示效果 ![powerKey.gif](https://cos.easydoc.net/22753220/files/l0c8jpp6.gif) ![image.png](https://cos.easydoc.net/22753220/files/l0c8s9hc.png) # 硬件设计 见硬件设计指南 [开关机 章节](https://hmi.wiki.luatos.com/doc/65042949/e6zPC3k9/6JwYmhOu)