main.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "gpio2demo"
  3. VERSION = "1.0.0"
  4. log.info("main", PROJECT, VERSION)
  5. -- sys库是标配
  6. _G.sys = require("sys")
  7. if wdt then
  8. --添加硬狗防止程序卡死,在支持的设备上启用这个功能
  9. wdt.init(9000)--初始化watchdog设置为9s
  10. sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
  11. end
  12. local button_timer_outtime = 10 --按键定时器: 10ms
  13. local button_shake_time = 1 --按键消抖时间: button_shake_time*button_timer_outtime
  14. local button_long_time = 100 --按键长按时间: button_shake_time*button_timer_outtime
  15. local button_detect = true
  16. local button_state = false
  17. local button_cont = 0
  18. local BTN_PIN = 1 -- 按实际开发板选取
  19. -- 若固件支持防抖, 启用防抖
  20. if gpio.debounce then
  21. gpio.debounce(BTN_PIN, 5)
  22. end
  23. button = gpio.setup(BTN_PIN, function()
  24. if not button_detect then return end
  25. button_detect = false
  26. button_state = true
  27. end,
  28. gpio.PULLUP,
  29. gpio.FALLING)
  30. button_timer = sys.timerLoopStart(function()
  31. if button_state then
  32. if button() == 0 then
  33. button_cont = button_cont + 1
  34. if button_cont > button_long_time then
  35. print("long pass")
  36. end
  37. else
  38. if button_cont < button_shake_time then
  39. else
  40. if button_cont < button_long_time then
  41. print("pass")
  42. else
  43. print("long pass")
  44. end
  45. end
  46. button_cont = 0
  47. button_state = false
  48. button_detect = true
  49. end
  50. end
  51. end,button_timer_outtime)
  52. sys.run()
  53. -- sys.run()之后后面不要加任何语句!!!!!