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