help :(

Dependencies:   FFT

Committer:
dimitryjl23
Date:
Fri Dec 04 18:01:22 2020 +0000
Revision:
11:951bbb0385aa
Parent:
0:d6c9b09b4042
Final :)

Who changed what in which revision?

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