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