led.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * led.cpp
  3. *
  4. * Created on: Nov 9, 2015
  5. * Author: Philipp Hinz
  6. */
  7. #include <wiringPi.h>
  8. #include <stdio.h>
  9. #include "global.h"
  10. #include "timer.h"
  11. #include "led.h"
  12. timer blinkTimer(&ledBlinkHandler); /**< timer for LED blinking */
  13. bool ledBlink[3]; /**< bool flag for LED blinking */
  14. bool ledBlinkO[3]; /**< bool flag for one time LED blinking */
  15. /**
  16. * Initializes the LEDs on the CAN Controller board
  17. */
  18. void initLed(void) {
  19. pinMode(LED_RED_PIN, OUTPUT);
  20. pinMode(LED_YELLOW_PIN, OUTPUT);
  21. pinMode(LED_GREEN_PIN, OUTPUT);
  22. ledOff(LED_RED);
  23. ledOff(LED_YELLOW);
  24. ledOff(LED_GREEN);
  25. blinkTimer.setDivider(10);
  26. blinkTimer.start();
  27. for (int i = 0; i < 3; i++) {
  28. ledBlink[i] = false;
  29. }
  30. }
  31. /**
  32. * Internal function that returns the WiringPi pin to the given LED
  33. * @param led integer for one of the 3 LEDs
  34. * @return WiringPi pin
  35. */
  36. int ledGetPin(int led) {
  37. switch (led) {
  38. case LED_RED:
  39. return LED_RED_PIN;
  40. break;
  41. case LED_YELLOW:
  42. return LED_YELLOW_PIN;
  43. break;
  44. case LED_GREEN:
  45. return LED_GREEN_PIN;
  46. break;
  47. default:
  48. break;
  49. }
  50. return -1;
  51. }
  52. /**
  53. * Set the LED pin to a value.
  54. * @param led integer for one of the 3 LEDs
  55. * @param value HIGH or LOW
  56. */
  57. void ledSet(int led, int value) {
  58. digitalWrite(ledGetPin(led), value);
  59. }
  60. /**
  61. * Turn the LED on
  62. * @param led integer for one of the 3 LEDs
  63. */
  64. void ledOn(int led) {
  65. ledSet(led, LOW);
  66. }
  67. /**
  68. * Turn the LED off (including blinking)
  69. * @param led integer for one of the 3 LEDs
  70. */
  71. void ledOff(int led) {
  72. ledBlinkOff(led);
  73. ledSet(led, HIGH);
  74. }
  75. /**
  76. * Toggle the LED
  77. * @param led integer for one of the 3 LEDs
  78. */
  79. void ledToggle(int led) {
  80. int pin = ledGetPin(led);
  81. if (digitalRead(pin) == HIGH)
  82. digitalWrite(pin, LOW);
  83. else
  84. digitalWrite(pin, HIGH);
  85. }
  86. /**
  87. * Enable the LED blinking
  88. * @param led integer for one of the 3 LEDs
  89. */
  90. void ledBlinkOn(int led) {
  91. ledBlink[led] = true;
  92. ledBlinkO[led] = false;
  93. }
  94. /**
  95. * Enable the LED blinking only once
  96. * @param led integer for one of the 3 LEDs
  97. */
  98. void ledBlinkOnce(int led) {
  99. ledBlink[led] = true;
  100. ledBlinkO[led] = true;
  101. }
  102. /**
  103. * Disable the LED blinking
  104. * @param led integer for one of the 3 LEDs
  105. */
  106. void ledBlinkOff(int led) {
  107. ledBlink[led] = false;
  108. ledBlinkO[led] = false;
  109. }
  110. /**
  111. * Function for LED blinking, called by a timer
  112. */
  113. void ledBlinkHandler(void) {
  114. for (int i = 0; i < 3; i++) {
  115. if (ledBlink[i] == true) {
  116. ledToggle(i);
  117. if (digitalRead(ledGetPin(i)) == HIGH && ledBlinkO[i])
  118. ledBlink[i] = false;
  119. }
  120. }
  121. }