LEDStatus.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // helper function to convert an LEDMode enum to a string
  126. String LEDStatus::LEDModeToString(LEDStatus::LEDMode mode) {
  127. switch (mode) {
  128. case LEDStatus::LEDMode::Off:
  129. return "Off";
  130. case LEDStatus::LEDMode::SlowToggle:
  131. return "Slow toggle";
  132. case LEDStatus::LEDMode::FastToggle:
  133. return "Fast toggle";
  134. case LEDStatus::LEDMode::SlowBlip:
  135. return "Slow blip";
  136. case LEDStatus::LEDMode::FastBlip:
  137. return "Fast blip";
  138. case LEDStatus::LEDMode::Flicker:
  139. return "Flicker";
  140. case LEDStatus::LEDMode::On:
  141. return "On";
  142. default:
  143. return "Unknown";
  144. }
  145. }
  146. // helper function to convert a string to an LEDMode enum (note, mismatch returns LedMode::Unknown)
  147. LEDStatus::LEDMode LEDStatus::stringToLEDMode(String mode) {
  148. if (mode == "Off")
  149. return LEDStatus::LEDMode::Off;
  150. if (mode == "Slow toggle")
  151. return LEDStatus::LEDMode::SlowToggle;
  152. if (mode == "Fast toggle")
  153. return LEDStatus::LEDMode::FastToggle;
  154. if (mode == "Slow blip")
  155. return LEDStatus::LEDMode::SlowBlip;
  156. if (mode == "Fast blip")
  157. return LEDStatus::LEDMode::FastBlip;
  158. if (mode == "Flicker")
  159. return LEDStatus::LEDMode::Flicker;
  160. if (mode == "On")
  161. return LEDStatus::LEDMode::On;
  162. // unable to match...
  163. return LEDStatus::LEDMode::Unknown;
  164. }
  165. // private helper converts mode to on/off times in ms
  166. void LEDStatus::_modeToTime(LEDStatus::LEDMode mode, uint16_t& ledOffMs, uint16_t& ledOnMs) {
  167. switch (mode) {
  168. case LEDMode::Off:
  169. ledOffMs = 1000;
  170. ledOnMs = 0;
  171. break;
  172. case LEDMode::SlowToggle:
  173. ledOffMs = 1000;
  174. ledOnMs = 1000;
  175. break;
  176. case LEDMode::FastToggle:
  177. ledOffMs = 100;
  178. ledOnMs = 100;
  179. break;
  180. case LEDMode::SlowBlip:
  181. ledOffMs = 1500;
  182. ledOnMs = 50;
  183. break;
  184. case LEDMode::FastBlip:
  185. ledOffMs = 333;
  186. ledOnMs = 50;
  187. break;
  188. case LEDMode::On:
  189. ledOffMs = 0;
  190. ledOnMs = 1000;
  191. break;
  192. case LEDMode::Flicker:
  193. ledOffMs = 50;
  194. ledOnMs = 30;
  195. break;
  196. default:
  197. Serial.printf_P(PSTR("LEDStatus::_modeToTime: Uknown LED mode %d\n"), mode);
  198. ledOffMs = 500;
  199. ledOnMs = 2000;
  200. break;
  201. }
  202. }
  203. // private helper to optionally inverse the LED
  204. uint8_t LEDStatus::_pinState(uint8_t val) {
  205. if (_inverse) {
  206. return (val == LOW) ? HIGH : LOW;
  207. }
  208. return val;
  209. }