mbed libraries for KL25Z
Ticker.h@0:8024c367e29f, 2012-10-05 (annotated)
- Committer:
- emilmont
- Date:
- Fri Oct 05 09:16:41 2012 +0000
- Revision:
- 0:8024c367e29f
- Child:
- 8:c14af7958ef5
First release of the mbed libraries for KL25Z
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 | 0:8024c367e29f | 3 | */ |
emilmont | 0:8024c367e29f | 4 | |
emilmont | 0:8024c367e29f | 5 | #ifndef MBED_TICKER_H |
emilmont | 0:8024c367e29f | 6 | #define MBED_TICKER_H |
emilmont | 0:8024c367e29f | 7 | |
emilmont | 0:8024c367e29f | 8 | #include "TimerEvent.h" |
emilmont | 0:8024c367e29f | 9 | #include "FunctionPointer.h" |
emilmont | 0:8024c367e29f | 10 | |
emilmont | 0:8024c367e29f | 11 | namespace mbed { |
emilmont | 0:8024c367e29f | 12 | |
emilmont | 0:8024c367e29f | 13 | /* Class: Ticker |
emilmont | 0:8024c367e29f | 14 | * A Ticker is used to call a function at a recurring interval |
emilmont | 0:8024c367e29f | 15 | * |
emilmont | 0:8024c367e29f | 16 | * You can use as many separate Ticker objects as you require. |
emilmont | 0:8024c367e29f | 17 | * |
emilmont | 0:8024c367e29f | 18 | * Example: |
emilmont | 0:8024c367e29f | 19 | * > // Toggle the blinking led after 5 seconds |
emilmont | 0:8024c367e29f | 20 | * > |
emilmont | 0:8024c367e29f | 21 | * > #include "mbed.h" |
emilmont | 0:8024c367e29f | 22 | * > |
emilmont | 0:8024c367e29f | 23 | * > Ticker timer; |
emilmont | 0:8024c367e29f | 24 | * > DigitalOut led1(LED1); |
emilmont | 0:8024c367e29f | 25 | * > DigitalOut led2(LED2); |
emilmont | 0:8024c367e29f | 26 | * > |
emilmont | 0:8024c367e29f | 27 | * > int flip = 0; |
emilmont | 0:8024c367e29f | 28 | * > |
emilmont | 0:8024c367e29f | 29 | * > void attime() { |
emilmont | 0:8024c367e29f | 30 | * > flip = !flip; |
emilmont | 0:8024c367e29f | 31 | * > } |
emilmont | 0:8024c367e29f | 32 | * > |
emilmont | 0:8024c367e29f | 33 | * > int main() { |
emilmont | 0:8024c367e29f | 34 | * > timer.attach(&attime, 5); |
emilmont | 0:8024c367e29f | 35 | * > while(1) { |
emilmont | 0:8024c367e29f | 36 | * > if(flip == 0) { |
emilmont | 0:8024c367e29f | 37 | * > led1 = !led1; |
emilmont | 0:8024c367e29f | 38 | * > } else { |
emilmont | 0:8024c367e29f | 39 | * > led2 = !led2; |
emilmont | 0:8024c367e29f | 40 | * > } |
emilmont | 0:8024c367e29f | 41 | * > wait(0.2); |
emilmont | 0:8024c367e29f | 42 | * > } |
emilmont | 0:8024c367e29f | 43 | * > } |
emilmont | 0:8024c367e29f | 44 | * |
emilmont | 0:8024c367e29f | 45 | */ |
emilmont | 0:8024c367e29f | 46 | class Ticker : public TimerEvent { |
emilmont | 0:8024c367e29f | 47 | |
emilmont | 0:8024c367e29f | 48 | public: |
emilmont | 0:8024c367e29f | 49 | |
emilmont | 0:8024c367e29f | 50 | /* Function: attach |
emilmont | 0:8024c367e29f | 51 | * Attach a function to be called by the Ticker, specifying the interval in seconds |
emilmont | 0:8024c367e29f | 52 | * |
emilmont | 0:8024c367e29f | 53 | * Variables: |
emilmont | 0:8024c367e29f | 54 | * fptr - pointer to the function to be called |
emilmont | 0:8024c367e29f | 55 | * t - the time between calls in seconds |
emilmont | 0:8024c367e29f | 56 | */ |
emilmont | 0:8024c367e29f | 57 | void attach(void (*fptr)(void), float t) { |
emilmont | 0:8024c367e29f | 58 | attach_us(fptr, t * 1000000.0f); |
emilmont | 0:8024c367e29f | 59 | } |
emilmont | 0:8024c367e29f | 60 | |
emilmont | 0:8024c367e29f | 61 | /* Function: attach |
emilmont | 0:8024c367e29f | 62 | * Attach a member function to be called by the Ticker, specifying the interval in seconds |
emilmont | 0:8024c367e29f | 63 | * |
emilmont | 0:8024c367e29f | 64 | * Variables: |
emilmont | 0:8024c367e29f | 65 | * tptr - pointer to the object to call the member function on |
emilmont | 0:8024c367e29f | 66 | * mptr - pointer to the member function to be called |
emilmont | 0:8024c367e29f | 67 | * t - the time between calls in seconds |
emilmont | 0:8024c367e29f | 68 | */ |
emilmont | 0:8024c367e29f | 69 | template<typename T> |
emilmont | 0:8024c367e29f | 70 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
emilmont | 0:8024c367e29f | 71 | attach_us(tptr, mptr, t * 1000000.0f); |
emilmont | 0:8024c367e29f | 72 | } |
emilmont | 0:8024c367e29f | 73 | |
emilmont | 0:8024c367e29f | 74 | /* Function: attach_us |
emilmont | 0:8024c367e29f | 75 | * Attach a function to be called by the Ticker, specifying the interval in micro-seconds |
emilmont | 0:8024c367e29f | 76 | * |
emilmont | 0:8024c367e29f | 77 | * Variables: |
emilmont | 0:8024c367e29f | 78 | * fptr - pointer to the function to be called |
emilmont | 0:8024c367e29f | 79 | * t - the time between calls in micro-seconds |
emilmont | 0:8024c367e29f | 80 | */ |
emilmont | 0:8024c367e29f | 81 | void attach_us(void (*fptr)(void), unsigned int t) { |
emilmont | 0:8024c367e29f | 82 | _function.attach(fptr); |
emilmont | 0:8024c367e29f | 83 | setup(t); |
emilmont | 0:8024c367e29f | 84 | } |
emilmont | 0:8024c367e29f | 85 | |
emilmont | 0:8024c367e29f | 86 | /* Function: attach_us |
emilmont | 0:8024c367e29f | 87 | * Attach a member function to be called by the Ticker, specifying the interval in micro-seconds |
emilmont | 0:8024c367e29f | 88 | * |
emilmont | 0:8024c367e29f | 89 | * Variables: |
emilmont | 0:8024c367e29f | 90 | * tptr - pointer to the object to call the member function on |
emilmont | 0:8024c367e29f | 91 | * mptr - pointer to the member function to be called |
emilmont | 0:8024c367e29f | 92 | * t - the time between calls in micro-seconds |
emilmont | 0:8024c367e29f | 93 | */ |
emilmont | 0:8024c367e29f | 94 | template<typename T> |
emilmont | 0:8024c367e29f | 95 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
emilmont | 0:8024c367e29f | 96 | _function.attach(tptr, mptr); |
emilmont | 0:8024c367e29f | 97 | setup(t); |
emilmont | 0:8024c367e29f | 98 | } |
emilmont | 0:8024c367e29f | 99 | |
emilmont | 0:8024c367e29f | 100 | /* Function: detach |
emilmont | 0:8024c367e29f | 101 | * Detach the function |
emilmont | 0:8024c367e29f | 102 | */ |
emilmont | 0:8024c367e29f | 103 | void detach(); |
emilmont | 0:8024c367e29f | 104 | |
emilmont | 0:8024c367e29f | 105 | protected: |
emilmont | 0:8024c367e29f | 106 | |
emilmont | 0:8024c367e29f | 107 | void setup(unsigned int t); |
emilmont | 0:8024c367e29f | 108 | virtual void handler(); |
emilmont | 0:8024c367e29f | 109 | |
emilmont | 0:8024c367e29f | 110 | unsigned int _delay; |
emilmont | 0:8024c367e29f | 111 | FunctionPointer _function; |
emilmont | 0:8024c367e29f | 112 | |
emilmont | 0:8024c367e29f | 113 | }; |
emilmont | 0:8024c367e29f | 114 | |
emilmont | 0:8024c367e29f | 115 | } // namespace mbed |
emilmont | 0:8024c367e29f | 116 | |
emilmont | 0:8024c367e29f | 117 | #endif |