The code from https://github.com/vpcola/Nucleo

Committer:
sinrab
Date:
Wed Oct 08 11:00:24 2014 +0000
Revision:
0:5464d5e415e5
The code from https://github.com/vpcola/Nucleo

Who changed what in which revision?

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