test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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