123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- /*
- * led.cpp
- *
- * Created on: Nov 9, 2015
- * Author: Philipp Hinz
- */
- #include <wiringPi.h>
- #include <stdio.h>
- #include "global.h"
- #include "timer.h"
- #include "led.h"
- timer blinkTimer(&ledBlinkHandler); /**< timer for LED blinking */
- bool ledBlink[3]; /**< bool flag for LED blinking */
- bool ledBlinkO[3]; /**< bool flag for one time LED blinking */
- /**
- * Initializes the LEDs on the CAN Controller board
- */
- void initLed(void) {
- pinMode(LED_RED_PIN, OUTPUT);
- pinMode(LED_YELLOW_PIN, OUTPUT);
- pinMode(LED_GREEN_PIN, OUTPUT);
- ledOff(LED_RED);
- ledOff(LED_YELLOW);
- ledOff(LED_GREEN);
- blinkTimer.setDivider(10);
- blinkTimer.start();
- for (int i = 0; i < 3; i++) {
- ledBlink[i] = false;
- }
- }
- /**
- * Internal function that returns the WiringPi pin to the given LED
- * @param led integer for one of the 3 LEDs
- * @return WiringPi pin
- */
- int ledGetPin(int led) {
- switch (led) {
- case LED_RED:
- return LED_RED_PIN;
- break;
- case LED_YELLOW:
- return LED_YELLOW_PIN;
- break;
- case LED_GREEN:
- return LED_GREEN_PIN;
- break;
- default:
- break;
- }
- return -1;
- }
- /**
- * Set the LED pin to a value.
- * @param led integer for one of the 3 LEDs
- * @param value HIGH or LOW
- */
- void ledSet(int led, int value) {
- digitalWrite(ledGetPin(led), value);
- }
- /**
- * Turn the LED on
- * @param led integer for one of the 3 LEDs
- */
- void ledOn(int led) {
- ledSet(led, LOW);
- }
- /**
- * Turn the LED off (including blinking)
- * @param led integer for one of the 3 LEDs
- */
- void ledOff(int led) {
- ledBlinkOff(led);
- ledSet(led, HIGH);
- }
- /**
- * Toggle the LED
- * @param led integer for one of the 3 LEDs
- */
- void ledToggle(int led) {
- int pin = ledGetPin(led);
- if (digitalRead(pin) == HIGH)
- digitalWrite(pin, LOW);
- else
- digitalWrite(pin, HIGH);
- }
- /**
- * Enable the LED blinking
- * @param led integer for one of the 3 LEDs
- */
- void ledBlinkOn(int led) {
- ledBlink[led] = true;
- ledBlinkO[led] = false;
- }
- /**
- * Enable the LED blinking only once
- * @param led integer for one of the 3 LEDs
- */
- void ledBlinkOnce(int led) {
- ledBlink[led] = true;
- ledBlinkO[led] = true;
- }
- /**
- * Disable the LED blinking
- * @param led integer for one of the 3 LEDs
- */
- void ledBlinkOff(int led) {
- ledBlink[led] = false;
- ledBlinkO[led] = false;
- }
- /**
- * Function for LED blinking, called by a timer
- */
- void ledBlinkHandler(void) {
- for (int i = 0; i < 3; i++) {
- if (ledBlink[i] == true) {
- ledToggle(i);
- if (digitalRead(ledGetPin(i)) == HIGH && ledBlinkO[i])
- ledBlink[i] = false;
- }
- }
- }
|