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