mbed(SerialHalfDuplex入り)
Fork of mbed by
Ticker.h@65:5798e58a58b1, 2013-08-12 (annotated)
- Committer:
- bogdanm
- Date:
- Mon Aug 12 13:17:46 2013 +0300
- Revision:
- 65:5798e58a58b1
- Parent:
- 59:0883845fe643
- Child:
- 66:9c8f0e3462fb
New target (LPC4088), new features (interrupt chaining), bug fixes (KL25Z I2C).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 65:5798e58a58b1 | 1 | /* mbed Microcontroller Library |
bogdanm | 65:5798e58a58b1 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 65:5798e58a58b1 | 3 | * |
bogdanm | 65:5798e58a58b1 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 65:5798e58a58b1 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 65:5798e58a58b1 | 6 | * You may obtain a copy of the License at |
bogdanm | 65:5798e58a58b1 | 7 | * |
bogdanm | 65:5798e58a58b1 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 65:5798e58a58b1 | 9 | * |
bogdanm | 65:5798e58a58b1 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 65:5798e58a58b1 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 65:5798e58a58b1 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 65:5798e58a58b1 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 65:5798e58a58b1 | 14 | * limitations under the License. |
bogdanm | 65:5798e58a58b1 | 15 | */ |
bogdanm | 65:5798e58a58b1 | 16 | #ifndef MBED_TICKER_H |
bogdanm | 65:5798e58a58b1 | 17 | #define MBED_TICKER_H |
bogdanm | 65:5798e58a58b1 | 18 | |
bogdanm | 65:5798e58a58b1 | 19 | #include "TimerEvent.h" |
bogdanm | 65:5798e58a58b1 | 20 | #include "FunctionPointer.h" |
bogdanm | 65:5798e58a58b1 | 21 | #include "CallChain.h" |
bogdanm | 65:5798e58a58b1 | 22 | |
bogdanm | 65:5798e58a58b1 | 23 | namespace mbed { |
bogdanm | 65:5798e58a58b1 | 24 | |
bogdanm | 65:5798e58a58b1 | 25 | /** A Ticker is used to call a function at a recurring interval |
bogdanm | 65:5798e58a58b1 | 26 | * |
bogdanm | 65:5798e58a58b1 | 27 | * You can use as many seperate Ticker objects as you require. |
bogdanm | 65:5798e58a58b1 | 28 | * |
bogdanm | 65:5798e58a58b1 | 29 | * Example: |
bogdanm | 65:5798e58a58b1 | 30 | * @code |
bogdanm | 65:5798e58a58b1 | 31 | * // Toggle the blinking led after 5 seconds |
bogdanm | 65:5798e58a58b1 | 32 | * |
bogdanm | 65:5798e58a58b1 | 33 | * #include "mbed.h" |
bogdanm | 65:5798e58a58b1 | 34 | * |
bogdanm | 65:5798e58a58b1 | 35 | * Ticker timer; |
bogdanm | 65:5798e58a58b1 | 36 | * DigitalOut led1(LED1); |
bogdanm | 65:5798e58a58b1 | 37 | * DigitalOut led2(LED2); |
bogdanm | 65:5798e58a58b1 | 38 | * |
bogdanm | 65:5798e58a58b1 | 39 | * int flip = 0; |
bogdanm | 65:5798e58a58b1 | 40 | * |
bogdanm | 65:5798e58a58b1 | 41 | * void attime() { |
bogdanm | 65:5798e58a58b1 | 42 | * flip = !flip; |
bogdanm | 65:5798e58a58b1 | 43 | * } |
bogdanm | 65:5798e58a58b1 | 44 | * |
bogdanm | 65:5798e58a58b1 | 45 | * int main() { |
bogdanm | 65:5798e58a58b1 | 46 | * timer.attach(&attime, 5); |
bogdanm | 65:5798e58a58b1 | 47 | * while(1) { |
bogdanm | 65:5798e58a58b1 | 48 | * if(flip == 0) { |
bogdanm | 65:5798e58a58b1 | 49 | * led1 = !led1; |
bogdanm | 65:5798e58a58b1 | 50 | * } else { |
bogdanm | 65:5798e58a58b1 | 51 | * led2 = !led2; |
bogdanm | 65:5798e58a58b1 | 52 | * } |
bogdanm | 65:5798e58a58b1 | 53 | * wait(0.2); |
bogdanm | 65:5798e58a58b1 | 54 | * } |
bogdanm | 65:5798e58a58b1 | 55 | * } |
bogdanm | 65:5798e58a58b1 | 56 | * @endcode |
bogdanm | 65:5798e58a58b1 | 57 | */ |
bogdanm | 65:5798e58a58b1 | 58 | class Ticker : public TimerEvent { |
bogdanm | 65:5798e58a58b1 | 59 | |
bogdanm | 65:5798e58a58b1 | 60 | public: |
bogdanm | 65:5798e58a58b1 | 61 | |
bogdanm | 65:5798e58a58b1 | 62 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
bogdanm | 65:5798e58a58b1 | 63 | * |
bogdanm | 65:5798e58a58b1 | 64 | * @param fptr pointer to the function to be called |
bogdanm | 65:5798e58a58b1 | 65 | * @param t the time between calls in seconds |
bogdanm | 65:5798e58a58b1 | 66 | * |
bogdanm | 65:5798e58a58b1 | 67 | * @returns |
bogdanm | 65:5798e58a58b1 | 68 | * The function object created for 'fptr' |
bogdanm | 65:5798e58a58b1 | 69 | */ |
bogdanm | 65:5798e58a58b1 | 70 | pFunctionPointer_t attach(void (*fptr)(void), float t) { |
bogdanm | 65:5798e58a58b1 | 71 | return attach_us(fptr, t * 1000000.0f); |
bogdanm | 65:5798e58a58b1 | 72 | } |
bogdanm | 65:5798e58a58b1 | 73 | |
bogdanm | 65:5798e58a58b1 | 74 | /** Add a function to be called by the Ticker at the end of the call chain |
bogdanm | 65:5798e58a58b1 | 75 | * |
bogdanm | 65:5798e58a58b1 | 76 | * @param fptr the function to add |
bogdanm | 65:5798e58a58b1 | 77 | * |
bogdanm | 65:5798e58a58b1 | 78 | * @returns |
bogdanm | 65:5798e58a58b1 | 79 | * The function object created for 'fptr' |
bogdanm | 65:5798e58a58b1 | 80 | */ |
bogdanm | 65:5798e58a58b1 | 81 | pFunctionPointer_t add_function(void (*fptr)(void)) { |
bogdanm | 65:5798e58a58b1 | 82 | return add_function_helper(fptr); |
bogdanm | 65:5798e58a58b1 | 83 | } |
bogdanm | 65:5798e58a58b1 | 84 | |
bogdanm | 65:5798e58a58b1 | 85 | /** Add a function to be called by the Ticker at the beginning of the call chain |
bogdanm | 65:5798e58a58b1 | 86 | * |
bogdanm | 65:5798e58a58b1 | 87 | * @param fptr the function to add |
bogdanm | 65:5798e58a58b1 | 88 | * |
bogdanm | 65:5798e58a58b1 | 89 | * @returns |
bogdanm | 65:5798e58a58b1 | 90 | * The function object created for 'fptr' |
bogdanm | 65:5798e58a58b1 | 91 | */ |
bogdanm | 65:5798e58a58b1 | 92 | pFunctionPointer_t add_function_front(void (*fptr)(void)) { |
bogdanm | 65:5798e58a58b1 | 93 | return add_function_helper(fptr, true); |
bogdanm | 65:5798e58a58b1 | 94 | } |
bogdanm | 65:5798e58a58b1 | 95 | |
bogdanm | 65:5798e58a58b1 | 96 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
bogdanm | 65:5798e58a58b1 | 97 | * |
bogdanm | 65:5798e58a58b1 | 98 | * @param tptr pointer to the object to call the member function on |
bogdanm | 65:5798e58a58b1 | 99 | * @param mptr pointer to the member function to be called |
bogdanm | 65:5798e58a58b1 | 100 | * @param t the time between calls in seconds |
bogdanm | 65:5798e58a58b1 | 101 | * |
bogdanm | 65:5798e58a58b1 | 102 | * @returns |
bogdanm | 65:5798e58a58b1 | 103 | * The function object created for 'tptr' and 'mptr' |
bogdanm | 65:5798e58a58b1 | 104 | */ |
bogdanm | 65:5798e58a58b1 | 105 | template<typename T> |
bogdanm | 65:5798e58a58b1 | 106 | pFunctionPointer_t attach(T* tptr, void (T::*mptr)(void), float t) { |
bogdanm | 65:5798e58a58b1 | 107 | return attach_us(tptr, mptr, t * 1000000.0f); |
bogdanm | 65:5798e58a58b1 | 108 | } |
bogdanm | 65:5798e58a58b1 | 109 | |
bogdanm | 65:5798e58a58b1 | 110 | /** Add a function to be called by the Ticker at the end of the call chain |
bogdanm | 65:5798e58a58b1 | 111 | * |
bogdanm | 65:5798e58a58b1 | 112 | * @param tptr pointer to the object to call the member function on |
bogdanm | 65:5798e58a58b1 | 113 | * @param mptr pointer to the member function to be called |
bogdanm | 65:5798e58a58b1 | 114 | * |
bogdanm | 65:5798e58a58b1 | 115 | * @returns |
bogdanm | 65:5798e58a58b1 | 116 | * The function object created for 'tptr' and 'mptr' |
bogdanm | 65:5798e58a58b1 | 117 | */ |
bogdanm | 65:5798e58a58b1 | 118 | template<typename T> |
bogdanm | 65:5798e58a58b1 | 119 | pFunctionPointer_t add_function(T* tptr, void (T::*mptr)(void)) { |
bogdanm | 65:5798e58a58b1 | 120 | return add_function_helper(tptr, mptr); |
bogdanm | 65:5798e58a58b1 | 121 | } |
bogdanm | 65:5798e58a58b1 | 122 | |
bogdanm | 65:5798e58a58b1 | 123 | /** Add a function to be called by the Ticker at the beginning of the call chain |
bogdanm | 65:5798e58a58b1 | 124 | * |
bogdanm | 65:5798e58a58b1 | 125 | * @param tptr pointer to the object to call the member function on |
bogdanm | 65:5798e58a58b1 | 126 | * @param mptr pointer to the member function to be called |
bogdanm | 65:5798e58a58b1 | 127 | * |
bogdanm | 65:5798e58a58b1 | 128 | * @returns |
bogdanm | 65:5798e58a58b1 | 129 | * The function object created for 'tptr' and 'mptr' |
bogdanm | 65:5798e58a58b1 | 130 | */ |
bogdanm | 65:5798e58a58b1 | 131 | template<typename T> |
bogdanm | 65:5798e58a58b1 | 132 | pFunctionPointer_t add_function_front(T* tptr, void (T::*mptr)(void)) { |
bogdanm | 65:5798e58a58b1 | 133 | return add_function_helper(tptr, mptr, true); |
bogdanm | 65:5798e58a58b1 | 134 | } |
bogdanm | 65:5798e58a58b1 | 135 | |
bogdanm | 65:5798e58a58b1 | 136 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
bogdanm | 65:5798e58a58b1 | 137 | * |
bogdanm | 65:5798e58a58b1 | 138 | * @param fptr pointer to the function to be called |
bogdanm | 65:5798e58a58b1 | 139 | * @param t the time between calls in micro-seconds |
bogdanm | 65:5798e58a58b1 | 140 | * |
bogdanm | 65:5798e58a58b1 | 141 | * @returns |
bogdanm | 65:5798e58a58b1 | 142 | * The function object created for 'fptr' |
bogdanm | 65:5798e58a58b1 | 143 | */ |
bogdanm | 65:5798e58a58b1 | 144 | pFunctionPointer_t attach_us(void (*fptr)(void), unsigned int t) { |
bogdanm | 65:5798e58a58b1 | 145 | pFunctionPointer_t pf = _chain.add(fptr); |
bogdanm | 65:5798e58a58b1 | 146 | setup(t); |
bogdanm | 65:5798e58a58b1 | 147 | return pf; |
bogdanm | 65:5798e58a58b1 | 148 | } |
bogdanm | 65:5798e58a58b1 | 149 | |
bogdanm | 65:5798e58a58b1 | 150 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
bogdanm | 65:5798e58a58b1 | 151 | * |
bogdanm | 65:5798e58a58b1 | 152 | * @param tptr pointer to the object to call the member function on |
bogdanm | 65:5798e58a58b1 | 153 | * @param mptr pointer to the member function to be called |
bogdanm | 65:5798e58a58b1 | 154 | * @param t the time between calls in micro-seconds |
bogdanm | 65:5798e58a58b1 | 155 | * |
bogdanm | 65:5798e58a58b1 | 156 | * @returns |
bogdanm | 65:5798e58a58b1 | 157 | * The function object created for 'tptr' and 'mptr' |
bogdanm | 65:5798e58a58b1 | 158 | */ |
bogdanm | 65:5798e58a58b1 | 159 | template<typename T> |
bogdanm | 65:5798e58a58b1 | 160 | pFunctionPointer_t attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
bogdanm | 65:5798e58a58b1 | 161 | pFunctionPointer_t pf = _chain.add(mptr, tptr); |
bogdanm | 65:5798e58a58b1 | 162 | setup(t); |
bogdanm | 65:5798e58a58b1 | 163 | return pf; |
bogdanm | 65:5798e58a58b1 | 164 | } |
bogdanm | 65:5798e58a58b1 | 165 | |
bogdanm | 65:5798e58a58b1 | 166 | /** Detach the function |
bogdanm | 65:5798e58a58b1 | 167 | */ |
bogdanm | 65:5798e58a58b1 | 168 | void detach(); |
bogdanm | 65:5798e58a58b1 | 169 | |
bogdanm | 65:5798e58a58b1 | 170 | /** Remove a function from the Ticker's call chain |
bogdanm | 65:5798e58a58b1 | 171 | * |
bogdanm | 65:5798e58a58b1 | 172 | * @param pf the function object to remove |
bogdanm | 65:5798e58a58b1 | 173 | * |
bogdanm | 65:5798e58a58b1 | 174 | * @returns |
bogdanm | 65:5798e58a58b1 | 175 | * true if the function was found and removed, false otherwise |
bogdanm | 65:5798e58a58b1 | 176 | */ |
bogdanm | 65:5798e58a58b1 | 177 | bool remove_function(pFunctionPointer_t pf) { |
bogdanm | 65:5798e58a58b1 | 178 | bool res = _chain.remove(pf); |
bogdanm | 65:5798e58a58b1 | 179 | if (res && _chain.size() == 0) |
bogdanm | 65:5798e58a58b1 | 180 | detach(); |
bogdanm | 65:5798e58a58b1 | 181 | return res; |
bogdanm | 65:5798e58a58b1 | 182 | } |
bogdanm | 65:5798e58a58b1 | 183 | |
bogdanm | 65:5798e58a58b1 | 184 | protected: |
bogdanm | 65:5798e58a58b1 | 185 | void setup(unsigned int t); |
bogdanm | 65:5798e58a58b1 | 186 | pFunctionPointer_t add_function_helper(void (*fptr)(void), bool front=false); |
bogdanm | 65:5798e58a58b1 | 187 | virtual void handler(); |
bogdanm | 65:5798e58a58b1 | 188 | |
bogdanm | 65:5798e58a58b1 | 189 | template<typename T> |
bogdanm | 65:5798e58a58b1 | 190 | pFunctionPointer_t add_function_helper(T* tptr, void (T::*mptr)(void), bool front=false) { |
bogdanm | 65:5798e58a58b1 | 191 | if (_chain.size() == 0) |
bogdanm | 65:5798e58a58b1 | 192 | return NULL; |
bogdanm | 65:5798e58a58b1 | 193 | return front ? _chain.add_front(tptr, mptr) : _chain.add(tptr, mptr); |
bogdanm | 65:5798e58a58b1 | 194 | } |
bogdanm | 65:5798e58a58b1 | 195 | |
bogdanm | 65:5798e58a58b1 | 196 | unsigned int _delay; |
bogdanm | 65:5798e58a58b1 | 197 | CallChain _chain; |
bogdanm | 65:5798e58a58b1 | 198 | }; |
bogdanm | 65:5798e58a58b1 | 199 | |
bogdanm | 65:5798e58a58b1 | 200 | } // namespace mbed |
bogdanm | 65:5798e58a58b1 | 201 | |
bogdanm | 65:5798e58a58b1 | 202 | #endif |