LEDStatus.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "LEDStatus.h"
  2. // constructor defines which pin the LED is attached to
  3. LEDStatus::LEDStatus(int8_t ledPin) {
  4. // if pin negative, reverse and set inverse on pin outputs
  5. if (ledPin < 0) {
  6. ledPin = -ledPin;
  7. _inverse = true;
  8. } else {
  9. _inverse = false;
  10. }
  11. // set up the pin
  12. _ledPin = ledPin;
  13. pinMode(_ledPin, OUTPUT);
  14. digitalWrite(_ledPin, _pinState(LOW));
  15. _timer = millis();
  16. }
  17. // change pin at runtime
  18. void LEDStatus::changePin(int8_t ledPin) {
  19. bool inverse;
  20. // if pin negative, reverse and set inverse on pin outputs
  21. if (ledPin < 0) {
  22. ledPin = -ledPin;
  23. inverse = true;
  24. } else {
  25. inverse = false;
  26. }
  27. if ((ledPin != _ledPin) && (inverse != _inverse)) {
  28. // make sure old pin is off
  29. digitalWrite(_ledPin, _pinState(LOW));
  30. _ledPin = ledPin;
  31. _inverse = inverse;
  32. // and make sure new pin is also off
  33. pinMode(_ledPin, OUTPUT);
  34. digitalWrite(_ledPin, _pinState(LOW));
  35. }
  36. }
  37. // identify how to flash the LED by mode, continuously until changed
  38. void LEDStatus::continuous(LEDStatus::LEDMode mode) {
  39. uint16_t ledOffMs, ledOnMs;
  40. _modeToTime(mode, ledOffMs, ledOnMs);
  41. continuous(ledOffMs, ledOnMs);
  42. }
  43. // identify how to flash the LED by on/off times (in ms), continuously until changed
  44. void LEDStatus::continuous(uint16_t ledOffMs, uint16_t ledOnMs) {
  45. _continuousOffMs = ledOffMs;
  46. _continuousOnMs = ledOnMs;
  47. _continuousCurrentlyOn = false;
  48. // reset LED to off
  49. if (_ledPin > 0) {
  50. digitalWrite(_ledPin, _pinState(LOW));
  51. }
  52. // restart timer
  53. _timer = millis();
  54. }
  55. // identify a one-shot LED action (overrides continuous until done) by mode
  56. void LEDStatus::oneshot(LEDStatus::LEDMode mode, uint8_t count) {
  57. uint16_t ledOffMs, ledOnMs;
  58. _modeToTime(mode, ledOffMs, ledOnMs);
  59. oneshot(ledOffMs, ledOnMs, count);
  60. }
  61. // identify a one-shot LED action (overrides continuous until done) by times (in ms)
  62. void LEDStatus::oneshot(uint16_t ledOffMs, uint16_t ledOnMs, uint8_t count) {
  63. _oneshotOffMs = ledOffMs;
  64. _oneshotOnMs = ledOnMs;
  65. _oneshotCountRemaining = count;
  66. _oneshotCurrentlyOn = false;
  67. // reset LED to off
  68. if (_ledPin > 0) {
  69. digitalWrite(_ledPin, _pinState(LOW));
  70. }
  71. // restart timer
  72. _timer = millis();
  73. }
  74. // call this function in your loop - it will return quickly after calculating if any changes need to
  75. // be made to the pin to flash the LED
  76. void LEDStatus::LEDStatus::handle() {
  77. // is a pin defined?
  78. if (_ledPin == 0) {
  79. return;
  80. }
  81. // are we currently running a one-shot?
  82. if (_oneshotCountRemaining > 0) {
  83. if (_oneshotCurrentlyOn) {
  84. if ((_timer + _oneshotOnMs) < millis()) {
  85. if (_oneshotOffMs > 0) {
  86. digitalWrite(_ledPin, _pinState(LOW));
  87. }
  88. _oneshotCurrentlyOn = false;
  89. --_oneshotCountRemaining;
  90. if (_oneshotCountRemaining == 0) {
  91. _continuousCurrentlyOn = false;
  92. }
  93. _timer += _oneshotOnMs;
  94. }
  95. } else {
  96. if ((_timer + _oneshotOffMs) < millis()) {
  97. if (_oneshotOnMs > 0) {
  98. digitalWrite(_ledPin, _pinState(HIGH));
  99. }
  100. _oneshotCurrentlyOn = true;
  101. _timer += _oneshotOffMs;
  102. }
  103. }
  104. } else {
  105. // operate using continuous
  106. if (_continuousCurrentlyOn) {
  107. if ((_timer + _continuousOnMs) < millis()) {
  108. if (_continuousOffMs > 0) {
  109. digitalWrite(_ledPin, _pinState(LOW));
  110. }
  111. _continuousCurrentlyOn = false;
  112. _timer += _continuousOnMs;
  113. }
  114. } else {
  115. if ((_timer + _continuousOffMs) < millis()) {
  116. if (_continuousOnMs > 0) {
  117. digitalWrite(_ledPin, _pinState(HIGH));
  118. }
  119. _continuousCurrentlyOn = true;
  120. _timer += _continuousOffMs;
  121. }
  122. }
  123. }
  124. }
  125. // private helper converts mode to on/off times in ms
  126. void LEDStatus::_modeToTime(LEDStatus::LEDMode mode, uint16_t& ledOffMs, uint16_t& ledOnMs) {
  127. switch (mode) {
  128. case LEDMode::Off:
  129. ledOffMs = 1000;
  130. ledOnMs = 0;
  131. break;
  132. case LEDMode::SlowToggle:
  133. ledOffMs = 1000;
  134. ledOnMs = 1000;
  135. break;
  136. case LEDMode::FastToggle:
  137. ledOffMs = 100;
  138. ledOnMs = 100;
  139. break;
  140. case LEDMode::SlowBlip:
  141. ledOffMs = 1500;
  142. ledOnMs = 50;
  143. break;
  144. case LEDMode::FastBlip:
  145. ledOffMs = 333;
  146. ledOnMs = 50;
  147. break;
  148. case LEDMode::On:
  149. ledOffMs = 0;
  150. ledOnMs = 1000;
  151. break;
  152. case LEDMode::Flicker:
  153. ledOffMs = 50;
  154. ledOnMs = 30;
  155. break;
  156. default:
  157. Serial.printf_P(PSTR("LEDStatus::_modeToTime: Uknown LED mode %d\n"), mode);
  158. ledOffMs = 500;
  159. ledOnMs = 2000;
  160. break;
  161. }
  162. }
  163. // private helper to optionally inverse the LED
  164. uint8_t LEDStatus::_pinState(uint8_t val) {
  165. if (_inverse) {
  166. return (val == LOW) ? HIGH : LOW;
  167. }
  168. return val;
  169. }