led.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. local LED_PIN = 1
  2. local OVERRIDE_PIN = 4
  3. local MAX_BRIGHTNESS = 1023
  4. local HEART_BEAT_IDX = 1
  5. local TRIPLE_BLINK_IDX = 1
  6. local TIMER = TIMERS.led
  7. local Q = {}
  8. local debug_message = debug_message
  9. local gpio = gpio
  10. local pwm = pwm
  11. local tmr = tmr
  12. local table = table
  13. local next = next
  14. module(...)
  15. local function init()
  16. debug_message('led.init')
  17. --handle timer, take exclusive control
  18. tmr.unregister(TIMER)
  19. --init pins
  20. gpio.mode(OVERRIDE_PIN, gpio.OUTPUT)
  21. pwm.setup(LED_PIN, 500, 0)
  22. pwm.start(LED_PIN)
  23. pwm.stop(LED_PIN)
  24. end
  25. local function enqueue(pattern)
  26. return function()
  27. table.insert(Q, pattern)
  28. if not tmr.state(TIMER) then
  29. next_pattern()
  30. end
  31. end
  32. end
  33. local function fade_in()
  34. local current_brightness = pwm.getduty(LED_PIN)
  35. if current_brightness < MAX_BRIGHTNESS then
  36. current_brightness = current_brightness + 1
  37. pwm.setduty(LED_PIN, current_brightness)
  38. tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_in)
  39. else
  40. next_pattern()
  41. end
  42. end
  43. local function fade_out()
  44. local current_brightness = pwm.getduty(LED_PIN)
  45. if current_brightness > 0 then
  46. current_brightness = current_brightness - 1
  47. pwm.setduty(LED_PIN, current_brightness)
  48. tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_out)
  49. else
  50. next_pattern()
  51. end
  52. end
  53. local function heart_beat()
  54. local intervals = {40, 200, 40, 900} --alternating, millisec
  55. local current_brightness = pwm.getduty(LED_PIN)
  56. current_brightness = MAX_BRIGHTNESS - current_brightness
  57. pwm.setduty(LED_PIN, current_brightness)
  58. tmr.alarm(TIMER, intervals[HEART_BEAT_IDX], tmr.ALARM_SINGLE, heart_beat)
  59. HEART_BEAT_IDX = HEART_BEAT_IDX + 1
  60. if HEART_BEAT_IDX > table.getn(intervals) then
  61. HEART_BEAT_IDX = 1
  62. end
  63. -- must call next_pattern() yourself
  64. end
  65. local function triple_blink()
  66. local intervals = {200, 50, 200, 50, 200, 50, 200} --alternating, millisec
  67. local current_brightness = pwm.getduty(LED_PIN)
  68. current_brightness = MAX_BRIGHTNESS - current_brightness
  69. pwm.setduty(LED_PIN, current_brightness)
  70. TRIPLE_BLINK_IDX = TRIPLE_BLINK_IDX + 1
  71. if TRIPLE_BLINK_IDX > table.getn(intervals) then
  72. TRIPLE_BLINK_IDX = 1
  73. pwm.setduty(LED_PIN, 0)
  74. next_pattern()
  75. else
  76. tmr.alarm(TIMER, intervals[TRIPLE_BLINK_IDX], tmr.ALARM_SINGLE, triple_blink)
  77. end
  78. end
  79. -- interface --
  80. q_fade_in = enqueue(fade_in)
  81. q_fade_out = enqueue(fade_out)
  82. q_heart_beat = enqueue(heart_beat)
  83. q_triple_blink = enqueue(triple_blink)
  84. function next_pattern()
  85. tmr.unregister(TIMER)
  86. if next(Q) then
  87. local next_p = table.remove(Q, 1) -- how expensive is this?
  88. next_p()
  89. else
  90. end
  91. end
  92. init(TIMER)