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