interrupt.lua 1.2 KB

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