BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:47:08 2018 +0000
Revision:
1:9c5af431a1f1
sdf

Who changed what in which revision?

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