Lab 1 Program C
Fork of mbed by
Ticker.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 - Ticker |
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_TICKER_H |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 6 | #define MBED_TICKER_H |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 7 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 8 | #include "TimerEvent.h" |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 9 | #include "FunctionPointer.h" |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 10 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 11 | namespace mbed { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 12 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 13 | /* Class: Ticker |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 14 | * A Ticker is used to call a function at a recurring interval |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 15 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 16 | * You can use as many seperate Ticker objects as you require. |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 17 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 18 | class Ticker : public TimerEvent { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 19 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 20 | public: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 21 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 22 | /* Function: attach |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 23 | * Attach a function to be called by the Ticker, specifiying the interval in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 24 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 25 | * Variables: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 26 | * fptr - pointer to the function to be called |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 27 | * t - the time between calls in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 28 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 29 | void attach(void (*fptr)(void), float t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 30 | attach_us(fptr, t * 1000000.0f); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 31 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 32 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 33 | /* Function: attach |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 34 | * Attach a member function to be called by the Ticker, specifiying the interval in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 35 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 36 | * Variables: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 37 | * tptr - pointer to the object to call the member function on |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 38 | * mptr - pointer to the member function to be called |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 39 | * t - the time between calls in seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 40 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 41 | template<typename T> |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 42 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 43 | attach_us(tptr, mptr, t * 1000000.0f); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 44 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 45 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 46 | /* Function: attach_us |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 47 | * Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 48 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 49 | * Variables: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 50 | * fptr - pointer to the function to be called |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 51 | * t - the time between calls in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 52 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 53 | void attach_us(void (*fptr)(void), unsigned int t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 54 | _function.attach(fptr); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 55 | setup(t); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 56 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 57 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 58 | /* Function: attach_us |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 59 | * Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 60 | * |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 61 | * Variables: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 62 | * tptr - pointer to the object to call the member function on |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 63 | * mptr - pointer to the member function to be called |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 64 | * t - the time between calls in micro-seconds |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 65 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 66 | template<typename T> |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 67 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 68 | _function.attach(tptr, mptr); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 69 | setup(t); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 70 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 71 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 72 | /* Function: detach |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 73 | * Detach the function |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 74 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 75 | void detach(); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 76 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 77 | protected: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 78 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 79 | void setup(unsigned int t); |
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 | unsigned int _delay; |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 83 | FunctionPointer _function; |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 84 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 85 | }; |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 86 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 87 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 88 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 89 | #endif |