BLE temperature profile using digital DS1820 or analog LM35 sensors
mbed/Ticker.h@0:637031152314, 2015-03-07 (annotated)
- Committer:
- gkroussos
- Date:
- Sat Mar 07 16:23:41 2015 +0000
- Revision:
- 0:637031152314
Working version 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gkroussos | 0:637031152314 | 1 | /* mbed Microcontroller Library |
gkroussos | 0:637031152314 | 2 | * Copyright (c) 2006-2013 ARM Limited |
gkroussos | 0:637031152314 | 3 | * |
gkroussos | 0:637031152314 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
gkroussos | 0:637031152314 | 5 | * you may not use this file except in compliance with the License. |
gkroussos | 0:637031152314 | 6 | * You may obtain a copy of the License at |
gkroussos | 0:637031152314 | 7 | * |
gkroussos | 0:637031152314 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
gkroussos | 0:637031152314 | 9 | * |
gkroussos | 0:637031152314 | 10 | * Unless required by applicable law or agreed to in writing, software |
gkroussos | 0:637031152314 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
gkroussos | 0:637031152314 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
gkroussos | 0:637031152314 | 13 | * See the License for the specific language governing permissions and |
gkroussos | 0:637031152314 | 14 | * limitations under the License. |
gkroussos | 0:637031152314 | 15 | */ |
gkroussos | 0:637031152314 | 16 | #ifndef MBED_TICKER_H |
gkroussos | 0:637031152314 | 17 | #define MBED_TICKER_H |
gkroussos | 0:637031152314 | 18 | |
gkroussos | 0:637031152314 | 19 | #include "TimerEvent.h" |
gkroussos | 0:637031152314 | 20 | #include "FunctionPointer.h" |
gkroussos | 0:637031152314 | 21 | |
gkroussos | 0:637031152314 | 22 | namespace mbed { |
gkroussos | 0:637031152314 | 23 | |
gkroussos | 0:637031152314 | 24 | /** A Ticker is used to call a function at a recurring interval |
gkroussos | 0:637031152314 | 25 | * |
gkroussos | 0:637031152314 | 26 | * You can use as many seperate Ticker objects as you require. |
gkroussos | 0:637031152314 | 27 | * |
gkroussos | 0:637031152314 | 28 | * Example: |
gkroussos | 0:637031152314 | 29 | * @code |
gkroussos | 0:637031152314 | 30 | * // Toggle the blinking led after 5 seconds |
gkroussos | 0:637031152314 | 31 | * |
gkroussos | 0:637031152314 | 32 | * #include "mbed.h" |
gkroussos | 0:637031152314 | 33 | * |
gkroussos | 0:637031152314 | 34 | * Ticker timer; |
gkroussos | 0:637031152314 | 35 | * DigitalOut led1(LED1); |
gkroussos | 0:637031152314 | 36 | * DigitalOut led2(LED2); |
gkroussos | 0:637031152314 | 37 | * |
gkroussos | 0:637031152314 | 38 | * int flip = 0; |
gkroussos | 0:637031152314 | 39 | * |
gkroussos | 0:637031152314 | 40 | * void attime() { |
gkroussos | 0:637031152314 | 41 | * flip = !flip; |
gkroussos | 0:637031152314 | 42 | * } |
gkroussos | 0:637031152314 | 43 | * |
gkroussos | 0:637031152314 | 44 | * int main() { |
gkroussos | 0:637031152314 | 45 | * timer.attach(&attime, 5); |
gkroussos | 0:637031152314 | 46 | * while(1) { |
gkroussos | 0:637031152314 | 47 | * if(flip == 0) { |
gkroussos | 0:637031152314 | 48 | * led1 = !led1; |
gkroussos | 0:637031152314 | 49 | * } else { |
gkroussos | 0:637031152314 | 50 | * led2 = !led2; |
gkroussos | 0:637031152314 | 51 | * } |
gkroussos | 0:637031152314 | 52 | * wait(0.2); |
gkroussos | 0:637031152314 | 53 | * } |
gkroussos | 0:637031152314 | 54 | * } |
gkroussos | 0:637031152314 | 55 | * @endcode |
gkroussos | 0:637031152314 | 56 | */ |
gkroussos | 0:637031152314 | 57 | class Ticker : public TimerEvent { |
gkroussos | 0:637031152314 | 58 | |
gkroussos | 0:637031152314 | 59 | public: |
gkroussos | 0:637031152314 | 60 | |
gkroussos | 0:637031152314 | 61 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
gkroussos | 0:637031152314 | 62 | * |
gkroussos | 0:637031152314 | 63 | * @param fptr pointer to the function to be called |
gkroussos | 0:637031152314 | 64 | * @param t the time between calls in seconds |
gkroussos | 0:637031152314 | 65 | */ |
gkroussos | 0:637031152314 | 66 | void attach(void (*fptr)(void), float t) { |
gkroussos | 0:637031152314 | 67 | attach_us(fptr, t * 1000000.0f); |
gkroussos | 0:637031152314 | 68 | } |
gkroussos | 0:637031152314 | 69 | |
gkroussos | 0:637031152314 | 70 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
gkroussos | 0:637031152314 | 71 | * |
gkroussos | 0:637031152314 | 72 | * @param tptr pointer to the object to call the member function on |
gkroussos | 0:637031152314 | 73 | * @param mptr pointer to the member function to be called |
gkroussos | 0:637031152314 | 74 | * @param t the time between calls in seconds |
gkroussos | 0:637031152314 | 75 | */ |
gkroussos | 0:637031152314 | 76 | template<typename T> |
gkroussos | 0:637031152314 | 77 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
gkroussos | 0:637031152314 | 78 | attach_us(tptr, mptr, t * 1000000.0f); |
gkroussos | 0:637031152314 | 79 | } |
gkroussos | 0:637031152314 | 80 | |
gkroussos | 0:637031152314 | 81 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
gkroussos | 0:637031152314 | 82 | * |
gkroussos | 0:637031152314 | 83 | * @param fptr pointer to the function to be called |
gkroussos | 0:637031152314 | 84 | * @param t the time between calls in micro-seconds |
gkroussos | 0:637031152314 | 85 | */ |
gkroussos | 0:637031152314 | 86 | void attach_us(void (*fptr)(void), unsigned int t) { |
gkroussos | 0:637031152314 | 87 | _function.attach(fptr); |
gkroussos | 0:637031152314 | 88 | setup(t); |
gkroussos | 0:637031152314 | 89 | } |
gkroussos | 0:637031152314 | 90 | |
gkroussos | 0:637031152314 | 91 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
gkroussos | 0:637031152314 | 92 | * |
gkroussos | 0:637031152314 | 93 | * @param tptr pointer to the object to call the member function on |
gkroussos | 0:637031152314 | 94 | * @param mptr pointer to the member function to be called |
gkroussos | 0:637031152314 | 95 | * @param t the time between calls in micro-seconds |
gkroussos | 0:637031152314 | 96 | */ |
gkroussos | 0:637031152314 | 97 | template<typename T> |
gkroussos | 0:637031152314 | 98 | void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { |
gkroussos | 0:637031152314 | 99 | _function.attach(tptr, mptr); |
gkroussos | 0:637031152314 | 100 | setup(t); |
gkroussos | 0:637031152314 | 101 | } |
gkroussos | 0:637031152314 | 102 | |
gkroussos | 0:637031152314 | 103 | /** Detach the function |
gkroussos | 0:637031152314 | 104 | */ |
gkroussos | 0:637031152314 | 105 | void detach(); |
gkroussos | 0:637031152314 | 106 | |
gkroussos | 0:637031152314 | 107 | protected: |
gkroussos | 0:637031152314 | 108 | void setup(unsigned int t); |
gkroussos | 0:637031152314 | 109 | virtual void handler(); |
gkroussos | 0:637031152314 | 110 | |
gkroussos | 0:637031152314 | 111 | unsigned int _delay; |
gkroussos | 0:637031152314 | 112 | FunctionPointer _function; |
gkroussos | 0:637031152314 | 113 | }; |
gkroussos | 0:637031152314 | 114 | |
gkroussos | 0:637031152314 | 115 | } // namespace mbed |
gkroussos | 0:637031152314 | 116 | |
gkroussos | 0:637031152314 | 117 | #endif |