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