Osamu Koizumi / LEDUtil

Files at this revision

API Documentation at this revision

Comitter:
coisme
Date:
Fri Jul 31 23:26:36 2015 +0000
Child:
1:220396a0cb8a
Commit message:
First commit.

Changed in this revision

LEDUtil.cpp Show annotated file Show diff for this revision Revisions of this file
LEDUtil.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDUtil.cpp	Fri Jul 31 23:26:36 2015 +0000
@@ -0,0 +1,57 @@
+#include "mbed.h"
+#include "LEDUtil.h"
+
+#define PIN_HIGH  1
+#define PIN_LOW   0
+
+LEDUtil::LEDUtil(PinName targetPinName, LEDUtil::Polarity targetPinPolarity) {
+    this->pinName = targetPinName;
+    this->polarity = targetPinPolarity;
+}    
+
+ 
+void LEDUtil::setLedState(LedState state) {
+    DigitalOut pin(pinName);
+    
+    if (state == LEDUtil::LED_ON) {
+        // turns on the LED
+        pin.write((polarity == LEDUtil::HIGH_ON_LOW_OFF) ? PIN_HIGH : PIN_LOW);
+    } else {
+        // turns off the LED
+        pin.write((polarity == LEDUtil::HIGH_ON_LOW_OFF) ? PIN_LOW : PIN_HIGH);
+    }
+}
+
+void LEDUtil::turnOn() {
+    LEDUtil::setLedState(LEDUtil::LED_ON);
+}
+
+void LEDUtil::turnOff() {
+    LEDUtil::setLedState(LEDUtil::LED_OFF);
+}
+
+bool LEDUtil::isOn() {
+    if (getLedState() == LEDUtil::LED_ON) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool LEDUtil::isOff() {
+    return (!LEDUtil::isOn());
+}
+
+LEDUtil::LedState LEDUtil::getLedState() {
+    DigitalOut pin(pinName);
+    LedState state = LEDUtil::LED_OFF;
+        
+    if (pin.read() == PIN_HIGH) {
+        state = ((polarity == LEDUtil::HIGH_ON_LOW_OFF) ? LEDUtil::LED_ON : LEDUtil::LED_OFF);
+    } else {
+        state = ((polarity == LEDUtil::LOW_ON_HIGH_OFF) ? LEDUtil::LED_OFF : LEDUtil::LED_ON);
+    }
+    
+    return state;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDUtil.h	Fri Jul 31 23:26:36 2015 +0000
@@ -0,0 +1,100 @@
+#ifndef __LEDUTIL_H__
+#define __LEDUTIL_H__
+
+/**
+ * A simple class to handle LED.
+ *
+ * Example (tested on BLENano):
+ * @code
+ * #include "mbed.h"
+ * #include "LEDUtil.h"
+ *
+ * // LED is connected to P0_19 on BLENano and it lights up when the pin state is high.
+ * #define LED_PIN_NAME   P0_19  
+ *
+ * void main() {
+ *     LEDUtil led1(P0_19, LEDUtil::HIGH_ON_LOW_OFF);
+ *   
+ *     while(true) {
+ *         led1.turnOn();
+ *         wait(0.5);
+ *         led1.turnOff();
+ *         wait(0.5);
+ *     }
+ * }
+ * @endcode
+ */
+class LEDUtil {
+
+    public:
+    
+    /** Polarity of LED's configuration. */
+    typedef enum {
+        /** The target LED lights up when the pin level is high. */
+        HIGH_ON_LOW_OFF,
+        /** The target LED lights up when the pin level is low. */
+        LOW_ON_HIGH_OFF  
+    } Polarity;
+    
+    /**
+     * Constructor.
+     *
+     * @param targetPinName pin name of the target pin which is coneccted with the target LED.
+     * @param targetPinPolarity pin polarity of the target. The default value is POLARITY_HIGH_ON_LOW_OFF
+     */
+    LEDUtil(PinName targetPinName, Polarity targetPinPolarity = HIGH_ON_LOW_OFF);
+
+    /**
+     * Turns on the LED.
+     */
+    void turnOn();
+    
+    /**
+     * Turns off the LED.
+     */
+    void turnOff();
+    
+    /**
+     * Returns if the LED is on.
+     * @return Returns true if the LED is on. Otherwise returns false.
+     */
+    bool isOn();
+    
+    /**
+     * Returns if the LED is off.
+     * @return Returns true if the LED is off. Otherwise returns false.
+     */
+    bool isOff(); 
+    
+    /**
+     * Get polarity setting.
+     * @return polarity setting of this LED configuration.
+     */
+    Polarity getPolarity();
+
+
+    private:
+    
+    /* Holds the target LED's pin name. */
+    PinName pinName;
+    
+    /* Holds polarity of the target LED pin. */
+    Polarity polarity;
+      
+    /* State of the LED, i.e. on or off. */
+    typedef enum {LED_ON, LED_OFF} LedState;
+
+    /*
+     * Sets the LED state.
+     * @param state LED state to be set.
+     */
+    void setLedState(LedState state);
+    
+    /*
+     * Gets the LED state.
+     * @return Returns LED state.
+     */
+    LedState getLedState();
+};
+
+#endif
\ No newline at end of file