This library is used to dirve a led with pluse for a duration and periodic toggle. Version 1.0

Files at this revision

API Documentation at this revision

Comitter:
bborredon
Date:
Thu Dec 24 12:32:33 2015 +0000
Commit message:
Led library version 1.0

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Led.cpp	Thu Dec 24 12:32:33 2015 +0000
@@ -0,0 +1,142 @@
+/***********************************************************
+Author: Bernard Borredon
+Date : 24 decembre 2015
+Version: 1.0
+  - creation.
+************************************************************/
+
+#include "Led.h"
+
+// Class LedBase
+
+/*    LedBase(PinName pin)
+ * Class constructor : create and initialize LedBase instance
+ * @param pin : name of the pin connected to the led (PinName)
+ * @returns none
+ */
+LedBase::LedBase(PinName pin) : _pin(pin) 
+{
+  
+  // Led is off
+  _pin = 0;
+  _isOn = 0;
+}
+
+/*    void toggle(void)
+ * Toggle led 
+ * @param none
+ * @returns none
+ */
+void LedBase::toggle(void)
+{
+  _isOn = !_isOn;
+  _pin = !_pin;
+}
+
+/*    void on(void)
+ * Light on led
+ * @param none
+ * @returns none
+ */
+void LedBase::on(void)
+{
+  if(!_isOn) {
+    _isOn = true;
+    _pin = 1;
+  }
+}
+
+/*    void off(void)
+ * Light off led
+ * @param none
+ * @returns none
+ */
+void LedBase::off(void)
+{
+  _isOn = false;
+  _pin = 0;
+}
+
+/*    bool LedBase::isOn(void)
+ * Get led status (on or off)
+ * @param none
+ * @returns led status (bool)
+ *      false : led is off
+ *      ture  : led is on
+ */
+bool LedBase::isOn(void)
+{
+  return(_isOn);
+}
+
+// Class Led 
+
+/*    Led(PinName pin)
+ * Class constructor : create and initialize Led instance
+ * @param pin : name of the pin connected to the led (PinName)
+ * @returns none
+ */
+Led::Led(PinName pin) : LedBase(pin)
+{
+  
+  // Create LedBase instance
+  _led = new LedBase(pin);
+}
+
+/*    void atTimer(void)
+ * Led on timer : light off the led when called (private fuction)
+ * @param none
+ * @returns none
+ */
+void Led::atTimer(void)
+{
+  _led->off();
+}
+
+/*    void toggleCB(void)
+ * Led toggle timer : toggle the led when called (private function)
+ * @param none
+ * @returns none
+ */
+void Led::toggleCB(void)
+{
+  _led->toggle();
+}
+
+/*    void Led::toggle(uint16_t period, bool mode)
+ * Periodically toggle led
+ * @param period : toggle period in ms (uinit16_t)
+ * @param mode : stop auto toggle if false (bool)
+ * @returns none
+ */
+void Led::toggle(uint16_t period, bool mode)
+{
+  timestamp_t t;
+
+  // Attach togggleCB function to _tToggle Ticker if mode is true and led is off,
+  // overwise detach the function.
+  if(mode && !_led->isOn()) {
+    t = period * 1000;
+    _tToggle.attach_us(this, &Led::toggleCB,t);
+  }
+  else {
+    _tToggle.detach();
+  }
+}
+
+/*    void pulse(uint16_t time)
+ * Light on led for a duration
+ * @param time : light on duration in ms (uint16_t)
+ * @returns none
+ */
+void Led::pulse(uint16_t time)
+{
+  timestamp_t t;
+  
+  // Attach atTimer function to _tPulse Timer if led is off.
+  if(!_led->isOn()) {
+    _led->on();
+    t = time * 1000;
+    _tPulse.attach_us(this,&Led::atTimer,t);
+  }
+}   
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Led.h	Thu Dec 24 12:32:33 2015 +0000
@@ -0,0 +1,125 @@
+#ifndef __Led_H__
+#define __Led_H__
+
+/***********************************************************
+Author: Bernard Borredon
+Date : 24 decembre 2015
+Version: 1.0
+  - creation.
+************************************************************/
+
+#include "mbed.h"
+
+/** Led class
+ *  Used to drive a led : on, on for a duration, off, toggle, toggle with a period.
+ *
+ * @code
+ * #include "Led.h"
+ *
+ * Led led1(LED1);  // LPC1768 LED1
+ * Led led2(LED2);  // LPC1768 LED2
+ * Led led3(LED3);  // LPC1768 LED3
+ * Led led4(LED4);  // LPC1768 LED4
+ *
+ * int main()
+ * {
+ *   int32_t count = 0;
+  
+ *   // Light on LED1
+ *   led1.on();
+ * 
+ *   // Toggle LED2 with a 2000ms period
+ *   led2.toggle(2000);
+ *
+ *   // Toggle LED3 with a 4000ms period
+ *   led3.toggle(4000,true);
+ * 
+ *  while(1) {
+ *             // Pulse LED4 for 1000ms each 10s
+ *              led4.pulse(1000);
+ *              count++;
+ *              wait(10.0);
+ *              
+ *              // After 60s stop toggle LED2
+ *              if(count == 6)
+ *                led2.toggle(0,false);
+ *   }
+ *   return(0);
+ * }
+ * @endcode
+ */
+
+/** LedBase class
+ *  Used to drive a led : on, off, toggle.
+ *
+ */
+
+class LedBase {
+    public :
+        /** Class constructor : create and initialize LedBase instance
+         * @param pin : name of the pin connected to the led (PinName)
+         * @returns none
+         */
+        LedBase(PinName pin);
+        
+        /** Toggle led 
+         * @param none
+         * @returns none
+         */
+        void toggle(void);
+        
+        /** Light on led
+         * @param none
+         * @returns none
+         */
+        void on(void);
+        
+        /** Light off led
+         * @param none
+         * @returns none
+         */
+        void off(void);
+        
+        /** Get led status (on or off)
+         * @param none
+         * @returns led status (bool)
+         *      false : led is off
+         *      ture  : led is on
+         */
+        bool isOn(void);
+        
+    protected :
+        DigitalOut _pin;
+        bool _isOn;
+};
+ 
+class Led : public LedBase {
+    public :
+        /** Class constructor : create and initialize Led instance
+         * @param pin : name of the pin connected to the led (PinName)
+         * @returns none
+         */
+        Led(PinName pin);
+         
+        /** Periodically toggle led
+         * @param period : toggle period in ms (uinit16_t)
+         * @param mode : stop auto toggle if false (bool)
+         * @returns none
+         */
+        void toggle(uint16_t period, bool mode = true);
+        
+        /** Light on led for a duration
+         * @param time : light on duration in ms (uint16_t)
+         * @returns none
+         */
+        virtual void pulse(uint16_t time);
+    
+    private :
+        LedBase *_led;
+        Timeout _tPulse;
+        Ticker _tToggle;
+        void atTimer(void);
+        void toggleCB(void);
+};
+
+#endif