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