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