mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
api/Ticker.h@36:ab3ee77451e7, 2013-10-23 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Oct 23 14:15:04 2013 +0100
- Revision:
- 36:ab3ee77451e7
- Parent:
- 19:398f4c622e1b
- Child:
- 305:89b9c3a9a045
Synchronized with git revision 2404dc0092fa583d899df3d9021a4ddb4510b011
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 13:0645d8841f51 | 1 | /* mbed Microcontroller Library |
bogdanm | 13:0645d8841f51 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 13:0645d8841f51 | 3 | * |
bogdanm | 13:0645d8841f51 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 13:0645d8841f51 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 13:0645d8841f51 | 6 | * You may obtain a copy of the License at |
bogdanm | 13:0645d8841f51 | 7 | * |
bogdanm | 13:0645d8841f51 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 13:0645d8841f51 | 9 | * |
bogdanm | 13:0645d8841f51 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 13:0645d8841f51 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 13:0645d8841f51 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 13:0645d8841f51 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 13:0645d8841f51 | 14 | * limitations under the License. |
bogdanm | 13:0645d8841f51 | 15 | */ |
bogdanm | 13:0645d8841f51 | 16 | #ifndef MBED_TICKER_H |
bogdanm | 13:0645d8841f51 | 17 | #define MBED_TICKER_H |
bogdanm | 13:0645d8841f51 | 18 | |
bogdanm | 13:0645d8841f51 | 19 | #include "TimerEvent.h" |
bogdanm | 13:0645d8841f51 | 20 | #include "FunctionPointer.h" |
bogdanm | 13:0645d8841f51 | 21 | |
bogdanm | 13:0645d8841f51 | 22 | namespace mbed { |
bogdanm | 13:0645d8841f51 | 23 | |
bogdanm | 13:0645d8841f51 | 24 | /** A Ticker is used to call a function at a recurring interval |
bogdanm | 13:0645d8841f51 | 25 | * |
bogdanm | 13:0645d8841f51 | 26 | * You can use as many seperate Ticker objects as you require. |
bogdanm | 13:0645d8841f51 | 27 | * |
bogdanm | 13:0645d8841f51 | 28 | * Example: |
bogdanm | 13:0645d8841f51 | 29 | * @code |
bogdanm | 13:0645d8841f51 | 30 | * // Toggle the blinking led after 5 seconds |
bogdanm | 13:0645d8841f51 | 31 | * |
bogdanm | 13:0645d8841f51 | 32 | * #include "mbed.h" |
bogdanm | 13:0645d8841f51 | 33 | * |
bogdanm | 13:0645d8841f51 | 34 | * Ticker timer; |
bogdanm | 13:0645d8841f51 | 35 | * DigitalOut led1(LED1); |
bogdanm | 13:0645d8841f51 | 36 | * DigitalOut led2(LED2); |
bogdanm | 13:0645d8841f51 | 37 | * |
bogdanm | 13:0645d8841f51 | 38 | * int flip = 0; |
bogdanm | 13:0645d8841f51 | 39 | * |
bogdanm | 13:0645d8841f51 | 40 | * void attime() { |
bogdanm | 13:0645d8841f51 | 41 | * flip = !flip; |
bogdanm | 13:0645d8841f51 | 42 | * } |
bogdanm | 13:0645d8841f51 | 43 | * |
bogdanm | 13:0645d8841f51 | 44 | * int main() { |
bogdanm | 13:0645d8841f51 | 45 | * timer.attach(&attime, 5); |
bogdanm | 13:0645d8841f51 | 46 | * while(1) { |
bogdanm | 13:0645d8841f51 | 47 | * if(flip == 0) { |
bogdanm | 13:0645d8841f51 | 48 | * led1 = !led1; |
bogdanm | 13:0645d8841f51 | 49 | * } else { |
bogdanm | 13:0645d8841f51 | 50 | * led2 = !led2; |
bogdanm | 13:0645d8841f51 | 51 | * } |
bogdanm | 13:0645d8841f51 | 52 | * wait(0.2); |
bogdanm | 13:0645d8841f51 | 53 | * } |
bogdanm | 13:0645d8841f51 | 54 | * } |
bogdanm | 13:0645d8841f51 | 55 | * @endcode |
bogdanm | 13:0645d8841f51 | 56 | */ |
bogdanm | 13:0645d8841f51 | 57 | class Ticker : public TimerEvent { |
bogdanm | 13:0645d8841f51 | 58 | |
bogdanm | 13:0645d8841f51 | 59 | public: |
bogdanm | 13:0645d8841f51 | 60 | |
bogdanm | 13:0645d8841f51 | 61 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
bogdanm | 13:0645d8841f51 | 62 | * |
bogdanm | 13:0645d8841f51 | 63 | * @param fptr pointer to the function to be called |
bogdanm | 13:0645d8841f51 | 64 | * @param t the time between calls in seconds |
bogdanm | 13:0645d8841f51 | 65 | */ |
mbed_official | 36:ab3ee77451e7 | 66 | void attach(void (*fptr)(void), float t) { |
mbed_official | 36:ab3ee77451e7 | 67 | attach_us(fptr, t * 1000000.0f); |
bogdanm | 13:0645d8841f51 | 68 | } |
bogdanm | 13:0645d8841f51 | 69 | |
bogdanm | 13:0645d8841f51 | 70 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
bogdanm | 13:0645d8841f51 | 71 | * |
bogdanm | 13:0645d8841f51 | 72 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 73 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 74 | * @param t the time between calls in seconds |
bogdanm | 13:0645d8841f51 | 75 | */ |
bogdanm | 13:0645d8841f51 | 76 | template<typename T> |
mbed_official | 36:ab3ee77451e7 | 77 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
mbed_official | 36:ab3ee77451e7 | 78 | attach_us(tptr, mptr, t * 1000000.0f); |
bogdanm | 13:0645d8841f51 | 79 | } |
bogdanm | 13:0645d8841f51 | 80 | |
bogdanm | 13:0645d8841f51 | 81 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
bogdanm | 13:0645d8841f51 | 82 | * |
bogdanm | 13:0645d8841f51 | 83 | * @param fptr pointer to the function to be called |
bogdanm | 13:0645d8841f51 | 84 | * @param t the time between calls in micro-seconds |
bogdanm | 13:0645d8841f51 | 85 | */ |
mbed_official | 36:ab3ee77451e7 | 86 | void attach_us(void (*fptr)(void), unsigned int t) { |
mbed_official | 36:ab3ee77451e7 | 87 | _function.attach(fptr); |
bogdanm | 13:0645d8841f51 | 88 | setup(t); |
bogdanm | 13:0645d8841f51 | 89 | } |
bogdanm | 13:0645d8841f51 | 90 | |
bogdanm | 13:0645d8841f51 | 91 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
bogdanm | 13:0645d8841f51 | 92 | * |
bogdanm | 13:0645d8841f51 | 93 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 94 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 95 | * @param t the time between calls in micro-seconds |
bogdanm | 13:0645d8841f51 | 96 | */ |
bogdanm | 13:0645d8841f51 | 97 | template<typename T> |
mbed_official | 36:ab3ee77451e7 | 98 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
mbed_official | 36:ab3ee77451e7 | 99 | _function.attach(tptr, mptr); |
bogdanm | 13:0645d8841f51 | 100 | setup(t); |
bogdanm | 13:0645d8841f51 | 101 | } |
bogdanm | 13:0645d8841f51 | 102 | |
bogdanm | 13:0645d8841f51 | 103 | /** Detach the function |
bogdanm | 13:0645d8841f51 | 104 | */ |
bogdanm | 13:0645d8841f51 | 105 | void detach(); |
bogdanm | 13:0645d8841f51 | 106 | |
bogdanm | 13:0645d8841f51 | 107 | protected: |
bogdanm | 13:0645d8841f51 | 108 | void setup(unsigned int t); |
bogdanm | 13:0645d8841f51 | 109 | virtual void handler(); |
bogdanm | 13:0645d8841f51 | 110 | |
bogdanm | 13:0645d8841f51 | 111 | unsigned int _delay; |
mbed_official | 36:ab3ee77451e7 | 112 | FunctionPointer _function; |
bogdanm | 13:0645d8841f51 | 113 | }; |
bogdanm | 13:0645d8841f51 | 114 | |
bogdanm | 13:0645d8841f51 | 115 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 116 | |
bogdanm | 13:0645d8841f51 | 117 | #endif |