mbed library sources
Fork of mbed-src by
api/Ticker.h@636:21ce76d41a4f, 2016-02-21 (annotated)
- Committer:
- rllamado
- Date:
- Sun Feb 21 07:27:08 2016 +0000
- Revision:
- 636:21ce76d41a4f
- Parent:
- 547:88c982c8f758
something
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: |
mbed_official | 525:c320967f86b9 | 60 | Ticker() : TimerEvent() { |
mbed_official | 525:c320967f86b9 | 61 | } |
mbed_official | 525:c320967f86b9 | 62 | |
mbed_official | 547:88c982c8f758 | 63 | Ticker(const ticker_data_t *data) : TimerEvent(data) { |
mbed_official | 525:c320967f86b9 | 64 | } |
bogdanm | 13:0645d8841f51 | 65 | |
bogdanm | 13:0645d8841f51 | 66 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
bogdanm | 13:0645d8841f51 | 67 | * |
bogdanm | 13:0645d8841f51 | 68 | * @param fptr pointer to the function to be called |
bogdanm | 13:0645d8841f51 | 69 | * @param t the time between calls in seconds |
bogdanm | 13:0645d8841f51 | 70 | */ |
mbed_official | 36:ab3ee77451e7 | 71 | void attach(void (*fptr)(void), float t) { |
mbed_official | 36:ab3ee77451e7 | 72 | attach_us(fptr, t * 1000000.0f); |
bogdanm | 13:0645d8841f51 | 73 | } |
bogdanm | 13:0645d8841f51 | 74 | |
bogdanm | 13:0645d8841f51 | 75 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
bogdanm | 13:0645d8841f51 | 76 | * |
bogdanm | 13:0645d8841f51 | 77 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 78 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 79 | * @param t the time between calls in seconds |
bogdanm | 13:0645d8841f51 | 80 | */ |
bogdanm | 13:0645d8841f51 | 81 | template<typename T> |
mbed_official | 36:ab3ee77451e7 | 82 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
mbed_official | 36:ab3ee77451e7 | 83 | attach_us(tptr, mptr, t * 1000000.0f); |
bogdanm | 13:0645d8841f51 | 84 | } |
bogdanm | 13:0645d8841f51 | 85 | |
bogdanm | 13:0645d8841f51 | 86 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
bogdanm | 13:0645d8841f51 | 87 | * |
bogdanm | 13:0645d8841f51 | 88 | * @param fptr pointer to the function to be called |
bogdanm | 13:0645d8841f51 | 89 | * @param t the time between calls in micro-seconds |
bogdanm | 13:0645d8841f51 | 90 | */ |
mbed_official | 304:89b9c3a9a045 | 91 | void attach_us(void (*fptr)(void), timestamp_t t) { |
mbed_official | 36:ab3ee77451e7 | 92 | _function.attach(fptr); |
bogdanm | 13:0645d8841f51 | 93 | setup(t); |
bogdanm | 13:0645d8841f51 | 94 | } |
bogdanm | 13:0645d8841f51 | 95 | |
bogdanm | 13:0645d8841f51 | 96 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
bogdanm | 13:0645d8841f51 | 97 | * |
bogdanm | 13:0645d8841f51 | 98 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 99 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 100 | * @param t the time between calls in micro-seconds |
bogdanm | 13:0645d8841f51 | 101 | */ |
bogdanm | 13:0645d8841f51 | 102 | template<typename T> |
mbed_official | 304:89b9c3a9a045 | 103 | void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) { |
mbed_official | 36:ab3ee77451e7 | 104 | _function.attach(tptr, mptr); |
bogdanm | 13:0645d8841f51 | 105 | setup(t); |
bogdanm | 13:0645d8841f51 | 106 | } |
bogdanm | 13:0645d8841f51 | 107 | |
mbed_official | 304:89b9c3a9a045 | 108 | virtual ~Ticker() { |
mbed_official | 304:89b9c3a9a045 | 109 | detach(); |
mbed_official | 304:89b9c3a9a045 | 110 | } |
mbed_official | 304:89b9c3a9a045 | 111 | |
bogdanm | 13:0645d8841f51 | 112 | /** Detach the function |
bogdanm | 13:0645d8841f51 | 113 | */ |
bogdanm | 13:0645d8841f51 | 114 | void detach(); |
bogdanm | 13:0645d8841f51 | 115 | |
bogdanm | 13:0645d8841f51 | 116 | protected: |
mbed_official | 304:89b9c3a9a045 | 117 | void setup(timestamp_t t); |
bogdanm | 13:0645d8841f51 | 118 | virtual void handler(); |
bogdanm | 13:0645d8841f51 | 119 | |
mbed_official | 304:89b9c3a9a045 | 120 | protected: |
mbed_official | 304:89b9c3a9a045 | 121 | timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ |
mbed_official | 304:89b9c3a9a045 | 122 | FunctionPointer _function; /**< Callback. */ |
bogdanm | 13:0645d8841f51 | 123 | }; |
bogdanm | 13:0645d8841f51 | 124 | |
bogdanm | 13:0645d8841f51 | 125 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 126 | |
bogdanm | 13:0645d8841f51 | 127 | #endif |