led.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. local LED_PIN = 1
  2. local OVERRIDE_PIN = 4
  3. local MAX_BRIGHTNESS = 1023
  4. local HEART_BEAT_IDX = 1
  5. local TRIPLE_BLINK_IDX = 1
  6. local TIMER = TIMERS.led
  7. local IS_FADE_IN = false
  8. local debug_message = debug_message
  9. local gpio = gpio
  10. local pwm = pwm
  11. local tmr = tmr
  12. local table = table
  13. module(...)
  14. function init()
  15. debug_message('led.init')
  16. --handle timer, take exclusive control
  17. tmr.unregister(TIMER)
  18. --init pins
  19. gpio.mode(OVERRIDE_PIN, gpio.OUTPUT)
  20. pwm.setup(LED_PIN, 500, 0)
  21. pwm.start(LED_PIN)
  22. pwm.stop(LED_PIN)
  23. end
  24. function pattern(start_duty, )
  25. function fade_in()
  26. IS_FADE_IN = true
  27. local current_brightness = pwm.getduty(LED_PIN)
  28. current_brightness = current_brightness + 1
  29. if(current_brightness > MAX_BRIGHTNESS) then
  30. current_brightness = MAX_BRIGHTNESS
  31. end
  32. pwm.setduty(LED_PIN, current_brightness)
  33. if current_brightness < MAX_BRIGHTNESS then
  34. tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_in)
  35. else
  36. IS_FADE_IN = false
  37. end
  38. end
  39. function fade_out()
  40. local current_brightness = pwm.getduty(LED_PIN)
  41. current_brightness = current_brightness - 1
  42. if(current_brightness < 0) then
  43. current_brightness = 0
  44. end
  45. pwm.setduty(LED_PIN, current_brightness)
  46. if current_brightness > 0 then
  47. tmr.alarm(TIMER, 2, tmr.ALARM_SINGLE, fade_out)
  48. end
  49. end
  50. function heart_beat()
  51. local intervals = {40, 200, 40, 900} --alternating, millisec
  52. local current_brightness = pwm.getduty(LED_PIN)
  53. current_brightness = MAX_BRIGHTNESS - current_brightness
  54. pwm.setduty(LED_PIN, current_brightness)
  55. --endless
  56. tmr.alarm(TIMER, intervals[HEART_BEAT_IDX], tmr.ALARM_SINGLE, heart_beat)
  57. HEART_BEAT_IDX = HEART_BEAT_IDX + 1
  58. if HEART_BEAT_IDX > table.getn(intervals) then
  59. HEART_BEAT_IDX = 1
  60. end
  61. end
  62. function triple_blink()
  63. local intervals = {100, 20, 100, 20, 100, 20, 100} --alternating, millisec
  64. local current_brightness = pwm.getduty(LED_PIN)
  65. current_brightness = MAX_BRIGHTNESS - current_brightness
  66. pwm.setduty(LED_PIN, current_brightness)
  67. TRIPLE_BLINK_IDX = TRIPLE_BLINK_IDX + 1
  68. if TRIPLE_BLINK_IDX > table.getn(intervals) then
  69. TRIPLE_BLINK_IDX = 1
  70. pwm.setduty(LED_PIN, 0)
  71. else
  72. tmr.alarm(TIMER, intervals[TRIPLE_BLINK_IDX], tmr.ALARM_SINGLE, triple_blink)
  73. end
  74. end
  75. function is_fade_in() return IS_FADE_IN end
  76. init(TIMER)