mbed libraries for KL25Z
Timeout.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 - Timeout |
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_TIMEOUT_H |
emilmont | 0:8024c367e29f | 6 | #define MBED_TIMEOUT_H |
emilmont | 0:8024c367e29f | 7 | |
emilmont | 0:8024c367e29f | 8 | #include "Ticker.h" |
emilmont | 0:8024c367e29f | 9 | |
emilmont | 0:8024c367e29f | 10 | namespace mbed { |
emilmont | 0:8024c367e29f | 11 | |
emilmont | 0:8024c367e29f | 12 | /* Class: Timeout |
emilmont | 0:8024c367e29f | 13 | * A Timeout is used to call a function at a point in the future |
emilmont | 0:8024c367e29f | 14 | * |
emilmont | 0:8024c367e29f | 15 | * You can use as many seperate Timeout objects as you require. |
emilmont | 0:8024c367e29f | 16 | * |
emilmont | 0:8024c367e29f | 17 | * Example: |
emilmont | 0:8024c367e29f | 18 | * > // Blink until timeout. |
emilmont | 0:8024c367e29f | 19 | * > |
emilmont | 0:8024c367e29f | 20 | * > #include "mbed.h" |
emilmont | 0:8024c367e29f | 21 | * > |
emilmont | 0:8024c367e29f | 22 | * > Timeout timeout; |
emilmont | 0:8024c367e29f | 23 | * > DigitalOut led(LED1); |
emilmont | 0:8024c367e29f | 24 | * > |
emilmont | 0:8024c367e29f | 25 | * > int on = 1; |
emilmont | 0:8024c367e29f | 26 | * > |
emilmont | 0:8024c367e29f | 27 | * > void attimeout() { |
emilmont | 0:8024c367e29f | 28 | * > on = 0; |
emilmont | 0:8024c367e29f | 29 | * > } |
emilmont | 0:8024c367e29f | 30 | * > |
emilmont | 0:8024c367e29f | 31 | * > int main() { |
emilmont | 0:8024c367e29f | 32 | * > timeout.attach(&attimeout, 5); |
emilmont | 0:8024c367e29f | 33 | * > while(on) { |
emilmont | 0:8024c367e29f | 34 | * > led = !led; |
emilmont | 0:8024c367e29f | 35 | * > wait(0.2); |
emilmont | 0:8024c367e29f | 36 | * > } |
emilmont | 0:8024c367e29f | 37 | * > } |
emilmont | 0:8024c367e29f | 38 | */ |
emilmont | 0:8024c367e29f | 39 | class Timeout : public Ticker { |
emilmont | 0:8024c367e29f | 40 | |
emilmont | 0:8024c367e29f | 41 | #if 0 // For documentation |
emilmont | 0:8024c367e29f | 42 | |
emilmont | 0:8024c367e29f | 43 | /* Function: attach |
emilmont | 0:8024c367e29f | 44 | * Attach a function to be called by the Timeout, specifiying the delay in seconds |
emilmont | 0:8024c367e29f | 45 | * |
emilmont | 0:8024c367e29f | 46 | * Variables: |
emilmont | 0:8024c367e29f | 47 | * fptr - pointer to the function to be called |
emilmont | 0:8024c367e29f | 48 | * t - the time before the call in seconds |
emilmont | 0:8024c367e29f | 49 | */ |
emilmont | 0:8024c367e29f | 50 | void attach(void (*fptr)(void), float t) { |
emilmont | 0:8024c367e29f | 51 | attach_us(fptr, t * 1000000.0f); |
emilmont | 0:8024c367e29f | 52 | } |
emilmont | 0:8024c367e29f | 53 | |
emilmont | 0:8024c367e29f | 54 | /* Function: attach |
emilmont | 0:8024c367e29f | 55 | * Attach a member function to be called by the Timeout, specifiying the delay in seconds |
emilmont | 0:8024c367e29f | 56 | * |
emilmont | 0:8024c367e29f | 57 | * Variables: |
emilmont | 0:8024c367e29f | 58 | * tptr - pointer to the object to call the member function on |
emilmont | 0:8024c367e29f | 59 | * mptr - pointer to the member function to be called |
emilmont | 0:8024c367e29f | 60 | * t - the time before the calls in seconds |
emilmont | 0:8024c367e29f | 61 | */ |
emilmont | 0:8024c367e29f | 62 | template<typename T> |
emilmont | 0:8024c367e29f | 63 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
emilmont | 0:8024c367e29f | 64 | attach_us(tptr, mptr, t * 1000000.0f); |
emilmont | 0:8024c367e29f | 65 | } |
emilmont | 0:8024c367e29f | 66 | |
emilmont | 0:8024c367e29f | 67 | /* Function: attach_us |
emilmont | 0:8024c367e29f | 68 | * Attach a function to be called by the Timeout, specifiying the delay in micro-seconds |
emilmont | 0:8024c367e29f | 69 | * |
emilmont | 0:8024c367e29f | 70 | * Variables: |
emilmont | 0:8024c367e29f | 71 | * fptr - pointer to the function to be called |
emilmont | 0:8024c367e29f | 72 | * t - the time before the call 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 | 0:8024c367e29f | 79 | /* Function: attach_us |
emilmont | 0:8024c367e29f | 80 | * Attach a member function to be called by the Timeout, specifiying the delay in micro-seconds |
emilmont | 0:8024c367e29f | 81 | * |
emilmont | 0:8024c367e29f | 82 | * Variables: |
emilmont | 0:8024c367e29f | 83 | * tptr - pointer to the object to call the member function on |
emilmont | 0:8024c367e29f | 84 | * mptr - pointer to the member function to be called |
emilmont | 0:8024c367e29f | 85 | * t - the time before the call in micro-seconds |
emilmont | 0:8024c367e29f | 86 | */ |
emilmont | 0:8024c367e29f | 87 | template<typename T> |
emilmont | 0:8024c367e29f | 88 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
emilmont | 0:8024c367e29f | 89 | _function.attach(tptr, mptr); |
emilmont | 0:8024c367e29f | 90 | setup(t); |
emilmont | 0:8024c367e29f | 91 | } |
emilmont | 0:8024c367e29f | 92 | |
emilmont | 0:8024c367e29f | 93 | /* Function: detach |
emilmont | 0:8024c367e29f | 94 | * Detach the function |
emilmont | 0:8024c367e29f | 95 | */ |
emilmont | 0:8024c367e29f | 96 | void detach(); |
emilmont | 0:8024c367e29f | 97 | |
emilmont | 0:8024c367e29f | 98 | #endif |
emilmont | 0:8024c367e29f | 99 | |
emilmont | 0:8024c367e29f | 100 | protected: |
emilmont | 0:8024c367e29f | 101 | |
emilmont | 0:8024c367e29f | 102 | virtual void handler(); |
emilmont | 0:8024c367e29f | 103 | |
emilmont | 0:8024c367e29f | 104 | }; |
emilmont | 0:8024c367e29f | 105 | |
emilmont | 0:8024c367e29f | 106 | } // namespace mbed |
emilmont | 0:8024c367e29f | 107 | |
emilmont | 0:8024c367e29f | 108 | #endif |