led.lua 4.6 KB

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