interrupt.lua 1.3 KB

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