mbed library sources that still uses the stm standard peripheral library

Fork of mbed-src by Reinhold Schaefer

Committer:
emilmont
Date:
Fri Jun 14 17:49:17 2013 +0100
Revision:
10:3bc89ef62ce7
Parent:
9:0ce32e54c9a7
Child:
13:0645d8841f51
Unify mbed library sources

Who changed what in which revision?

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