interrupt.lua 1.2 KB

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