]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/api/Timer.h
aedf0377e0007f9697fef87af0c4151b3c7cab8c
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / api / Timer.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_TIMER_H
17 #define MBED_TIMER_H
18
19 #include "platform.h"
20
21 namespace mbed {
22
23 /** A general purpose timer
24  *
25  * Example:
26  * @code
27  * // Count the time to toggle a LED
28  *
29  * #include "mbed.h"
30  *
31  * Timer timer;
32  * DigitalOut led(LED1);
33  * int begin, end;
34  *
35  * int main() {
36  *     timer.start();
37  *     begin = timer.read_us();
38  *     led = !led;
39  *     end = timer.read_us();
40  *     printf("Toggle the led takes %d us", end - begin);
41  * }
42  * @endcode
43  */
44 class Timer {
45
46 public:
47     Timer();
48
49     /** Start the timer
50      */
51     void start();
52
53     /** Stop the timer
54      */
55     void stop();
56
57     /** Reset the timer to 0.
58      *
59      * If it was already counting, it will continue
60      */
61     void reset();
62
63     /** Get the time passed in seconds
64      */
65     float read();
66
67     /** Get the time passed in mili-seconds
68      */
69     int read_ms();
70
71     /** Get the time passed in micro-seconds
72      */
73     int read_us();
74
75 #ifdef MBED_OPERATORS
76     operator float();
77 #endif
78
79 protected:
80     int slicetime();
81     int _running;          // whether the timer is running
82     unsigned int _start;   // the start time of the latest slice
83     int _time;             // any accumulated time from previous slices
84 };
85
86 } // namespace mbed
87
88 #endif