mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Mon Feb 18 09:41:56 2013 +0000
Revision:
9:663789d7729f
Parent:
8:c14af7958ef5
Update mbed-KL25Z to latest build

Who changed what in which revision?

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