led.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. --pin definitions, should we move it out to another module?
  2. local led_pin = 1 --GPIO 5
  3. local override_pin = 2 --GPIO 4
  4. --pattern definitions
  5. local STOPPED = 0
  6. local HEARTBEAT = 1
  7. local FADE_IN = 2
  8. local FADE_OUT = 3
  9. local TRIPLE_BLINK = 4
  10. --private constants
  11. local heartbeat_times = {40, 200, 40, 900} --format is alternating on and off times in ms
  12. local triple_blink_times = {100, 20, 100, 20, 100, 20, 100} --format is alternating off and on times in ms
  13. local timer_id = nil --"constant"
  14. local led_pwm_frequency = 500 --units: hz
  15. local led_max_brightness = 1023
  16. --state variables
  17. local current_pattern
  18. local pattern_queue = {}
  19. local heartbeat_index = 1
  20. local triple_blink_index = 1
  21. --function declarations
  22. local init = nil
  23. local stop = nil
  24. local do_pattern = nil
  25. local fade_in_update = nil
  26. local fade_out_update = nil
  27. local heartbeat_update = nil
  28. local triple_blink_update = nil
  29. local end_pattern = nil
  30. local override_enable = nil
  31. local override_disable = nil
  32. local invalid_timer = nil
  33. -----------------------public functions------------------------------
  34. function init(timer)
  35. --handle timer, take exclusive control
  36. timer_id = timer
  37. tmr.unregister(timer_id)
  38. --init pins
  39. gpio.mode(override_pin, gpio.OUTPUT)
  40. pwm.setup(led_pin, led_pwm_frequency, 0)
  41. pwm.start(led_pin)
  42. pwm.stop(led_pin)
  43. stop()
  44. end
  45. function stop()
  46. if(invalid_timer()) then
  47. print "Error, call led.init(timer_id) before calling any member functions"
  48. return
  49. end
  50. tmr.unregister(timer_id)
  51. --deassert control
  52. override_disable()
  53. --stop pwm
  54. --pwm.stop(led_pin)
  55. current_pattern = STOPPED
  56. pattern_queue = {}
  57. end
  58. function do_pattern(pattern)
  59. if(invalid_timer()) then
  60. print "Error, call led.init(timer_id) before calling any member functions"
  61. return
  62. end
  63. --TODO: CONFIRM VALID PATTERN
  64. --if(not is_valid_pattern(pattern))
  65. if(pattern == current_pattern) then
  66. --do nothing
  67. return
  68. end
  69. if not (current_pattern == STOPPED) then
  70. --put pattern in queue
  71. table.insert(pattern_queue, pattern)
  72. else
  73. --start pattern!
  74. current_pattern = pattern
  75. override_enable()
  76. --if train could be optimized with else's but this is more readable
  77. if(current_pattern == FADE_IN) then
  78. pwm.setduty(led_pin, 0)
  79. pwm.start(led_pin)
  80. fade_in_update()
  81. end
  82. if(current_pattern == FADE_OUT) then
  83. pwm.setduty(led_pin, led_max_brightness)
  84. pwm.start(led_pin)
  85. fade_out_update()
  86. end
  87. if(current_pattern == HEARTBEAT) then
  88. pwm.setduty(led_pin, 0)
  89. pwm.start(led_pin)
  90. heartbeat_index = 1
  91. heartbeat_update()
  92. end
  93. if(current_pattern == TRIPLE_BLINK) then
  94. pwm.setduty(led_pin, led_max_brightness)
  95. pwm.start(led_pin)
  96. triple_blink_index = 1
  97. triple_blink_update()
  98. end
  99. end
  100. end
  101. -----------------------private functions-------------------------
  102. function fade_in_update()
  103. local current_brightness = pwm.getduty(led_pin)
  104. current_brightness = current_brightness + 1
  105. if(current_brightness > led_max_brightness) then
  106. current_brightness = led_max_brightness
  107. end
  108. pwm.setduty(led_pin, current_brightness)
  109. if current_brightness < led_max_brightness then
  110. tmr.alarm(timer_id, 2, tmr.ALARM_SINGLE, fade_in_update)
  111. else
  112. end_pattern()
  113. end
  114. end
  115. function fade_out_update()
  116. local current_brightness = pwm.getduty(led_pin)
  117. current_brightness = current_brightness - 1
  118. if(current_brightness < 0) then
  119. current_brightness = 0
  120. end
  121. pwm.setduty(led_pin, current_brightness)
  122. if current_brightness > 0 then
  123. tmr.alarm(timer_id, 2, tmr.ALARM_SINGLE, fade_out_update)
  124. else
  125. end_pattern()
  126. end
  127. end
  128. function heartbeat_update()
  129. local current_brightness = pwm.getduty(led_pin)
  130. current_brightness = led_max_brightness - current_brightness
  131. pwm.setduty(led_pin, current_brightness)
  132. tmr.alarm(timer_id, heartbeat_times[heartbeat_index], tmr.ALARM_SINGLE, heartbeat_update)
  133. heartbeat_index = heartbeat_index + 1
  134. if heartbeat_index > table.getn(heartbeat_times) then
  135. heartbeat_index = 1
  136. end
  137. --pattern does not end
  138. end
  139. function triple_blink_update()
  140. local current_brightness = pwm.getduty(led_pin)
  141. current_brightness = led_max_brightness - current_brightness
  142. pwm.setduty(led_pin, current_brightness)
  143. tmr.alarm(timer_id, triple_blink_times[triple_blink_index], tmr.ALARM_SINGLE, triple_blink_update)
  144. triple_blink_index = triple_blink_index + 1
  145. if triple_blink_index > table.getn(triple_blink_times) then
  146. end_pattern()
  147. end
  148. end
  149. function end_pattern()
  150. if table.getn(pattern_queue) == 0 then
  151. stop()
  152. else
  153. p = pattern_queue[1]
  154. table.remove(pattern_queue, 1)
  155. current_pattern = STOPPED
  156. tmr.unregister(timer_id)
  157. do_pattern(p)
  158. end
  159. end
  160. function override_enable()
  161. gpio.write(override_pin, gpio.HIGH)
  162. end
  163. function override_disable()
  164. gpio.write(override_pin, gpio.LOW)
  165. end
  166. function invalid_timer()
  167. return (timer_id == nil or timer_id > 6)
  168. end
  169. return {
  170. init = init,
  171. stop = stop,
  172. do_pattern = do_pattern,
  173. HEARTBEAT = HEARTBEAT,
  174. FADE_IN = FADE_IN,
  175. FADE_OUT = FADE_OUT,
  176. TRIPLE_BLINK = TRIPLE_BLINK,
  177. }