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