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