led.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. local LED_PIN = 1
  2. local MAX_DUTY = 1023
  3. local TIMER = TIMERS.led
  4. local Q = {}
  5. local debug_message = debug_message
  6. local pwm = pwm
  7. local tmr = tmr
  8. local table = table
  9. local next = next
  10. module(...)
  11. local function init()
  12. --handle timer, take exclusive control
  13. tmr.unregister(TIMER)
  14. --init pins
  15. pwm.setup(LED_PIN, 500, 0)
  16. pwm.start(LED_PIN)
  17. pwm.stop(LED_PIN)
  18. end
  19. -- params must contain duty and interval
  20. function pattern(params, trans_func)
  21. local params = params
  22. local callback = nil
  23. callback = function()
  24. if params and params.duty ~= nil then
  25. pwm.setduty(LED_PIN, params.duty)
  26. tmr.alarm(TIMER, params.interval, tmr.ALARM_SINGLE, callback)
  27. params = trans_func(params)
  28. else
  29. next_pattern()
  30. end
  31. end
  32. return callback
  33. end
  34. function next_pattern()
  35. debug_message('next_pattern')
  36. if next(Q) then
  37. debug_message('has next')
  38. local next_p = table.remove(Q, 1) -- how expensive is this?
  39. next_p()
  40. end
  41. end
  42. function enqueue(pattern)
  43. debug_message('enqueue')
  44. table.insert(Q, pattern)
  45. if not tmr.state(TIMER) then
  46. next_pattern()
  47. end
  48. end
  49. function kill()
  50. Q = {}
  51. tmr.unregister(TIMER)
  52. pwm.setduty(LED_PIN, 0)
  53. end
  54. -- built-in conveniences specific to Yo Button
  55. function q_fade_in()
  56. local params = {interval=2, duty=0}
  57. local transition = function(params)
  58. if params.duty >= 1023 then
  59. return nil
  60. else
  61. params.duty = params.duty + 1
  62. return params
  63. end
  64. end
  65. enqueue(pattern(params, transition))
  66. end
  67. function q_fade_out()
  68. local params = {interval=2, duty=MAX_DUTY}
  69. local transition = function(params)
  70. if params.duty <= 0 then
  71. return nil
  72. else
  73. params.duty = params.duty - 1
  74. return params
  75. end
  76. end
  77. enqueue(pattern(params, transition))
  78. end
  79. function q_triple_blink()
  80. local params = {interval=50, duty=MAX_DUTY, reps = 3}
  81. local transition = function(params)
  82. if params.reps <= 0 then
  83. return nil
  84. elseif params.interval == 50 then
  85. params.interval = 200
  86. params.duty = 0
  87. params.reps = params.reps - 1
  88. else
  89. params.interval = 50
  90. params.duty = MAX_DUTY
  91. end
  92. return params
  93. end
  94. enqueue(pattern(params, transition))
  95. end
  96. function q_heart_beat()
  97. local params = {interval=40, duty=MAX_DUTY, index = 1}
  98. local transition = function(params)
  99. local intervals = {40, 200, 40, 900}
  100. params.index = params.index + 1
  101. if params.index > 4 then
  102. params.index = 1
  103. end
  104. params.interval = intervals[params.index]
  105. params.duty = (params.index % 2) * MAX_DUTY
  106. return params
  107. end
  108. enqueue(pattern(params, transition))
  109. end
  110. init(TIMER)