forked

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Timer.h Source File

Timer.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_TIMER_H
00017 #define MBED_TIMER_H
00018 
00019 #include "platform/platform.h"
00020 #include "hal/ticker_api.h"
00021 #include "platform/NonCopyable.h"
00022 
00023 namespace mbed {
00024 /** \addtogroup drivers */
00025 
00026 /** A general purpose timer
00027  *
00028  * @note Synchronization level: Interrupt safe
00029  *
00030  * Example:
00031  * @code
00032  * // Count the time to toggle a LED
00033  *
00034  * #include "mbed.h"
00035  *
00036  * Timer timer;
00037  * DigitalOut led(LED1);
00038  * int begin, end;
00039  *
00040  * int main() {
00041  *     timer.start();
00042  *     begin = timer.read_us();
00043  *     led = !led;
00044  *     end = timer.read_us();
00045  *     printf("Toggle the led takes %d us", end - begin);
00046  * }
00047  * @endcode
00048  * @ingroup drivers
00049  */
00050 class Timer : private NonCopyable<Timer> {
00051 
00052 public:
00053     Timer();
00054     Timer(const ticker_data_t *data);
00055 
00056     /** Start the timer
00057      */
00058     void start();
00059 
00060     /** Stop the timer
00061      */
00062     void stop();
00063 
00064     /** Reset the timer to 0.
00065      *
00066      * If it was already counting, it will continue
00067      */
00068     void reset();
00069 
00070     /** Get the time passed in seconds
00071      *
00072      *  @returns    Time passed in seconds
00073      */
00074     float read();
00075 
00076     /** Get the time passed in milli-seconds
00077      *
00078      *  @returns    Time passed in milli seconds
00079      */
00080     int read_ms();
00081 
00082     /** Get the time passed in micro-seconds
00083      *
00084      *  @returns    Time passed in micro seconds
00085      */
00086     int read_us();
00087 
00088     /** An operator shorthand for read()
00089      */
00090     operator float();
00091 
00092     /** Get in a high resolution type the time passed in micro-seconds.
00093      */
00094     us_timestamp_t read_high_resolution_us();
00095 
00096 protected:
00097     us_timestamp_t slicetime();
00098     int _running;            // whether the timer is running
00099     us_timestamp_t _start;   // the start time of the latest slice
00100     us_timestamp_t _time;    // any accumulated time from previous slices
00101     const ticker_data_t *_ticker_data;
00102 };
00103 
00104 } // namespace mbed
00105 
00106 #endif