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