mbed library sources

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Jun 10 16:03:00 2013 +0100
Revision:
9:0ce32e54c9a7
Parent:
cpp/Ticker.h@2:143cac498751
Child:
10:3bc89ef62ce7
Refactoring of the mbed SDK:
- Provide a well defined HAL and API
- Keep separated the HAL implementations for the different targets

Who changed what in which revision?

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