Giles Barton-Owen / Flasher

Dependents:   Flasher_HelloWorld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Flasher.h Source File

Flasher.h

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 #ifndef FLASHER_H
00025 #define FLASHER_H
00026 
00027 #include "mbed.h"
00028 
00029 #define NUMSTATES 5
00030 #define OFF 0
00031 #define ON 1
00032 #define SLOW 2
00033 #define MEDIUM 3
00034 #define QUICK 4
00035 #define FAST 4
00036 
00037 /** A LED Flasher class based on Ticker and Timeout
00038  *  
00039  * States are defined in words:
00040  * 
00041  * OFF, ON, SLOW, MEDIUM, QUICK or FAST
00042  * 
00043  * With default times of (in seconds):
00044  * 
00045  * Slow: 2
00046  * 
00047  * Medium: 1
00048  * 
00049  * Fast/Quick: 0.3
00050  * 
00051  *
00052  * Example:
00053  * @code
00054  * // Switches LED1 through the different modes
00055  * #include "mbed.h"
00056  * #include "Flasher.h"
00057  * 
00058  * Flasher myFlasher(LED1, OFF); //Defines myFlasher on LED1 starting off
00059  * 
00060  * int main() {
00061  *      while(1){   
00062  *          wait(5);
00063  *          myFlasher = ON;
00064  *          wait(5);
00065  *          myFlasher.updateFlash(SLOW);
00066  *          wait(5);
00067  *          myFlasher = MEDIUM;
00068  *          wait(5);
00069  *          myFlasher.updateFlash(QUICK);
00070  *          wait(5);
00071  *          myFlasher.onFor(3,ON,MEDIUM);
00072  *          wait(5);
00073  *          myFlasher.pauseFor(2);
00074  *          wait(5);
00075  *          myFlasher.updateFlash(OFF);
00076  *      }
00077  *  }
00078  * @endcode
00079  */
00080 
00081 
00082 class Flasher {
00083 
00084 public:
00085 
00086 /** Create a Flasher object attached to the specified Pin and Start State
00087  *
00088  * @param pin DigitalOut pin to connect the LED to
00089  * @param startState State for the object to start in
00090  *
00091  */
00092 Flasher(PinName pin, int startState);
00093 
00094 /** Set new state for the obeject
00095  *
00096  * @param statein New State for the object
00097  */
00098  
00099 void updateFlash(int statein);
00100 
00101 /** Read the current state of the object
00102  * 
00103  * @param returns The current state
00104  */
00105  
00106 int getState();
00107 
00108 /** Turn the LED off for a specified period, returns to old state
00109  * 
00110  * @param time Time in seconds to be off for
00111  */
00112 
00113 void pauseFor(float time);
00114 
00115 /** Sets a new state for a while then returns to the return state
00116  * 
00117  * @param time Time in seconds to be in new state for
00118  * @param newState The new state for it to be in
00119  * @param returnState The state for it to return to
00120  */
00121 
00122 void onFor(float time, int newState, int returnState);
00123 
00124 /** Stops the flasher until resume is called
00125  */
00126 
00127 void pause();
00128 
00129 /** Resumes the flasher
00130  */
00131  
00132 void resume();
00133 
00134 /** Configures the flash times for a specified state
00135  * 
00136  * @param statein State to change
00137  * @param newTime new flash period
00138  */
00139  
00140 void setFlashTime(int statein, float newTime);
00141 
00142 /** Reads back the flash time for a state
00143  * 
00144  * @param statein State to read back
00145  * @param returns Time of that states flash
00146  */
00147 
00148 float getFlashTime(int statein);
00149 
00150 /** Sets state quickly
00151  * 
00152  * @param statein State to change to
00153  */
00154 
00155 Flasher& operator= (int statein);
00156 
00157 /** Gets State quickly
00158  *
00159  * @param returns Current state
00160  */
00161 
00162 operator int();
00163 
00164 private:
00165 
00166 DigitalOut _led;
00167 
00168 int state;
00169 
00170 int returntoState;
00171 
00172 bool active;
00173 
00174 float forTime;
00175 
00176 float flashTimes[NUMSTATES];
00177 
00178 Ticker flashbase;
00179 
00180 Timeout forbase;
00181 
00182 void tick();
00183 
00184 void timeout();
00185 
00186 int checkState(int statein);
00187 
00188 };
00189 
00190 #endif
00191 
00192 
00193