mbed.h verzija za koristenje na predmetu PAI

Committer:
esokic
Date:
Tue Mar 10 09:51:52 2015 +0000
Revision:
0:05aad811ea07
mbed.h verzija od marta 2014, za koristenje na predmetu PAI

Who changed what in which revision?

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