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