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:
Sun Dec 23 05:07:44 2012 +0000
Revision:
3:dbd7ab16f171
Parent:
2:fddd1a6486bd
Some changes compiles

Who changed what in which revision?

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