mbed libraries for KL25Z
Ticker.h@8:c14af7958ef5, 2012-11-09 (annotated)
- Committer:
- emilmont
- Date:
- Fri Nov 09 11:33:53 2012 +0000
- Revision:
- 8:c14af7958ef5
- Parent:
- 0:8024c367e29f
- Child:
- 9:663789d7729f
SPI driver; ADC driver; DAC driver; microlib support; general bugfixing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 0:8024c367e29f | 1 | /* mbed Microcontroller Library - Ticker |
emilmont | 0:8024c367e29f | 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
emilmont | 8:c14af7958ef5 | 3 | */ |
emilmont | 0:8024c367e29f | 4 | #ifndef MBED_TICKER_H |
emilmont | 0:8024c367e29f | 5 | #define MBED_TICKER_H |
emilmont | 0:8024c367e29f | 6 | |
emilmont | 0:8024c367e29f | 7 | #include "TimerEvent.h" |
emilmont | 0:8024c367e29f | 8 | #include "FunctionPointer.h" |
emilmont | 0:8024c367e29f | 9 | |
emilmont | 0:8024c367e29f | 10 | namespace mbed { |
emilmont | 0:8024c367e29f | 11 | |
emilmont | 8:c14af7958ef5 | 12 | /** A Ticker is used to call a function at a recurring interval |
emilmont | 0:8024c367e29f | 13 | * |
emilmont | 8:c14af7958ef5 | 14 | * You can use as many seperate Ticker objects as you require. |
emilmont | 0:8024c367e29f | 15 | * |
emilmont | 0:8024c367e29f | 16 | * Example: |
emilmont | 8:c14af7958ef5 | 17 | * @code |
emilmont | 8:c14af7958ef5 | 18 | * // Toggle the blinking led after 5 seconds |
emilmont | 8:c14af7958ef5 | 19 | * |
emilmont | 8:c14af7958ef5 | 20 | * #include "mbed.h" |
emilmont | 0:8024c367e29f | 21 | * |
emilmont | 8:c14af7958ef5 | 22 | * Ticker timer; |
emilmont | 8:c14af7958ef5 | 23 | * DigitalOut led1(LED1); |
emilmont | 8:c14af7958ef5 | 24 | * DigitalOut led2(LED2); |
emilmont | 8:c14af7958ef5 | 25 | * |
emilmont | 8:c14af7958ef5 | 26 | * int flip = 0; |
emilmont | 8:c14af7958ef5 | 27 | * |
emilmont | 8:c14af7958ef5 | 28 | * void attime() { |
emilmont | 8:c14af7958ef5 | 29 | * flip = !flip; |
emilmont | 8:c14af7958ef5 | 30 | * } |
emilmont | 8:c14af7958ef5 | 31 | * |
emilmont | 8:c14af7958ef5 | 32 | * int main() { |
emilmont | 8:c14af7958ef5 | 33 | * timer.attach(&attime, 5); |
emilmont | 8:c14af7958ef5 | 34 | * while(1) { |
emilmont | 8:c14af7958ef5 | 35 | * if(flip == 0) { |
emilmont | 8:c14af7958ef5 | 36 | * led1 = !led1; |
emilmont | 8:c14af7958ef5 | 37 | * } else { |
emilmont | 8:c14af7958ef5 | 38 | * led2 = !led2; |
emilmont | 8:c14af7958ef5 | 39 | * } |
emilmont | 8:c14af7958ef5 | 40 | * wait(0.2); |
emilmont | 8:c14af7958ef5 | 41 | * } |
emilmont | 8:c14af7958ef5 | 42 | * } |
emilmont | 8:c14af7958ef5 | 43 | * @endcode |
emilmont | 0:8024c367e29f | 44 | */ |
emilmont | 0:8024c367e29f | 45 | class Ticker : public TimerEvent { |
emilmont | 0:8024c367e29f | 46 | |
emilmont | 0:8024c367e29f | 47 | public: |
emilmont | 0:8024c367e29f | 48 | |
emilmont | 8:c14af7958ef5 | 49 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
emilmont | 8:c14af7958ef5 | 50 | * |
emilmont | 8:c14af7958ef5 | 51 | * @param fptr pointer to the function to be called |
emilmont | 8:c14af7958ef5 | 52 | * @param t the time between calls in seconds |
emilmont | 0:8024c367e29f | 53 | */ |
emilmont | 0:8024c367e29f | 54 | void attach(void (*fptr)(void), float t) { |
emilmont | 0:8024c367e29f | 55 | attach_us(fptr, t * 1000000.0f); |
emilmont | 0:8024c367e29f | 56 | } |
emilmont | 0:8024c367e29f | 57 | |
emilmont | 8:c14af7958ef5 | 58 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
emilmont | 8:c14af7958ef5 | 59 | * |
emilmont | 8:c14af7958ef5 | 60 | * @param tptr pointer to the object to call the member function on |
emilmont | 8:c14af7958ef5 | 61 | * @param mptr pointer to the member function to be called |
emilmont | 8:c14af7958ef5 | 62 | * @param t the time between calls in seconds |
emilmont | 0:8024c367e29f | 63 | */ |
emilmont | 0:8024c367e29f | 64 | template<typename T> |
emilmont | 0:8024c367e29f | 65 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
emilmont | 0:8024c367e29f | 66 | attach_us(tptr, mptr, t * 1000000.0f); |
emilmont | 0:8024c367e29f | 67 | } |
emilmont | 0:8024c367e29f | 68 | |
emilmont | 8:c14af7958ef5 | 69 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
emilmont | 8:c14af7958ef5 | 70 | * |
emilmont | 8:c14af7958ef5 | 71 | * @param fptr pointer to the function to be called |
emilmont | 8:c14af7958ef5 | 72 | * @param t the time between calls in micro-seconds |
emilmont | 0:8024c367e29f | 73 | */ |
emilmont | 0:8024c367e29f | 74 | void attach_us(void (*fptr)(void), unsigned int t) { |
emilmont | 0:8024c367e29f | 75 | _function.attach(fptr); |
emilmont | 0:8024c367e29f | 76 | setup(t); |
emilmont | 0:8024c367e29f | 77 | } |
emilmont | 0:8024c367e29f | 78 | |
emilmont | 8:c14af7958ef5 | 79 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
emilmont | 8:c14af7958ef5 | 80 | * |
emilmont | 8:c14af7958ef5 | 81 | * @param tptr pointer to the object to call the member function on |
emilmont | 8:c14af7958ef5 | 82 | * @param mptr pointer to the member function to be called |
emilmont | 8:c14af7958ef5 | 83 | * @param t the time between calls in micro-seconds |
emilmont | 0:8024c367e29f | 84 | */ |
emilmont | 0:8024c367e29f | 85 | template<typename T> |
emilmont | 0:8024c367e29f | 86 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
emilmont | 0:8024c367e29f | 87 | _function.attach(tptr, mptr); |
emilmont | 0:8024c367e29f | 88 | setup(t); |
emilmont | 0:8024c367e29f | 89 | } |
emilmont | 0:8024c367e29f | 90 | |
emilmont | 8:c14af7958ef5 | 91 | /** Detach the function |
emilmont | 8:c14af7958ef5 | 92 | */ |
emilmont | 0:8024c367e29f | 93 | void detach(); |
emilmont | 0:8024c367e29f | 94 | |
emilmont | 0:8024c367e29f | 95 | protected: |
emilmont | 0:8024c367e29f | 96 | void setup(unsigned int t); |
emilmont | 0:8024c367e29f | 97 | virtual void handler(); |
emilmont | 8:c14af7958ef5 | 98 | |
emilmont | 0:8024c367e29f | 99 | unsigned int _delay; |
emilmont | 0:8024c367e29f | 100 | FunctionPointer _function; |
emilmont | 0:8024c367e29f | 101 | }; |
emilmont | 0:8024c367e29f | 102 | |
emilmont | 0:8024c367e29f | 103 | } // namespace mbed |
emilmont | 0:8024c367e29f | 104 | |
emilmont | 0:8024c367e29f | 105 | #endif |