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:
0:32fa44559a40
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 #include "mbed.h"
PennElectric 3:dbd7ab16f171 20 #include "ToggleFlash.h"
PennElectric 3:dbd7ab16f171 21
PennElectric 3:dbd7ab16f171 22 void ToggleFlash::toggle() {
PennElectric 3:dbd7ab16f171 23 _pin = !_pin; // Toggle pin
PennElectric 3:dbd7ab16f171 24 }
PennElectric 3:dbd7ab16f171 25 ToggleFlash::ToggleFlash(PinName pin) : _pin(pin) {
PennElectric 3:dbd7ab16f171 26 _pin = 0; // Drive pin low on startup
PennElectric 3:dbd7ab16f171 27 }
PennElectric 3:dbd7ab16f171 28 ToggleFlash::ToggleFlash(PinName pin, float time) : _pin(pin) {
PennElectric 3:dbd7ab16f171 29 _pin = 0; // Drive pin low on startup
PennElectric 3:dbd7ab16f171 30 _time = time; // Set given time
PennElectric 3:dbd7ab16f171 31 }
PennElectric 3:dbd7ab16f171 32 void ToggleFlash::setTime(float time) {
PennElectric 3:dbd7ab16f171 33 if (_time == time) return; // Do nothing if time already correct
PennElectric 3:dbd7ab16f171 34 _time = time; // Set new time
PennElectric 3:dbd7ab16f171 35 if (_flasherState != 0) { // Refresh the Flash if it was currently flashing
PennElectric 3:dbd7ab16f171 36 _flasher.detach();
PennElectric 3:dbd7ab16f171 37 _flasher.attach(this, &ToggleFlash::toggle, _time);
PennElectric 3:dbd7ab16f171 38 }
PennElectric 3:dbd7ab16f171 39 }
PennElectric 3:dbd7ab16f171 40 float ToggleFlash::getTime() {
PennElectric 3:dbd7ab16f171 41 return _time; // Return current time setting
PennElectric 3:dbd7ab16f171 42 }
PennElectric 3:dbd7ab16f171 43 void ToggleFlash::enable() {
PennElectric 3:dbd7ab16f171 44 if (_flasherState != 0) return; // Do nothing if its already flashing
PennElectric 3:dbd7ab16f171 45 _flasher.attach(this, &ToggleFlash::toggle, _time); // Else attach current time setting
PennElectric 3:dbd7ab16f171 46 _flasherState = 1; // Update state tracker
PennElectric 3:dbd7ab16f171 47 }
PennElectric 3:dbd7ab16f171 48 void ToggleFlash::disableTo(int state) {
PennElectric 3:dbd7ab16f171 49 _flasher.detach(); // Stop flashing
PennElectric 3:dbd7ab16f171 50 _flasherState = 0; // Update state tracker
PennElectric 3:dbd7ab16f171 51 _pin = state; // Drive pin to state
PennElectric 3:dbd7ab16f171 52 }
PennElectric 3:dbd7ab16f171 53 int ToggleFlash::readFlasher() {
PennElectric 3:dbd7ab16f171 54 return _flasherState; // Return state of the Flash
PennElectric 3:dbd7ab16f171 55 }
PennElectric 3:dbd7ab16f171 56 int ToggleFlash::read() {
PennElectric 3:dbd7ab16f171 57 return _pin; // Return state of the pin
PennElectric 0:32fa44559a40 58 }