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