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).
ToggleFlash.h@2:fddd1a6486bd, 2012-12-22 (annotated)
- Committer:
- PennElectric
- Date:
- Sat Dec 22 21:19:15 2012 +0000
- Revision:
- 2:fddd1a6486bd
- Parent:
- 1:0764e2d49518
- Child:
- 3:dbd7ab16f171
added first constructor to auto doc;
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:0764e2d49518 | 21 | #ifndef ToggleFlash |
PennElectric | 1:0764e2d49518 | 22 | #define ToggleFlash |
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 | 1:0764e2d49518 | 30 | * ToggleFlash 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 | 2:fddd1a6486bd | 49 | /** Create a ToggleFlasher object connected to a pin |
PennElectric | 2:fddd1a6486bd | 50 | * @param pin The pin to which the ToggleFlasher is attached |
PennElectric | 2:fddd1a6486bd | 51 | */ |
PennElectric | 0:32fa44559a40 | 52 | ToggleFlash(PinName pin); |
PennElectric | 0:32fa44559a40 | 53 | /** Create a ToggleFlasher object connected to a pin and set the on-off time |
PennElectric | 0:32fa44559a40 | 54 | * @param pin The pin to which the ToggleFlasher is attached |
PennElectric | 0:32fa44559a40 | 55 | * @param time The specified on-off time in seconds |
PennElectric | 0:32fa44559a40 | 56 | */ |
PennElectric | 0:32fa44559a40 | 57 | ToggleFlash(PinName pin, float time); |
PennElectric | 0:32fa44559a40 | 58 | /** Set the on-off time ex. 1.5 means on for 1.5 seconds off for 1.5 seconds |
PennElectric | 0:32fa44559a40 | 59 | * @param time The specified on-off time in seconds |
PennElectric | 0:32fa44559a40 | 60 | */ |
PennElectric | 0:32fa44559a40 | 61 | void setTime(float time); |
PennElectric | 0:32fa44559a40 | 62 | /** Retrieve the current time setting of the flasher object |
PennElectric | 0:32fa44559a40 | 63 | * @returns The current on-off time of the ToggleFlash object |
PennElectric | 0:32fa44559a40 | 64 | */ |
PennElectric | 0:32fa44559a40 | 65 | float getTime(); |
PennElectric | 0:32fa44559a40 | 66 | /** Enable the flasher to start toggling pin |
PennElectric | 0:32fa44559a40 | 67 | * |
PennElectric | 0:32fa44559a40 | 68 | */ |
PennElectric | 0:32fa44559a40 | 69 | void enable(); |
PennElectric | 0:32fa44559a40 | 70 | /** Disable the flasher and give the pin a constant state |
PennElectric | 0:32fa44559a40 | 71 | * @param state 1 or 0 for constant on or constant off |
PennElectric | 0:32fa44559a40 | 72 | */ |
PennElectric | 0:32fa44559a40 | 73 | void disableTo(int state); |
PennElectric | 0:32fa44559a40 | 74 | /** Return the current status of the flasher |
PennElectric | 0:32fa44559a40 | 75 | * @returns 1 or 0 for flasher enabled or flasher disabled |
PennElectric | 0:32fa44559a40 | 76 | */ |
PennElectric | 0:32fa44559a40 | 77 | int readFlasher(); |
PennElectric | 0:32fa44559a40 | 78 | /** Return the current state of the pin |
PennElectric | 0:32fa44559a40 | 79 | * @returns 1 or 0 for currently set high or low |
PennElectric | 0:32fa44559a40 | 80 | */ |
PennElectric | 0:32fa44559a40 | 81 | int read(); |
PennElectric | 0:32fa44559a40 | 82 | #ifdef MBED_OPERATORS |
PennElectric | 0:32fa44559a40 | 83 | /** An operator shorthand for disableTo(int state) to give the pin a constant state |
PennElectric | 0:32fa44559a40 | 84 | */ |
PennElectric | 0:32fa44559a40 | 85 | ToggleFlash& operator= (int state) { |
PennElectric | 0:32fa44559a40 | 86 | disableTo(state); |
PennElectric | 0:32fa44559a40 | 87 | return *this; |
PennElectric | 0:32fa44559a40 | 88 | } |
PennElectric | 0:32fa44559a40 | 89 | /** An operator shorthand for read() to read pin's current state |
PennElectric | 0:32fa44559a40 | 90 | */ |
PennElectric | 0:32fa44559a40 | 91 | operator int() { |
PennElectric | 0:32fa44559a40 | 92 | return read(); |
PennElectric | 0:32fa44559a40 | 93 | } |
PennElectric | 0:32fa44559a40 | 94 | #endif |
PennElectric | 0:32fa44559a40 | 95 | private: |
PennElectric | 0:32fa44559a40 | 96 | DigitalOut _pin; |
PennElectric | 0:32fa44559a40 | 97 | int _flasherState; |
PennElectric | 0:32fa44559a40 | 98 | float _time; |
PennElectric | 0:32fa44559a40 | 99 | Ticker _flasher; |
PennElectric | 0:32fa44559a40 | 100 | void toggle(); |
PennElectric | 0:32fa44559a40 | 101 | }; |
PennElectric | 0:32fa44559a40 | 102 | |
PennElectric | 0:32fa44559a40 | 103 | #endif |