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