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

Committer:
us191
Date:
Mon May 06 08:23:41 2013 +0000
Revision:
0:77714f74d105
generate API

Who changed what in which revision?

UserRevisionLine numberNew contents of line
us191 0:77714f74d105 1 #include "Led.h"
us191 0:77714f74d105 2
us191 0:77714f74d105 3 /********************************************************************************************************
us191 0:77714f74d105 4 public methods
us191 0:77714f74d105 5 ********************************************************************************************************/
us191 0:77714f74d105 6
us191 0:77714f74d105 7 /* Create a Led interface */
us191 0:77714f74d105 8 Led::Led(PinName pin) : _pin(pin) {
us191 0:77714f74d105 9 // default the output to 0
us191 0:77714f74d105 10 _pin = 0;
us191 0:77714f74d105 11 // delay to flip pin (flash led)
us191 0:77714f74d105 12 _flashDelay = 0.2;
us191 0:77714f74d105 13 }
us191 0:77714f74d105 14
us191 0:77714f74d105 15 /* Destructor */
us191 0:77714f74d105 16 Led::~Led()
us191 0:77714f74d105 17 {
us191 0:77714f74d105 18 }
us191 0:77714f74d105 19
us191 0:77714f74d105 20 /* put on LED */
us191 0:77714f74d105 21 void Led::on(void) {
us191 0:77714f74d105 22 // stop flash LED if forgot
us191 0:77714f74d105 23 this->stopFlash();
us191 0:77714f74d105 24 _pin = 1;
us191 0:77714f74d105 25 }
us191 0:77714f74d105 26
us191 0:77714f74d105 27 /* put off LED */
us191 0:77714f74d105 28 void Led::off(void) {
us191 0:77714f74d105 29 // stop flash LED
us191 0:77714f74d105 30 this->stopFlash();
us191 0:77714f74d105 31 _pin = 0;
us191 0:77714f74d105 32 }
us191 0:77714f74d105 33
us191 0:77714f74d105 34 /* launch LED flash */
us191 0:77714f74d105 35 void Led::flash(void) {
us191 0:77714f74d105 36 // stop flash LED if forgot
us191 0:77714f74d105 37 this->stopFlash();
us191 0:77714f74d105 38 // Attach a function to be called by the Ticker, specifiying the interval delay in seconds.
us191 0:77714f74d105 39 // attach flipPin : change pin status each _flashDelay seconds (flash led)
us191 0:77714f74d105 40 _ticker.attach(this, &Led::flipPin, _flashDelay);
us191 0:77714f74d105 41 }
us191 0:77714f74d105 42
us191 0:77714f74d105 43 /* get pin status
us191 0:77714f74d105 44 *
us191 0:77714f74d105 45 * @return _pin
us191 0:77714f74d105 46 */
us191 0:77714f74d105 47 int Led::read(void) {
us191 0:77714f74d105 48 return _pin;
us191 0:77714f74d105 49 }
us191 0:77714f74d105 50
us191 0:77714f74d105 51 /* get flash delay value
us191 0:77714f74d105 52 *
us191 0:77714f74d105 53 * @return _flashDelay
us191 0:77714f74d105 54 */
us191 0:77714f74d105 55 float Led::getFlashDelay(void) const {
us191 0:77714f74d105 56 return _flashDelay;
us191 0:77714f74d105 57 }
us191 0:77714f74d105 58
us191 0:77714f74d105 59 /* change flash delay value to delay
us191 0:77714f74d105 60 *
us191 0:77714f74d105 61 * @param delay new delay to flash LED
us191 0:77714f74d105 62 */
us191 0:77714f74d105 63 void Led::setFlashDelay(float delay) {
us191 0:77714f74d105 64 _flashDelay = delay;
us191 0:77714f74d105 65 }
us191 0:77714f74d105 66
us191 0:77714f74d105 67 /********************************************************************************************************
us191 0:77714f74d105 68 private methods
us191 0:77714f74d105 69 ********************************************************************************************************/
us191 0:77714f74d105 70
us191 0:77714f74d105 71 // flipPin method call by ticker
us191 0:77714f74d105 72 // flip pin status
us191 0:77714f74d105 73 // use by flash()
us191 0:77714f74d105 74 void Led::flipPin(void) {
us191 0:77714f74d105 75 // flash LED
us191 0:77714f74d105 76 _pin = !_pin;
us191 0:77714f74d105 77 }
us191 0:77714f74d105 78
us191 0:77714f74d105 79 // detach method flipPin
us191 0:77714f74d105 80 // use by on(), off() and flash()
us191 0:77714f74d105 81 void Led::stopFlash(void) {
us191 0:77714f74d105 82 // Detach the function
us191 0:77714f74d105 83 _ticker.detach();
us191 0:77714f74d105 84 _pin = 0;
us191 0:77714f74d105 85 }