manage a led (put on/off, flash...)

Files at this revision

API Documentation at this revision

Comitter:
us191
Date:
Mon May 06 08:23:41 2013 +0000
Commit message:
generate API

Changed in this revision

Led.cpp Show annotated file Show diff for this revision Revisions of this file
Led.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 77714f74d105 Led.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Led.cpp	Mon May 06 08:23:41 2013 +0000
@@ -0,0 +1,85 @@
+#include "Led.h"
+
+/********************************************************************************************************
+                                         public methods
+ ********************************************************************************************************/
+
+/* Create a Led interface */
+Led::Led(PinName pin) : _pin(pin) {
+    // default the output to 0
+    _pin = 0;
+    // delay to flip pin (flash led)
+    _flashDelay = 0.2;
+}
+
+/* Destructor */
+Led::~Led()
+{
+}
+
+/* put on LED */
+void Led::on(void) {
+    // stop flash LED if forgot
+    this->stopFlash();
+    _pin = 1;
+}
+
+/* put off LED */
+void Led::off(void) {
+    // stop flash LED
+    this->stopFlash();
+    _pin = 0;
+}
+
+/* launch LED flash */
+void Led::flash(void) {
+    // stop flash LED if forgot
+    this->stopFlash();
+    // Attach a function to be called by the Ticker, specifiying the interval delay in seconds.
+    // attach flipPin : change pin status each _flashDelay seconds (flash led)
+    _ticker.attach(this, &Led::flipPin, _flashDelay);
+}
+
+/* get pin status 
+ * 
+ * @return  _pin
+ */
+int Led::read(void) {
+    return _pin;
+}
+
+/* get flash delay value 
+ * 
+ * @return _flashDelay
+ */
+float Led::getFlashDelay(void) const {
+    return _flashDelay;
+}
+
+/* change flash delay value to delay 
+ * 
+ * @param delay     new delay to flash LED
+ */
+void Led::setFlashDelay(float delay) {
+    _flashDelay = delay;
+}
+
+/********************************************************************************************************
+                                         private methods
+ ********************************************************************************************************/
+
+// flipPin method call by ticker
+// flip pin status
+// use by flash()
+void Led::flipPin(void) {
+    // flash LED
+    _pin = !_pin;
+}
+
+// detach method flipPin
+// use by on(), off() and flash()
+void Led::stopFlash(void) {
+    // Detach the function
+    _ticker.detach();
+    _pin = 0;
+}
\ No newline at end of file
diff -r 000000000000 -r 77714f74d105 Led.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Led.h	Mon May 06 08:23:41 2013 +0000
@@ -0,0 +1,110 @@
+#ifndef Led_H
+#define Led_H
+
+#include "mbed.h"
+
+/**
+ *  This class manage LED.
+ *  She permit to put on/off LED, flash LED, get/set delay to flash and read pin status.
+ *  
+ * @code                        
+ *
+ * #include "Led.h"
+ *
+ * Led led (p12);
+ *
+ * int main() {
+ *
+ *  led.on();                               // put on LED
+ *  wait(2);                                // wait 2 seconds (operations)
+ *  
+ *  led.flash();                            // flash LED (flash delay = 0.2);
+ *  wait(2);                                // wait 2 seconds
+ *  
+ *  float delay = led.getFlashDelay();      // delay = flashDelay
+ *  led.off();                              // put off LED
+ *  wait(2);                                // wait 2 seconds (operations)
+ *  
+ *  delay *= 2;                             // delay = delay * 2
+ *  led.setFlashDelay(delay);               // flashDelay = delay
+ *  led.flash();                            // flash LED (flashDelay = 0.4);
+ *  wait(2);                                // wait 2 seconds (operations)
+ *  
+ *  led.on();                               // put on LED
+ *  int pinStatus = int(led);               // pinStatus = 1 if LED is lighted, else 0
+ *  wait(1);                                // wait 1 second (operations)
+ *  
+ *  if (pinStatus)  led.flash();            // if LED is lighted, flash LED
+ *  wait(2);                                // wait 2 seconds
+ *  led.off();                              // put off LED
+ *  pinStatus = int(led);                   // pinStatus = 1 if LED is lighted, else 0
+ *  if (pinStatus)  led.flash();            // if LED is lighted, flash LED
+ *  wait(2);                                // wait 2 seconds (operations)
+ *  
+ *  led.off();                              // put off LED
+ *  
+ *  return 0;
+ * }
+ * @endcode
+ */
+
+class Led {
+
+
+private:
+    Ticker     _ticker; // use for flash led
+    DigitalOut _pin;
+    float      _flashDelay;
+
+    
+public:
+    
+    /** Create a Led interface */
+    Led(PinName pin);
+
+    /** Destructor */
+    ~Led(void);
+
+    /** put on LED */
+    void on(void);
+    
+    /** put off LED */
+    void off(void);
+    
+    /** launch LED flash */
+    void flash(void);
+        
+    /** get pin status 
+     * 
+     * @return  _pin
+     */
+    int read(void);
+
+    /** A shorthand for read() */
+    operator int() { return read(); }
+
+    /** get flash delay value 
+     * 
+     * @return _flashDelay
+     */
+    float getFlashDelay(void) const;
+    
+    /** change flash delay value to delay 
+     * 
+     * @param delay     new delay to flash LED
+     */
+    void setFlashDelay(float delay);
+    
+    
+private:
+    
+    // method to change pin status
+    // use by flash()
+    void flipPin(void);
+
+    // stop LED flash
+    // use by on(), off() and flash()
+    void stopFlash(void);
+};
+
+#endif // Led_H
\ No newline at end of file