This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Committer:
apluscw
Date:
Fri Jul 20 21:24:42 2018 +0000
Revision:
187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.

Who changed what in which revision?

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