The library can flash an LED or DigitalOut pin at 50% duty using a Ticker interrupt triggered toggle function. This library does NOT use wait() so that it can flash a pin while more code is being executed. Instead, it relies on Ticker generated interrupts. There is only one parameter required, the on/off time of the pin in seconds (i.e. 0.5 means 0.5 seconds on and 0.5 seconds off).

Committer:
PennElectric
Date:
Sat Dec 22 21:07:34 2012 +0000
Revision:
0:32fa44559a40
Child:
1:0764e2d49518
Library completed December 2012

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PennElectric 0:32fa44559a40 1 /* Copyright (c) <2012> <P. Patel>, MIT License
PennElectric 0:32fa44559a40 2 *
PennElectric 0:32fa44559a40 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
PennElectric 0:32fa44559a40 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
PennElectric 0:32fa44559a40 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
PennElectric 0:32fa44559a40 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
PennElectric 0:32fa44559a40 7 * furnished to do so, subject to the following conditions:
PennElectric 0:32fa44559a40 8 *
PennElectric 0:32fa44559a40 9 * The above copyright notice and this permission notice shall be included in all copies or
PennElectric 0:32fa44559a40 10 * substantial portions of the Software.
PennElectric 0:32fa44559a40 11 *
PennElectric 0:32fa44559a40 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
PennElectric 0:32fa44559a40 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
PennElectric 0:32fa44559a40 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
PennElectric 0:32fa44559a40 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
PennElectric 0:32fa44559a40 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
PennElectric 0:32fa44559a40 17 */
PennElectric 0:32fa44559a40 18
PennElectric 0:32fa44559a40 19 // ----------------------------- LED Ticker Interrupt Flasher -----------------------------------
PennElectric 0:32fa44559a40 20
PennElectric 0:32fa44559a40 21 #ifndef ToggleFlasher
PennElectric 0:32fa44559a40 22 #define ToggleFlasher
PennElectric 0:32fa44559a40 23
PennElectric 0:32fa44559a40 24 #include "mbed.h"
PennElectric 0:32fa44559a40 25
PennElectric 0:32fa44559a40 26 /** LED Toggle-Flashing (50% duty) library based on Ticker interrupts instead of wait
PennElectric 0:32fa44559a40 27 *
PennElectric 0:32fa44559a40 28 * Example
PennElectric 0:32fa44559a40 29 * @code
PennElectric 0:32fa44559a40 30 * ToggleFlasher myflasher(p20, 1.5); // A flasher object attached to pin 20 with 1.5 second timing
PennElectric 0:32fa44559a40 31 *
PennElectric 0:32fa44559a40 32 * int main() {
PennElectric 0:32fa44559a40 33 * while (1) {
PennElectric 0:32fa44559a40 34 * myflasher.enable(); // Turn on flasher
PennElectric 0:32fa44559a40 35 *
PennElectric 0:32fa44559a40 36 * wait(3); // More code, simulated by wait
PennElectric 0:32fa44559a40 37 * myflasher.disableTo(1); // Hold pin high
PennElectric 0:32fa44559a40 38 myflasher.setTime(2); // Set new timing
PennElectric 0:32fa44559a40 39 wait(3);
PennElectric 0:32fa44559a40 40 myflasher.enable(); // Resume flashing
PennElectric 0:32fa44559a40 41 wait(3);
PennElectric 0:32fa44559a40 42 myflasher = 0; // Hold pin low (shorthand for disableTo(int state))
PennElectric 0:32fa44559a40 43 * }
PennElectric 0:32fa44559a40 44 * }
PennElectric 0:32fa44559a40 45 * @endcode
PennElectric 0:32fa44559a40 46 */
PennElectric 0:32fa44559a40 47 class ToggleFlash{
PennElectric 0:32fa44559a40 48 public:
PennElectric 0:32fa44559a40 49 ToggleFlash(PinName pin);
PennElectric 0:32fa44559a40 50 /** Create a ToggleFlasher object connected to a pin and set the on-off time
PennElectric 0:32fa44559a40 51 * @param pin The pin to which the ToggleFlasher is attached
PennElectric 0:32fa44559a40 52 * @param time The specified on-off time in seconds
PennElectric 0:32fa44559a40 53 */
PennElectric 0:32fa44559a40 54 ToggleFlash(PinName pin, float time);
PennElectric 0:32fa44559a40 55 /** Set the on-off time ex. 1.5 means on for 1.5 seconds off for 1.5 seconds
PennElectric 0:32fa44559a40 56 * @param time The specified on-off time in seconds
PennElectric 0:32fa44559a40 57 */
PennElectric 0:32fa44559a40 58 void setTime(float time);
PennElectric 0:32fa44559a40 59 /** Retrieve the current time setting of the flasher object
PennElectric 0:32fa44559a40 60 * @returns The current on-off time of the ToggleFlash object
PennElectric 0:32fa44559a40 61 */
PennElectric 0:32fa44559a40 62 float getTime();
PennElectric 0:32fa44559a40 63 /** Enable the flasher to start toggling pin
PennElectric 0:32fa44559a40 64 *
PennElectric 0:32fa44559a40 65 */
PennElectric 0:32fa44559a40 66 void enable();
PennElectric 0:32fa44559a40 67 /** Disable the flasher and give the pin a constant state
PennElectric 0:32fa44559a40 68 * @param state 1 or 0 for constant on or constant off
PennElectric 0:32fa44559a40 69 */
PennElectric 0:32fa44559a40 70 void disableTo(int state);
PennElectric 0:32fa44559a40 71 /** Return the current status of the flasher
PennElectric 0:32fa44559a40 72 * @returns 1 or 0 for flasher enabled or flasher disabled
PennElectric 0:32fa44559a40 73 */
PennElectric 0:32fa44559a40 74 int readFlasher();
PennElectric 0:32fa44559a40 75 /** Return the current state of the pin
PennElectric 0:32fa44559a40 76 * @returns 1 or 0 for currently set high or low
PennElectric 0:32fa44559a40 77 */
PennElectric 0:32fa44559a40 78 int read();
PennElectric 0:32fa44559a40 79 #ifdef MBED_OPERATORS
PennElectric 0:32fa44559a40 80 /** An operator shorthand for disableTo(int state) to give the pin a constant state
PennElectric 0:32fa44559a40 81 */
PennElectric 0:32fa44559a40 82 ToggleFlash& operator= (int state) {
PennElectric 0:32fa44559a40 83 disableTo(state);
PennElectric 0:32fa44559a40 84 return *this;
PennElectric 0:32fa44559a40 85 }
PennElectric 0:32fa44559a40 86 /** An operator shorthand for read() to read pin's current state
PennElectric 0:32fa44559a40 87 */
PennElectric 0:32fa44559a40 88 operator int() {
PennElectric 0:32fa44559a40 89 return read();
PennElectric 0:32fa44559a40 90 }
PennElectric 0:32fa44559a40 91 #endif
PennElectric 0:32fa44559a40 92 private:
PennElectric 0:32fa44559a40 93 DigitalOut _pin;
PennElectric 0:32fa44559a40 94 int _flasherState;
PennElectric 0:32fa44559a40 95 float _time;
PennElectric 0:32fa44559a40 96 Ticker _flasher;
PennElectric 0:32fa44559a40 97 void toggle();
PennElectric 0:32fa44559a40 98 };
PennElectric 0:32fa44559a40 99
PennElectric 0:32fa44559a40 100 #endif