interrupt.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. -- using timer 5 for short/long press detection
  2. local longPress = 3000 -- 3 seconds
  3. local buttonPin = 6 -- GPIO6
  4. function debugMsg(msg)
  5. if DEBUG then
  6. print("Yo debug:", msg)
  7. end
  8. end
  9. function shortOrLongPress()
  10. local level = gpio.read(buttonPin)
  11. debugMsg('The pin value has changed to '..gpio.read(buttonPin))
  12. debugMsg("detected level " .. level)
  13. if level == 1 then -- button depressed
  14. debugMsg("LONG PRESS TIMER START")
  15. tmr.alarm(INTERRUPT_TIMER, longPress, 0, function()
  16. debugMsg("LONG PRESS")
  17. dofile('wifiSetup.lua')
  18. end)
  19. else -- button released
  20. debugMsg("SETUP STATUS " .. tostring(SETUP))
  21. tmr.stop(INTERRUPT_TIMER)
  22. if not SETUP then
  23. debugMsg("SHORT PRESS")
  24. dofile('sendYo.lua')
  25. end
  26. end
  27. end
  28. function debounce (func)
  29. local last = 0
  30. local delay = 200000
  31. return function (...)
  32. local now = tmr.now()
  33. if now - last < delay then
  34. tmr.stop(INTERRUPT_TIMER)
  35. debugMsg("DEBOUNCE PREVENTED EXTRA PRESS")
  36. if not SETUP then
  37. debugMsg("DEBOUNCE INTERPRETED AS SHORT PRESS")
  38. dofile('sendYo.lua')
  39. end
  40. return
  41. end
  42. last = now
  43. debugMsg("PRESS")
  44. return func(...)
  45. end
  46. end
  47. gpio.mode(buttonPin, gpio.INT, gpio.FLOAT)
  48. gpio.trig(buttonPin, "both", debounce(shortOrLongPress))