The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Mon Oct 02 15:20:36 2017 +0100
Revision:
152:235179ab3f27
Parent:
146:22da6e220af6
Child:
153:b484a57bc302
Release 152 of the mbed library.

Who changed what in which revision?

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