Osamu Koizumi / LEDUtil
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LEDUtil.cpp Source File

LEDUtil.cpp

00001 #include "mbed.h"
00002 #include "LEDUtil.h"
00003 
00004 #define PIN_HIGH  1
00005 #define PIN_LOW   0
00006 
00007 LEDUtil::LEDUtil(PinName targetPinName, LEDUtil::Polarity targetPinPolarity)
00008 {
00009     this->pinName = targetPinName;
00010     this->polarity = targetPinPolarity;
00011 }
00012 
00013 
00014 void LEDUtil::setLedState(LedState state)
00015 {
00016     DigitalOut pin(pinName);
00017 
00018     if (state == LEDUtil::LED_ON) {
00019         // turns on the LED
00020         pin.write((polarity == LEDUtil::HIGH_ON_LOW_OFF) ? PIN_HIGH : PIN_LOW);
00021     } else {
00022         // turns off the LED
00023         pin.write((polarity == LEDUtil::HIGH_ON_LOW_OFF) ? PIN_LOW : PIN_HIGH);
00024     }
00025 }
00026 
00027 void LEDUtil::turnOn()
00028 {
00029     LEDUtil::setLedState(LEDUtil::LED_ON);
00030 }
00031 
00032 void LEDUtil::turnOff()
00033 {
00034     LEDUtil::setLedState(LEDUtil::LED_OFF);
00035 }
00036 
00037 bool LEDUtil::isOn()
00038 {
00039     if (getLedState() == LEDUtil::LED_ON) {
00040         return true;
00041     } else {
00042         return false;
00043     }
00044 }
00045 
00046 bool LEDUtil::isOff()
00047 {
00048     return (!LEDUtil::isOn());
00049 }
00050 
00051 LEDUtil::LedState LEDUtil::getLedState()
00052 {
00053     DigitalOut pin(pinName);
00054     LedState state = LEDUtil::LED_OFF;
00055 
00056     if (pin.read() == PIN_HIGH) {
00057         state = ((polarity == LEDUtil::HIGH_ON_LOW_OFF) ? LEDUtil::LED_ON : LEDUtil::LED_OFF);
00058     } else {
00059         state = ((polarity == LEDUtil::LOW_ON_HIGH_OFF) ? LEDUtil::LED_OFF : LEDUtil::LED_ON);
00060     }
00061 
00062     return state;
00063 }
00064