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