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