Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

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