Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Flasher_HelloWorld
Flasher.cpp
00001 /*Small library to debug and alert with a single LED 00002 * Copyright (c) 2011 p07gbar 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 * 00022 */ 00023 00024 #include "Flasher.h" 00025 00026 Flasher::Flasher(PinName pin, int startState):_led(pin) 00027 { 00028 state = checkState(startState); 00029 flashTimes[0] = 10; 00030 flashTimes[1] = 10; 00031 flashTimes[2] = 2; 00032 flashTimes[3] = 1; 00033 flashTimes[4] = 0.3; 00034 flashbase.attach(this, &Flasher::tick, flashTimes[state]); 00035 active = true; 00036 tick(); 00037 } 00038 00039 void Flasher::updateFlash(int statein) 00040 { 00041 state = checkState(statein); 00042 flashbase.attach(this, &Flasher::tick, flashTimes[state]); 00043 tick(); 00044 } 00045 00046 int Flasher::getState() 00047 { 00048 return state; 00049 } 00050 00051 void Flasher::pauseFor(float time) 00052 { 00053 active = false; 00054 forTime = time; 00055 returntoState = state; 00056 flashbase.detach(); 00057 forbase.attach(this, &Flasher::timeout, time); 00058 } 00059 00060 void Flasher::onFor(float time,int newState, int returnState = 0) 00061 { 00062 active = true; 00063 state = checkState(newState); 00064 returntoState = checkState(returnState); 00065 flashbase.attach(this, &Flasher::tick, flashTimes[state]); 00066 forTime = time; 00067 forbase.attach(this, &Flasher::timeout, time); 00068 } 00069 00070 void Flasher::pause() 00071 { 00072 active = false; 00073 _led = 0; 00074 flashbase.detach(); 00075 forbase.detach(); 00076 } 00077 00078 void Flasher::resume() 00079 { 00080 active = true; 00081 flashbase.attach(this, &Flasher::tick, flashTimes[state]); 00082 } 00083 00084 void Flasher::setFlashTime(int statein, float newTime) 00085 { 00086 flashTimes[checkState(statein)] = newTime; 00087 } 00088 00089 float Flasher::getFlashTime(int statein) 00090 { 00091 float toRet = flashTimes[checkState(statein)]; 00092 return toRet; 00093 } 00094 00095 void Flasher::tick() 00096 { 00097 if(active) 00098 { 00099 switch(state) 00100 { 00101 case 0: 00102 _led = 0; 00103 break; 00104 case 1: 00105 _led = 1; 00106 break; 00107 case 2: 00108 case 3: 00109 case 4: 00110 _led = !_led; 00111 break; 00112 default: 00113 _led = 0; 00114 break; 00115 } 00116 } 00117 } 00118 00119 void Flasher::timeout() 00120 { 00121 active = true; 00122 state = returntoState; 00123 forTime = 0; 00124 flashbase.attach(this, &Flasher::tick, flashTimes[state]); 00125 forbase.detach(); 00126 tick(); 00127 } 00128 00129 int Flasher::checkState(int statein) 00130 { 00131 if(statein > NUMSTATES || statein < 0) 00132 { 00133 return 0; 00134 } 00135 else 00136 { 00137 return statein; 00138 } 00139 } 00140 00141 Flasher& Flasher::operator= (int statein) 00142 { 00143 updateFlash(statein); 00144 return *this; 00145 } 00146 00147 Flasher::operator int() 00148 { 00149 return state; 00150 }
Generated on Sat Jul 30 2022 11:57:40 by
1.7.2