Lab 1 Program C
Fork of mbed by
Timeout.h@9:cf0d45ce28a6, 2009-04-15 (annotated)
- Committer:
- simon.ford@mbed.co.uk
- Date:
- Wed Apr 15 14:15:04 2009 +0000
- Revision:
- 9:cf0d45ce28a6
- Parent:
- 4:5d1359a283bc
- Child:
- 11:1c1ebd0324fa
Update library with fixes
* TimerEvent hang bugfix
* FileLike use as file pointer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 1 | /* mbed Microcontroller Library - Timeout |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 2 | * Copyright (c) 2007-2008, sford |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 3 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 4 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 5 | #ifndef MBED_TIMEOUT_H |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 6 | #define MBED_TIMEOUT_H |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 7 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 8 | #include "Ticker.h" |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 9 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 10 | namespace mbed { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 11 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 12 | /* Class: Timeout |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 13 | * A Timeout is used to call a function at a point in the future |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 14 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 15 | * You can use as many seperate Timeout objects as you require. |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 16 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 17 | class Timeout : public Ticker { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 18 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 19 | #if 0 // For documentation |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 20 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 21 | /* Function: attach |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 22 | * Attach a function to be called by the Timeout, specifiying the delay in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 23 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 24 | * Variables: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 25 | * fptr - pointer to the function to be called |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 26 | * t - the time before the call in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 27 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 28 | void attach(void (*fptr)(void), float t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 29 | attach_us(fptr, t * 1000000.0f); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 30 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 31 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 32 | /* Function: attach |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 33 | * Attach a member function to be called by the Timeout, specifiying the delay in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 34 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 35 | * Variables: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 36 | * tptr - pointer to the object to call the member function on |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 37 | * mptr - pointer to the member function to be called |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 38 | * t - the time before the calls in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 39 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 40 | template<typename T> |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 41 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 42 | attach_us(tptr, mptr, t * 1000000.0f); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 43 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 44 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 45 | /* Function: attach_us |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 46 | * Attach a function to be called by the Timeout, specifiying the delay in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 47 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 48 | * Variables: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 49 | * fptr - pointer to the function to be called |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 50 | * t - the time before the call in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 51 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 52 | void attach_us(void (*fptr)(void), unsigned int t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 53 | _function.attach(fptr); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 54 | setup(t); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 55 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 56 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 57 | /* Function: attach_us |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 58 | * Attach a member function to be called by the Timeout, specifiying the delay in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 59 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 60 | * Variables: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 61 | * tptr - pointer to the object to call the member function on |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 62 | * mptr - pointer to the member function to be called |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 63 | * t - the time before the call in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 64 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 65 | template<typename T> |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 66 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 67 | _function.attach(tptr, mptr); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 68 | setup(t); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 69 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 70 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 71 | /* Function: detach |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 72 | * Detach the function |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 73 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 74 | void detach(); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 75 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 76 | #endif |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 77 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 78 | protected: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 79 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 80 | virtual void handler(); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 81 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 82 | }; |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 83 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 84 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 85 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 86 | #endif |