SD card interface
mbed-export/Ticker.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 - Ticker |
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_TICKER_H |
lharoon | 0:22612ae617a0 | 6 | #define MBED_TICKER_H |
lharoon | 0:22612ae617a0 | 7 | |
lharoon | 0:22612ae617a0 | 8 | #include "TimerEvent.h" |
lharoon | 0:22612ae617a0 | 9 | #include "FunctionPointer.h" |
lharoon | 0:22612ae617a0 | 10 | |
lharoon | 0:22612ae617a0 | 11 | namespace mbed { |
lharoon | 0:22612ae617a0 | 12 | |
lharoon | 0:22612ae617a0 | 13 | /* Class: Ticker |
lharoon | 0:22612ae617a0 | 14 | * A Ticker is used to call a function at a recurring interval |
lharoon | 0:22612ae617a0 | 15 | * |
lharoon | 0:22612ae617a0 | 16 | * You can use as many seperate Ticker objects as you require. |
lharoon | 0:22612ae617a0 | 17 | * |
lharoon | 0:22612ae617a0 | 18 | * Example: |
lharoon | 0:22612ae617a0 | 19 | * > // Toggle the blinking led after 5 seconds |
lharoon | 0:22612ae617a0 | 20 | * > |
lharoon | 0:22612ae617a0 | 21 | * > #include "mbed.h" |
lharoon | 0:22612ae617a0 | 22 | * > |
lharoon | 0:22612ae617a0 | 23 | * > Ticker timer; |
lharoon | 0:22612ae617a0 | 24 | * > DigitalOut led1(LED1); |
lharoon | 0:22612ae617a0 | 25 | * > DigitalOut led2(LED2); |
lharoon | 0:22612ae617a0 | 26 | * > |
lharoon | 0:22612ae617a0 | 27 | * > int flip = 0; |
lharoon | 0:22612ae617a0 | 28 | * > |
lharoon | 0:22612ae617a0 | 29 | * > void attime() { |
lharoon | 0:22612ae617a0 | 30 | * > flip = !flip; |
lharoon | 0:22612ae617a0 | 31 | * > } |
lharoon | 0:22612ae617a0 | 32 | * > |
lharoon | 0:22612ae617a0 | 33 | * > int main() { |
lharoon | 0:22612ae617a0 | 34 | * > timer.attach(&attime, 5); |
lharoon | 0:22612ae617a0 | 35 | * > while(1) { |
lharoon | 0:22612ae617a0 | 36 | * > if(flip == 0) { |
lharoon | 0:22612ae617a0 | 37 | * > led1 = !led1; |
lharoon | 0:22612ae617a0 | 38 | * > } else { |
lharoon | 0:22612ae617a0 | 39 | * > led2 = !led2; |
lharoon | 0:22612ae617a0 | 40 | * > } |
lharoon | 0:22612ae617a0 | 41 | * > wait(0.2); |
lharoon | 0:22612ae617a0 | 42 | * > } |
lharoon | 0:22612ae617a0 | 43 | * > } |
lharoon | 0:22612ae617a0 | 44 | * |
lharoon | 0:22612ae617a0 | 45 | */ |
lharoon | 0:22612ae617a0 | 46 | class Ticker : public TimerEvent { |
lharoon | 0:22612ae617a0 | 47 | |
lharoon | 0:22612ae617a0 | 48 | public: |
lharoon | 0:22612ae617a0 | 49 | |
lharoon | 0:22612ae617a0 | 50 | /* Function: attach |
lharoon | 0:22612ae617a0 | 51 | * Attach a function to be called by the Ticker, specifiying the interval in seconds |
lharoon | 0:22612ae617a0 | 52 | * |
lharoon | 0:22612ae617a0 | 53 | * Variables: |
lharoon | 0:22612ae617a0 | 54 | * fptr - pointer to the function to be called |
lharoon | 0:22612ae617a0 | 55 | * t - the time between calls in seconds |
lharoon | 0:22612ae617a0 | 56 | */ |
lharoon | 0:22612ae617a0 | 57 | void attach(void (*fptr)(void), float t) { |
lharoon | 0:22612ae617a0 | 58 | attach_us(fptr, t * 1000000.0f); |
lharoon | 0:22612ae617a0 | 59 | } |
lharoon | 0:22612ae617a0 | 60 | |
lharoon | 0:22612ae617a0 | 61 | /* Function: attach |
lharoon | 0:22612ae617a0 | 62 | * Attach a member function to be called by the Ticker, specifiying the interval in seconds |
lharoon | 0:22612ae617a0 | 63 | * |
lharoon | 0:22612ae617a0 | 64 | * Variables: |
lharoon | 0:22612ae617a0 | 65 | * tptr - pointer to the object to call the member function on |
lharoon | 0:22612ae617a0 | 66 | * mptr - pointer to the member function to be called |
lharoon | 0:22612ae617a0 | 67 | * t - the time between calls in seconds |
lharoon | 0:22612ae617a0 | 68 | */ |
lharoon | 0:22612ae617a0 | 69 | template<typename T> |
lharoon | 0:22612ae617a0 | 70 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
lharoon | 0:22612ae617a0 | 71 | attach_us(tptr, mptr, t * 1000000.0f); |
lharoon | 0:22612ae617a0 | 72 | } |
lharoon | 0:22612ae617a0 | 73 | |
lharoon | 0:22612ae617a0 | 74 | /* Function: attach_us |
lharoon | 0:22612ae617a0 | 75 | * Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
lharoon | 0:22612ae617a0 | 76 | * |
lharoon | 0:22612ae617a0 | 77 | * Variables: |
lharoon | 0:22612ae617a0 | 78 | * fptr - pointer to the function to be called |
lharoon | 0:22612ae617a0 | 79 | * t - the time between calls in micro-seconds |
lharoon | 0:22612ae617a0 | 80 | */ |
lharoon | 0:22612ae617a0 | 81 | void attach_us(void (*fptr)(void), unsigned int t) { |
lharoon | 0:22612ae617a0 | 82 | _function.attach(fptr); |
lharoon | 0:22612ae617a0 | 83 | setup(t); |
lharoon | 0:22612ae617a0 | 84 | } |
lharoon | 0:22612ae617a0 | 85 | |
lharoon | 0:22612ae617a0 | 86 | /* Function: attach_us |
lharoon | 0:22612ae617a0 | 87 | * Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
lharoon | 0:22612ae617a0 | 88 | * |
lharoon | 0:22612ae617a0 | 89 | * Variables: |
lharoon | 0:22612ae617a0 | 90 | * tptr - pointer to the object to call the member function on |
lharoon | 0:22612ae617a0 | 91 | * mptr - pointer to the member function to be called |
lharoon | 0:22612ae617a0 | 92 | * t - the time between calls in micro-seconds |
lharoon | 0:22612ae617a0 | 93 | */ |
lharoon | 0:22612ae617a0 | 94 | template<typename T> |
lharoon | 0:22612ae617a0 | 95 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
lharoon | 0:22612ae617a0 | 96 | _function.attach(tptr, mptr); |
lharoon | 0:22612ae617a0 | 97 | setup(t); |
lharoon | 0:22612ae617a0 | 98 | } |
lharoon | 0:22612ae617a0 | 99 | |
lharoon | 0:22612ae617a0 | 100 | /* Function: detach |
lharoon | 0:22612ae617a0 | 101 | * Detach the function |
lharoon | 0:22612ae617a0 | 102 | */ |
lharoon | 0:22612ae617a0 | 103 | void detach(); |
lharoon | 0:22612ae617a0 | 104 | |
lharoon | 0:22612ae617a0 | 105 | protected: |
lharoon | 0:22612ae617a0 | 106 | |
lharoon | 0:22612ae617a0 | 107 | void setup(unsigned int t); |
lharoon | 0:22612ae617a0 | 108 | virtual void handler(); |
lharoon | 0:22612ae617a0 | 109 | |
lharoon | 0:22612ae617a0 | 110 | unsigned int _delay; |
lharoon | 0:22612ae617a0 | 111 | FunctionPointer _function; |
lharoon | 0:22612ae617a0 | 112 | |
lharoon | 0:22612ae617a0 | 113 | }; |
lharoon | 0:22612ae617a0 | 114 | |
lharoon | 0:22612ae617a0 | 115 | } // namespace mbed |
lharoon | 0:22612ae617a0 | 116 | |
lharoon | 0:22612ae617a0 | 117 | #endif |