]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/common/Timer.cpp
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / common / Timer.cpp
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 #include "Timer.h"
17 #include "us_ticker_api.h"
18
19 namespace mbed {
20
21 Timer::Timer() : _running(), _start(), _time() {
22     reset();
23 }
24
25 void Timer::start() {
26     if (!_running) {
27         _start = us_ticker_read();
28         _running = 1;
29     }
30 }
31
32 void Timer::stop() {
33     _time += slicetime();
34     _running = 0;
35 }
36
37 int Timer::read_us() {
38     return _time + slicetime();
39 }
40
41 float Timer::read() {
42     return (float)read_us() / 1000000.0f;
43 }
44
45 int Timer::read_ms() {
46     return read_us() / 1000;
47 }
48
49 int Timer::slicetime() {
50     if (_running) {
51         return us_ticker_read() - _start;
52     } else {
53         return 0;
54     }
55 }
56
57 void Timer::reset() {
58     _start = us_ticker_read();
59     _time = 0;
60 }
61
62 #ifdef MBED_OPERATORS
63 Timer::operator float() {
64     return read();
65 }
66 #endif
67
68 } // namespace mbed