led.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. debug_message('pattern')
  29. for k,v in pairs(params) do print(k,v) end
  30. function callback()
  31. if params and params.duty ~= nil then
  32. pwm.setduty(LED_PIN, params.duty)
  33. tmr.alarm(TIMER, params.interval, tmr.ALARM_SINGLE, callback)
  34. params = trans_func(params)
  35. else
  36. next_pattern()
  37. end
  38. end
  39. return callback
  40. end
  41. function enqueue(pattern)
  42. debug_message('enqueue')
  43. for k,v in pairs(Q) do print(k,v) end
  44. table.insert(Q, pattern)
  45. -- debug_message('tmr.state ' .. tostring(tmr.state(TIMER)))
  46. -- if not tmr.state(TIMER) then
  47. -- next_pattern()
  48. -- end
  49. end
  50. function next_pattern()
  51. debug_message('next_pattern')
  52. if next(Q) then
  53. debug_message('has next')
  54. local next_p = table.remove(Q, 1) -- how expensive is this?
  55. next_p()
  56. end
  57. end
  58. function kill()
  59. Q = {}
  60. tmr.unregister(TIMER)
  61. pwm.setduty(LED_PIN, 0)
  62. end
  63. -- built-in conveniences specific to Yo Button
  64. function q_fade_in()
  65. local params = {interval=2, duty=0}
  66. local transition = function(params)
  67. if params.duty >= 1023 then
  68. return nil
  69. else
  70. params.duty = params.duty + 1
  71. return params
  72. end
  73. end
  74. enqueue(pattern(params, transition))
  75. end
  76. function q_fade_out()
  77. local params = {interval=2, duty=MAX_DUTY}
  78. local transition = function(params)
  79. if params.duty <= 0 then
  80. return nil
  81. else
  82. params.duty = params.duty - 1
  83. return params
  84. end
  85. end
  86. enqueue(pattern(params, transition))
  87. end
  88. -- local function fade_in()
  89. -- local current_brightness = pwm.getduty(LED_PIN)
  90. -- if current_brightness < MAX_DUTY then
  91. -- current_brightness = current_brightness + 1
  92. -- pwm.setduty(LED_PIN, current_brightness)
  93. -- tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_in)
  94. -- else
  95. -- next_pattern()
  96. -- end
  97. -- end
  98. -- local function fade_out()
  99. -- local current_brightness = pwm.getduty(LED_PIN)
  100. -- if current_brightness > 0 then
  101. -- current_brightness = current_brightness - 1
  102. -- pwm.setduty(LED_PIN, current_brightness)
  103. -- tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_out)
  104. -- else
  105. -- next_pattern()
  106. -- end
  107. -- end
  108. -- local function heart_beat()
  109. -- local intervals = {40, 200, 40, 900} --alternating, millisec
  110. -- local current_brightness = pwm.getduty(LED_PIN)
  111. -- current_brightness = MAX_DUTY - current_brightness
  112. -- pwm.setduty(LED_PIN, current_brightness)
  113. -- tmr.alarm(TIMER, intervals[HEART_BEAT_IDX], tmr.ALARM_SINGLE, heart_beat)
  114. -- HEART_BEAT_IDX = HEART_BEAT_IDX + 1
  115. -- if HEART_BEAT_IDX > table.getn(intervals) then
  116. -- HEART_BEAT_IDX = 1
  117. -- end
  118. -- -- must call next_pattern() yourself
  119. -- end
  120. -- local function triple_blink()
  121. -- local intervals = {200, 50, 200, 50, 200, 50, 200} --alternating, millisec
  122. -- local current_brightness = pwm.getduty(LED_PIN)
  123. -- current_brightness = MAX_DUTY - current_brightness
  124. -- pwm.setduty(LED_PIN, current_brightness)
  125. -- TRIPLE_BLINK_IDX = TRIPLE_BLINK_IDX + 1
  126. -- if TRIPLE_BLINK_IDX > table.getn(intervals) then
  127. -- TRIPLE_BLINK_IDX = 1
  128. -- pwm.setduty(LED_PIN, 0)
  129. -- next_pattern()
  130. -- else
  131. -- tmr.alarm(TIMER, intervals[TRIPLE_BLINK_IDX], tmr.ALARM_SINGLE, triple_blink)
  132. -- end
  133. -- end
  134. -- interface --
  135. -- q_fade_in = enqueue(fade_in)
  136. -- q_fade_out = enqueue(fade_out)
  137. -- q_heart_beat = enqueue(heart_beat)
  138. -- q_triple_blink = enqueue(triple_blink)
  139. init(TIMER)