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