initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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