SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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