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