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 #ifndef Led_H
us191 0:77714f74d105 2 #define Led_H
us191 0:77714f74d105 3
us191 0:77714f74d105 4 #include "mbed.h"
us191 0:77714f74d105 5
us191 0:77714f74d105 6 /**
us191 0:77714f74d105 7 * This class manage LED.
us191 0:77714f74d105 8 * She permit to put on/off LED, flash LED, get/set delay to flash and read pin status.
us191 0:77714f74d105 9 *
us191 0:77714f74d105 10 * @code
us191 0:77714f74d105 11 *
us191 0:77714f74d105 12 * #include "Led.h"
us191 0:77714f74d105 13 *
us191 0:77714f74d105 14 * Led led (p12);
us191 0:77714f74d105 15 *
us191 0:77714f74d105 16 * int main() {
us191 0:77714f74d105 17 *
us191 0:77714f74d105 18 * led.on(); // put on LED
us191 0:77714f74d105 19 * wait(2); // wait 2 seconds (operations)
us191 0:77714f74d105 20 *
us191 0:77714f74d105 21 * led.flash(); // flash LED (flash delay = 0.2);
us191 0:77714f74d105 22 * wait(2); // wait 2 seconds
us191 0:77714f74d105 23 *
us191 0:77714f74d105 24 * float delay = led.getFlashDelay(); // delay = flashDelay
us191 0:77714f74d105 25 * led.off(); // put off LED
us191 0:77714f74d105 26 * wait(2); // wait 2 seconds (operations)
us191 0:77714f74d105 27 *
us191 0:77714f74d105 28 * delay *= 2; // delay = delay * 2
us191 0:77714f74d105 29 * led.setFlashDelay(delay); // flashDelay = delay
us191 0:77714f74d105 30 * led.flash(); // flash LED (flashDelay = 0.4);
us191 0:77714f74d105 31 * wait(2); // wait 2 seconds (operations)
us191 0:77714f74d105 32 *
us191 0:77714f74d105 33 * led.on(); // put on LED
us191 0:77714f74d105 34 * int pinStatus = int(led); // pinStatus = 1 if LED is lighted, else 0
us191 0:77714f74d105 35 * wait(1); // wait 1 second (operations)
us191 0:77714f74d105 36 *
us191 0:77714f74d105 37 * if (pinStatus) led.flash(); // if LED is lighted, flash LED
us191 0:77714f74d105 38 * wait(2); // wait 2 seconds
us191 0:77714f74d105 39 * led.off(); // put off LED
us191 0:77714f74d105 40 * pinStatus = int(led); // pinStatus = 1 if LED is lighted, else 0
us191 0:77714f74d105 41 * if (pinStatus) led.flash(); // if LED is lighted, flash LED
us191 0:77714f74d105 42 * wait(2); // wait 2 seconds (operations)
us191 0:77714f74d105 43 *
us191 0:77714f74d105 44 * led.off(); // put off LED
us191 0:77714f74d105 45 *
us191 0:77714f74d105 46 * return 0;
us191 0:77714f74d105 47 * }
us191 0:77714f74d105 48 * @endcode
us191 0:77714f74d105 49 */
us191 0:77714f74d105 50
us191 0:77714f74d105 51 class Led {
us191 0:77714f74d105 52
us191 0:77714f74d105 53
us191 0:77714f74d105 54 private:
us191 0:77714f74d105 55 Ticker _ticker; // use for flash led
us191 0:77714f74d105 56 DigitalOut _pin;
us191 0:77714f74d105 57 float _flashDelay;
us191 0:77714f74d105 58
us191 0:77714f74d105 59
us191 0:77714f74d105 60 public:
us191 0:77714f74d105 61
us191 0:77714f74d105 62 /** Create a Led interface */
us191 0:77714f74d105 63 Led(PinName pin);
us191 0:77714f74d105 64
us191 0:77714f74d105 65 /** Destructor */
us191 0:77714f74d105 66 ~Led(void);
us191 0:77714f74d105 67
us191 0:77714f74d105 68 /** put on LED */
us191 0:77714f74d105 69 void on(void);
us191 0:77714f74d105 70
us191 0:77714f74d105 71 /** put off LED */
us191 0:77714f74d105 72 void off(void);
us191 0:77714f74d105 73
us191 0:77714f74d105 74 /** launch LED flash */
us191 0:77714f74d105 75 void flash(void);
us191 0:77714f74d105 76
us191 0:77714f74d105 77 /** get pin status
us191 0:77714f74d105 78 *
us191 0:77714f74d105 79 * @return _pin
us191 0:77714f74d105 80 */
us191 0:77714f74d105 81 int read(void);
us191 0:77714f74d105 82
us191 0:77714f74d105 83 /** A shorthand for read() */
us191 0:77714f74d105 84 operator int() { return read(); }
us191 0:77714f74d105 85
us191 0:77714f74d105 86 /** get flash delay value
us191 0:77714f74d105 87 *
us191 0:77714f74d105 88 * @return _flashDelay
us191 0:77714f74d105 89 */
us191 0:77714f74d105 90 float getFlashDelay(void) const;
us191 0:77714f74d105 91
us191 0:77714f74d105 92 /** change flash delay value to delay
us191 0:77714f74d105 93 *
us191 0:77714f74d105 94 * @param delay new delay to flash LED
us191 0:77714f74d105 95 */
us191 0:77714f74d105 96 void setFlashDelay(float delay);
us191 0:77714f74d105 97
us191 0:77714f74d105 98
us191 0:77714f74d105 99 private:
us191 0:77714f74d105 100
us191 0:77714f74d105 101 // method to change pin status
us191 0:77714f74d105 102 // use by flash()
us191 0:77714f74d105 103 void flipPin(void);
us191 0:77714f74d105 104
us191 0:77714f74d105 105 // stop LED flash
us191 0:77714f74d105 106 // use by on(), off() and flash()
us191 0:77714f74d105 107 void stopFlash(void);
us191 0:77714f74d105 108 };
us191 0:77714f74d105 109
us191 0:77714f74d105 110 #endif // Led_H