mbed library sources that still uses the stm standard peripheral library
Fork of mbed-src by
cpp/Ticker.h@0:fd0d7bdfcdc2, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:24:08 2012 +0000
- Revision:
- 0:fd0d7bdfcdc2
- Child:
- 2:143cac498751
mbed sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:fd0d7bdfcdc2 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
mbed_official | 0:fd0d7bdfcdc2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_official | 0:fd0d7bdfcdc2 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_official | 0:fd0d7bdfcdc2 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_official | 0:fd0d7bdfcdc2 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 0:fd0d7bdfcdc2 | 10 | * |
mbed_official | 0:fd0d7bdfcdc2 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_official | 0:fd0d7bdfcdc2 | 12 | * all copies or substantial portions of the Software. |
mbed_official | 0:fd0d7bdfcdc2 | 13 | * |
mbed_official | 0:fd0d7bdfcdc2 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_official | 0:fd0d7bdfcdc2 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_official | 0:fd0d7bdfcdc2 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_official | 0:fd0d7bdfcdc2 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_official | 0:fd0d7bdfcdc2 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:fd0d7bdfcdc2 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_official | 0:fd0d7bdfcdc2 | 20 | * SOFTWARE. |
mbed_official | 0:fd0d7bdfcdc2 | 21 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 22 | #ifndef MBED_TICKER_H |
mbed_official | 0:fd0d7bdfcdc2 | 23 | #define MBED_TICKER_H |
mbed_official | 0:fd0d7bdfcdc2 | 24 | |
mbed_official | 0:fd0d7bdfcdc2 | 25 | #include "TimerEvent.h" |
mbed_official | 0:fd0d7bdfcdc2 | 26 | #include "FunctionPointer.h" |
mbed_official | 0:fd0d7bdfcdc2 | 27 | |
mbed_official | 0:fd0d7bdfcdc2 | 28 | namespace mbed { |
mbed_official | 0:fd0d7bdfcdc2 | 29 | |
mbed_official | 0:fd0d7bdfcdc2 | 30 | /** A Ticker is used to call a function at a recurring interval |
mbed_official | 0:fd0d7bdfcdc2 | 31 | * |
mbed_official | 0:fd0d7bdfcdc2 | 32 | * You can use as many seperate Ticker objects as you require. |
mbed_official | 0:fd0d7bdfcdc2 | 33 | * |
mbed_official | 0:fd0d7bdfcdc2 | 34 | * Example: |
mbed_official | 0:fd0d7bdfcdc2 | 35 | * @code |
mbed_official | 0:fd0d7bdfcdc2 | 36 | * // Toggle the blinking led after 5 seconds |
mbed_official | 0:fd0d7bdfcdc2 | 37 | * |
mbed_official | 0:fd0d7bdfcdc2 | 38 | * #include "mbed.h" |
mbed_official | 0:fd0d7bdfcdc2 | 39 | * |
mbed_official | 0:fd0d7bdfcdc2 | 40 | * Ticker timer; |
mbed_official | 0:fd0d7bdfcdc2 | 41 | * DigitalOut led1(LED1); |
mbed_official | 0:fd0d7bdfcdc2 | 42 | * DigitalOut led2(LED2); |
mbed_official | 0:fd0d7bdfcdc2 | 43 | * |
mbed_official | 0:fd0d7bdfcdc2 | 44 | * int flip = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 45 | * |
mbed_official | 0:fd0d7bdfcdc2 | 46 | * void attime() { |
mbed_official | 0:fd0d7bdfcdc2 | 47 | * flip = !flip; |
mbed_official | 0:fd0d7bdfcdc2 | 48 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 49 | * |
mbed_official | 0:fd0d7bdfcdc2 | 50 | * int main() { |
mbed_official | 0:fd0d7bdfcdc2 | 51 | * timer.attach(&attime, 5); |
mbed_official | 0:fd0d7bdfcdc2 | 52 | * while(1) { |
mbed_official | 0:fd0d7bdfcdc2 | 53 | * if(flip == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 54 | * led1 = !led1; |
mbed_official | 0:fd0d7bdfcdc2 | 55 | * } else { |
mbed_official | 0:fd0d7bdfcdc2 | 56 | * led2 = !led2; |
mbed_official | 0:fd0d7bdfcdc2 | 57 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 58 | * wait(0.2); |
mbed_official | 0:fd0d7bdfcdc2 | 59 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 60 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 61 | * @endcode |
mbed_official | 0:fd0d7bdfcdc2 | 62 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 63 | class Ticker : public TimerEvent { |
mbed_official | 0:fd0d7bdfcdc2 | 64 | |
mbed_official | 0:fd0d7bdfcdc2 | 65 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 66 | |
mbed_official | 0:fd0d7bdfcdc2 | 67 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
mbed_official | 0:fd0d7bdfcdc2 | 68 | * |
mbed_official | 0:fd0d7bdfcdc2 | 69 | * @param fptr pointer to the function to be called |
mbed_official | 0:fd0d7bdfcdc2 | 70 | * @param t the time between calls in seconds |
mbed_official | 0:fd0d7bdfcdc2 | 71 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 72 | void attach(void (*fptr)(void), float t) { |
mbed_official | 0:fd0d7bdfcdc2 | 73 | attach_us(fptr, t * 1000000.0f); |
mbed_official | 0:fd0d7bdfcdc2 | 74 | } |
mbed_official | 0:fd0d7bdfcdc2 | 75 | |
mbed_official | 0:fd0d7bdfcdc2 | 76 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
mbed_official | 0:fd0d7bdfcdc2 | 77 | * |
mbed_official | 0:fd0d7bdfcdc2 | 78 | * @param tptr pointer to the object to call the member function on |
mbed_official | 0:fd0d7bdfcdc2 | 79 | * @param mptr pointer to the member function to be called |
mbed_official | 0:fd0d7bdfcdc2 | 80 | * @param t the time between calls in seconds |
mbed_official | 0:fd0d7bdfcdc2 | 81 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 82 | template<typename T> |
mbed_official | 0:fd0d7bdfcdc2 | 83 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
mbed_official | 0:fd0d7bdfcdc2 | 84 | attach_us(tptr, mptr, t * 1000000.0f); |
mbed_official | 0:fd0d7bdfcdc2 | 85 | } |
mbed_official | 0:fd0d7bdfcdc2 | 86 | |
mbed_official | 0:fd0d7bdfcdc2 | 87 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
mbed_official | 0:fd0d7bdfcdc2 | 88 | * |
mbed_official | 0:fd0d7bdfcdc2 | 89 | * @param fptr pointer to the function to be called |
mbed_official | 0:fd0d7bdfcdc2 | 90 | * @param t the time between calls in micro-seconds |
mbed_official | 0:fd0d7bdfcdc2 | 91 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 92 | void attach_us(void (*fptr)(void), unsigned int t) { |
mbed_official | 0:fd0d7bdfcdc2 | 93 | _function.attach(fptr); |
mbed_official | 0:fd0d7bdfcdc2 | 94 | setup(t); |
mbed_official | 0:fd0d7bdfcdc2 | 95 | } |
mbed_official | 0:fd0d7bdfcdc2 | 96 | |
mbed_official | 0:fd0d7bdfcdc2 | 97 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
mbed_official | 0:fd0d7bdfcdc2 | 98 | * |
mbed_official | 0:fd0d7bdfcdc2 | 99 | * @param tptr pointer to the object to call the member function on |
mbed_official | 0:fd0d7bdfcdc2 | 100 | * @param mptr pointer to the member function to be called |
mbed_official | 0:fd0d7bdfcdc2 | 101 | * @param t the time between calls in micro-seconds |
mbed_official | 0:fd0d7bdfcdc2 | 102 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 103 | template<typename T> |
mbed_official | 0:fd0d7bdfcdc2 | 104 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
mbed_official | 0:fd0d7bdfcdc2 | 105 | _function.attach(tptr, mptr); |
mbed_official | 0:fd0d7bdfcdc2 | 106 | setup(t); |
mbed_official | 0:fd0d7bdfcdc2 | 107 | } |
mbed_official | 0:fd0d7bdfcdc2 | 108 | |
mbed_official | 0:fd0d7bdfcdc2 | 109 | /** Detach the function |
mbed_official | 0:fd0d7bdfcdc2 | 110 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 111 | void detach(); |
mbed_official | 0:fd0d7bdfcdc2 | 112 | |
mbed_official | 0:fd0d7bdfcdc2 | 113 | protected: |
mbed_official | 0:fd0d7bdfcdc2 | 114 | void setup(unsigned int t); |
mbed_official | 0:fd0d7bdfcdc2 | 115 | virtual void handler(); |
mbed_official | 0:fd0d7bdfcdc2 | 116 | |
mbed_official | 0:fd0d7bdfcdc2 | 117 | unsigned int _delay; |
mbed_official | 0:fd0d7bdfcdc2 | 118 | FunctionPointer _function; |
mbed_official | 0:fd0d7bdfcdc2 | 119 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 120 | |
mbed_official | 0:fd0d7bdfcdc2 | 121 | } // namespace mbed |
mbed_official | 0:fd0d7bdfcdc2 | 122 | |
mbed_official | 0:fd0d7bdfcdc2 | 123 | #endif |