powerKey.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --- 模块功能:开机键功能配置
  2. -- @module powerKey
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.06.13
  7. require"sys"
  8. module(..., package.seeall)
  9. --[[
  10. sta:按键状态,IDLE表示空闲状态,PRESSED表示已按下状态,LONGPRESSED表示已经长按下状态
  11. longprd:长按键判断时长,默认3秒;按下大于等于3秒再弹起判定为长按键;按下后,在3秒内弹起,判定为短按键
  12. longcb:长按键处理函数
  13. shortcb:短按键处理函数
  14. ]]
  15. local sta,longprd,longcb,shortcb = "IDLE",3000
  16. local function longtimercb()
  17. log.info("keypad.longtimercb")
  18. sta = "LONGPRESSED"
  19. end
  20. local function keyMsg(msg)
  21. log.info("keyMsg",msg.key_matrix_row,msg.key_matrix_col,msg.pressed)
  22. if msg.pressed then
  23. sta = "PRESSED"
  24. sys.timerStart(longtimercb,longprd)
  25. else
  26. sys.timerStop(longtimercb)
  27. if sta=="PRESSED" then
  28. if shortcb then shortcb() end
  29. elseif sta=="LONGPRESSED" then
  30. (longcb or rtos.poweroff)()
  31. end
  32. sta = "IDLE"
  33. end
  34. end
  35. --- 配置开机键长按弹起和短按弹起的功能.
  36. -- 如何定义长按键和短按键,例如长按键判断时长为3秒:
  37. -- 按下大于等于3秒再弹起判定为长按键;
  38. -- 按下后,在3秒内弹起,判定为短按键
  39. -- @number[opt=3000] longPrd 长按键判断时长,单位毫秒
  40. -- @function[opt=nil] longCb 长按弹起时的回调函数,如果为nil,使用默认的处理函数,会自动关机
  41. -- @function[opt=nil] shortCb 短按弹起时的回调函数
  42. -- @return nil
  43. -- @usage
  44. -- powerKey.setup(nil,longCb,shortCb)
  45. -- powerKey.setup(5000,longCb)
  46. -- powerKey.setup()
  47. function setup(longPrd,longCb,shortCb)
  48. longprd,longcb,shortcb = longPrd or 3000,longCb,shortCb
  49. end
  50. rtos.on(rtos.MSG_KEYPAD,keyMsg)
  51. rtos.init_module(rtos.MOD_KEYPAD,0,0,0)