test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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