dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/hal/api/Timer.h@1:55a6170b404f, 2016-09-17 (annotated)
- Committer:
- nexpaq
- Date:
- Sat Sep 17 16:32:05 2016 +0000
- Revision:
- 1:55a6170b404f
checking in for sharing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | /* mbed Microcontroller Library |
nexpaq | 1:55a6170b404f | 2 | * Copyright (c) 2006-2013 ARM Limited |
nexpaq | 1:55a6170b404f | 3 | * |
nexpaq | 1:55a6170b404f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 1:55a6170b404f | 5 | * you may not use this file except in compliance with the License. |
nexpaq | 1:55a6170b404f | 6 | * You may obtain a copy of the License at |
nexpaq | 1:55a6170b404f | 7 | * |
nexpaq | 1:55a6170b404f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 1:55a6170b404f | 9 | * |
nexpaq | 1:55a6170b404f | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 1:55a6170b404f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 1:55a6170b404f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 1:55a6170b404f | 13 | * See the License for the specific language governing permissions and |
nexpaq | 1:55a6170b404f | 14 | * limitations under the License. |
nexpaq | 1:55a6170b404f | 15 | */ |
nexpaq | 1:55a6170b404f | 16 | #ifndef MBED_TIMER_H |
nexpaq | 1:55a6170b404f | 17 | #define MBED_TIMER_H |
nexpaq | 1:55a6170b404f | 18 | |
nexpaq | 1:55a6170b404f | 19 | #include "platform.h" |
nexpaq | 1:55a6170b404f | 20 | #include "ticker_api.h" |
nexpaq | 1:55a6170b404f | 21 | |
nexpaq | 1:55a6170b404f | 22 | namespace mbed { |
nexpaq | 1:55a6170b404f | 23 | |
nexpaq | 1:55a6170b404f | 24 | /** A general purpose timer |
nexpaq | 1:55a6170b404f | 25 | * |
nexpaq | 1:55a6170b404f | 26 | * @Note Synchronization level: Interrupt safe |
nexpaq | 1:55a6170b404f | 27 | * |
nexpaq | 1:55a6170b404f | 28 | * Example: |
nexpaq | 1:55a6170b404f | 29 | * @code |
nexpaq | 1:55a6170b404f | 30 | * // Count the time to toggle a LED |
nexpaq | 1:55a6170b404f | 31 | * |
nexpaq | 1:55a6170b404f | 32 | * #include "mbed.h" |
nexpaq | 1:55a6170b404f | 33 | * |
nexpaq | 1:55a6170b404f | 34 | * Timer timer; |
nexpaq | 1:55a6170b404f | 35 | * DigitalOut led(LED1); |
nexpaq | 1:55a6170b404f | 36 | * int begin, end; |
nexpaq | 1:55a6170b404f | 37 | * |
nexpaq | 1:55a6170b404f | 38 | * int main() { |
nexpaq | 1:55a6170b404f | 39 | * timer.start(); |
nexpaq | 1:55a6170b404f | 40 | * begin = timer.read_us(); |
nexpaq | 1:55a6170b404f | 41 | * led = !led; |
nexpaq | 1:55a6170b404f | 42 | * end = timer.read_us(); |
nexpaq | 1:55a6170b404f | 43 | * printf("Toggle the led takes %d us", end - begin); |
nexpaq | 1:55a6170b404f | 44 | * } |
nexpaq | 1:55a6170b404f | 45 | * @endcode |
nexpaq | 1:55a6170b404f | 46 | */ |
nexpaq | 1:55a6170b404f | 47 | class Timer { |
nexpaq | 1:55a6170b404f | 48 | |
nexpaq | 1:55a6170b404f | 49 | public: |
nexpaq | 1:55a6170b404f | 50 | Timer(); |
nexpaq | 1:55a6170b404f | 51 | Timer(const ticker_data_t *data); |
nexpaq | 1:55a6170b404f | 52 | |
nexpaq | 1:55a6170b404f | 53 | /** Start the timer |
nexpaq | 1:55a6170b404f | 54 | */ |
nexpaq | 1:55a6170b404f | 55 | void start(); |
nexpaq | 1:55a6170b404f | 56 | |
nexpaq | 1:55a6170b404f | 57 | /** Stop the timer |
nexpaq | 1:55a6170b404f | 58 | */ |
nexpaq | 1:55a6170b404f | 59 | void stop(); |
nexpaq | 1:55a6170b404f | 60 | |
nexpaq | 1:55a6170b404f | 61 | /** Reset the timer to 0. |
nexpaq | 1:55a6170b404f | 62 | * |
nexpaq | 1:55a6170b404f | 63 | * If it was already counting, it will continue |
nexpaq | 1:55a6170b404f | 64 | */ |
nexpaq | 1:55a6170b404f | 65 | void reset(); |
nexpaq | 1:55a6170b404f | 66 | |
nexpaq | 1:55a6170b404f | 67 | /** Get the time passed in seconds |
nexpaq | 1:55a6170b404f | 68 | */ |
nexpaq | 1:55a6170b404f | 69 | float read(); |
nexpaq | 1:55a6170b404f | 70 | |
nexpaq | 1:55a6170b404f | 71 | /** Get the time passed in mili-seconds |
nexpaq | 1:55a6170b404f | 72 | */ |
nexpaq | 1:55a6170b404f | 73 | int read_ms(); |
nexpaq | 1:55a6170b404f | 74 | |
nexpaq | 1:55a6170b404f | 75 | /** Get the time passed in micro-seconds |
nexpaq | 1:55a6170b404f | 76 | */ |
nexpaq | 1:55a6170b404f | 77 | int read_us(); |
nexpaq | 1:55a6170b404f | 78 | |
nexpaq | 1:55a6170b404f | 79 | /** An operator shorthand for read() |
nexpaq | 1:55a6170b404f | 80 | */ |
nexpaq | 1:55a6170b404f | 81 | operator float(); |
nexpaq | 1:55a6170b404f | 82 | |
nexpaq | 1:55a6170b404f | 83 | protected: |
nexpaq | 1:55a6170b404f | 84 | int slicetime(); |
nexpaq | 1:55a6170b404f | 85 | int _running; // whether the timer is running |
nexpaq | 1:55a6170b404f | 86 | unsigned int _start; // the start time of the latest slice |
nexpaq | 1:55a6170b404f | 87 | int _time; // any accumulated time from previous slices |
nexpaq | 1:55a6170b404f | 88 | const ticker_data_t *_ticker_data; |
nexpaq | 1:55a6170b404f | 89 | }; |
nexpaq | 1:55a6170b404f | 90 | |
nexpaq | 1:55a6170b404f | 91 | } // namespace mbed |
nexpaq | 1:55a6170b404f | 92 | |
nexpaq | 1:55a6170b404f | 93 | #endif |