led.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. local LED_PIN = 1
  2. local MAX_DUTY = 1023
  3. local HEART_BEAT_IDX = 1
  4. local TRIPLE_BLINK_IDX = 1
  5. local TIMER = TIMERS.led
  6. local Q = {}
  7. local debug_message = debug_message
  8. local pwm = pwm
  9. local tmr = tmr
  10. local table = table
  11. local next = next
  12. local tostring = tostring
  13. local print = print
  14. local pairs = pairs
  15. module(...)
  16. local function init()
  17. debug_message('led.init')
  18. --handle timer, take exclusive control
  19. tmr.unregister(TIMER)
  20. --init pins
  21. pwm.setup(LED_PIN, 500, 0)
  22. pwm.start(LED_PIN)
  23. pwm.stop(LED_PIN)
  24. end
  25. -- params must contain duty and interval
  26. function pattern(params, trans_func)
  27. local params = params
  28. local callback = nil
  29. callback = function()
  30. if params and params.duty ~= nil then
  31. pwm.setduty(LED_PIN, params.duty)
  32. tmr.alarm(TIMER, params.interval, tmr.ALARM_SINGLE, callback)
  33. params = trans_func(params)
  34. else
  35. next_pattern()
  36. end
  37. end
  38. return callback
  39. end
  40. function enqueue(pattern)
  41. debug_message('enqueue')
  42. table.insert(Q, pattern)
  43. debug_message('tmr.state ' .. tostring(tmr.state(TIMER)))
  44. if not tmr.state(TIMER) then
  45. next_pattern()
  46. end
  47. end
  48. function next_pattern()
  49. debug_message('next_pattern')
  50. if next(Q) then
  51. debug_message('has next')
  52. local next_p = table.remove(Q, 1) -- how expensive is this?
  53. next_p()
  54. end
  55. end
  56. function kill()
  57. Q = {}
  58. tmr.unregister(TIMER)
  59. pwm.setduty(LED_PIN, 0)
  60. end
  61. function q_blink()
  62. print('q_blink')
  63. local params = {interval=1000, duty=0}
  64. local transition = function(p)
  65. if params.duty == 0 then
  66. params.duty = MAX_DUTY
  67. else
  68. params.duty = 0
  69. end
  70. return params
  71. end
  72. enqueue(pattern(params, transition))
  73. end
  74. -- built-in conveniences specific to Yo Button
  75. function q_fade_in()
  76. local params = {interval=2, duty=0}
  77. local transition = function(params)
  78. if params.duty >= 1023 then
  79. return nil
  80. else
  81. params.duty = params.duty + 1
  82. return params
  83. end
  84. end
  85. enqueue(pattern(params, transition))
  86. end
  87. function q_fade_out()
  88. local params = {interval=2, duty=MAX_DUTY}
  89. local transition = function(params)
  90. if params.duty <= 0 then
  91. return nil
  92. else
  93. params.duty = params.duty - 1
  94. return params
  95. end
  96. end
  97. enqueue(pattern(params, transition))
  98. end
  99. -- local function fade_in()
  100. -- local current_brightness = pwm.getduty(LED_PIN)
  101. -- if current_brightness < MAX_DUTY then
  102. -- current_brightness = current_brightness + 1
  103. -- pwm.setduty(LED_PIN, current_brightness)
  104. -- tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_in)
  105. -- else
  106. -- next_pattern()
  107. -- end
  108. -- end
  109. -- local function fade_out()
  110. -- local current_brightness = pwm.getduty(LED_PIN)
  111. -- if current_brightness > 0 then
  112. -- current_brightness = current_brightness - 1
  113. -- pwm.setduty(LED_PIN, current_brightness)
  114. -- tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_out)
  115. -- else
  116. -- next_pattern()
  117. -- end
  118. -- end
  119. -- local function heart_beat()
  120. -- local intervals = {40, 200, 40, 900} --alternating, millisec
  121. -- local current_brightness = pwm.getduty(LED_PIN)
  122. -- current_brightness = MAX_DUTY - current_brightness
  123. -- pwm.setduty(LED_PIN, current_brightness)
  124. -- tmr.alarm(TIMER, intervals[HEART_BEAT_IDX], tmr.ALARM_SINGLE, heart_beat)
  125. -- HEART_BEAT_IDX = HEART_BEAT_IDX + 1
  126. -- if HEART_BEAT_IDX > table.getn(intervals) then
  127. -- HEART_BEAT_IDX = 1
  128. -- end
  129. -- -- must call next_pattern() yourself
  130. -- end
  131. -- local function triple_blink()
  132. -- local intervals = {200, 50, 200, 50, 200, 50, 200} --alternating, millisec
  133. -- local current_brightness = pwm.getduty(LED_PIN)
  134. -- current_brightness = MAX_DUTY - current_brightness
  135. -- pwm.setduty(LED_PIN, current_brightness)
  136. -- TRIPLE_BLINK_IDX = TRIPLE_BLINK_IDX + 1
  137. -- if TRIPLE_BLINK_IDX > table.getn(intervals) then
  138. -- TRIPLE_BLINK_IDX = 1
  139. -- pwm.setduty(LED_PIN, 0)
  140. -- next_pattern()
  141. -- else
  142. -- tmr.alarm(TIMER, intervals[TRIPLE_BLINK_IDX], tmr.ALARM_SINGLE, triple_blink)
  143. -- end
  144. -- end
  145. -- interface --
  146. -- q_fade_in = enqueue(fade_in)
  147. -- q_fade_out = enqueue(fade_out)
  148. -- q_heart_beat = enqueue(heart_beat)
  149. -- q_triple_blink = enqueue(triple_blink)
  150. init(TIMER)