interrupt.lua 1.2 KB

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