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