main.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. TIMERS = {
  2. interrupt = 0,
  3. setup_timeout = 1,
  4. led = 3
  5. }
  6. local button_pin = 6
  7. local led_pin = 1
  8. local yo = require('yo')
  9. local server = require('server')
  10. local led = require('led')
  11. local read = require('read')
  12. function setup_mode(func, ...)
  13. led.kill()
  14. led.q_heart_beat()
  15. wifi.setmode(wifi.STATIONAP)
  16. wifi.ap.config({
  17. ssid = "YoButton-" .. node.chipid(),
  18. pwd = "yobutton",
  19. auth = wifi.AUTH_OPEN,
  20. max = 1
  21. })
  22. wifi.ap.dhcp.start()
  23. wifi.sleeptype(wifi.NONE_SLEEP)
  24. --setup_mode inactivity timeout: 5 minutes
  25. tmr.alarm(TIMERS.setup_timeout, 60*5*1000, tmr.ALARM_SINGLE, function()
  26. default_mode(function()
  27. debug_message('setup_mode: timeout')
  28. return nil --nil function to use decorator side effects: code smell
  29. end)
  30. end)
  31. func(...)
  32. end
  33. function default_mode(func, ...)
  34. led.kill()
  35. led.q_fade_in()
  36. server.stop()
  37. wifi.setmode(wifi.STATION)
  38. wifi.sleeptype(wifi.NONE_SLEEP)
  39. local success = func(...)
  40. debug_message(success)
  41. wifi.sleeptype(wifi.MODEM_SLEEP)
  42. end
  43. function handle_short_press()
  44. default_mode(yo.yo, read.yo_recipient(), read.api_key())
  45. end
  46. function handle_long_press()
  47. setup_mode(server.start)
  48. end
  49. function handle_button_flip()
  50. local long_press_time = 3000 -- 3 seconds
  51. local level = gpio.read(button_pin)
  52. debug_message('handle_button_flip: ' .. level)
  53. if level == 1 then -- button depressed
  54. debug_message('handle_button_flip: pressed: start long press timer')
  55. tmr.alarm(TIMERS.interrupt, long_press_time, tmr.ALARM_SINGLE, function()
  56. debug_message('handle_button_flip: long press!')
  57. if server.is_serving() then
  58. debug_message('handle_button_flip: toggle setup OFF')
  59. handle_short_press()
  60. else
  61. debug_message('handle_button_flip: toggle setup ON')
  62. handle_long_press()
  63. end
  64. end)
  65. else -- button released
  66. debug_message('handle_button_flip: released: end long press timer')
  67. tmr.stop(TIMERS.interrupt)
  68. if not server.is_serving() then
  69. debug_message('handle_button_flip: short press!')
  70. handle_short_press()
  71. end
  72. end
  73. end
  74. function debounce(delay, func)
  75. local last = 0
  76. return function(...)
  77. local now = tmr.now()
  78. if now - last < delay then
  79. debug_message("debounce: prevent")
  80. return
  81. end
  82. last = now
  83. debug_message("debounce: allow")
  84. return func(...)
  85. end
  86. end
  87. wifi.setmode(wifi.STATION)
  88. pwm.setduty(led_pin, 0)
  89. handle_short_press = debounce(3000000, handle_short_press) --3 seconds
  90. gpio.mode(button_pin, gpio.INT, gpio.FLOAT)
  91. gpio.trig(button_pin, "both", debounce(50000, handle_button_flip)) --50 ms