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