mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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