Mbed OS Reference
Loading...
Searching...
No Matches
Timer Class Reference

Timer implementation using the us ticker. More...

#include <Timer.h>

Inheritance diagram for Timer:
TimerBase

Public Member Functions

void start ()
 Start the timer.
 
void stop ()
 Stop the timer.
 
void reset ()
 Reset the timer to 0.
 
float read () const
 Get the time passed in seconds.
 
int read_ms () const
 Get the time passed in milliseconds.
 
int read_us () const
 Get the time passed in microseconds.
 
 operator float () const
 An operator shorthand for read()
 
us_timestamp_t read_high_resolution_us () const
 Get (in a high resolution type) the time passed in microseconds.
 
std::chrono::microseconds elapsed_time () const
 Get the time passed as chrono microseconds.
 

Detailed Description

Timer implementation using the us ticker.

Locks deep sleep while actively running.

Example:

// Count the time to toggle an LED
#include "mbed.h"
#include <cinttypes>
Timer timer;
DigitalOut led(LED1);
int main() {
timer.start();
const auto begin = timer.elapsed_time();
led = !led;
const auto end = timer.elapsed_time();
printf("Toggling the led takes %" PRIi64 " us", (end - begin).count());
}
A digital output, used for setting the state of a pin.
Definition DigitalOut.h:55
void start()
Start the timer.
std::chrono::microseconds elapsed_time() const
Get the time passed as chrono microseconds.
Timer implementation using the us ticker.
Definition Timer.h:167
Note
When constructed, a Timer is stopped and reset to zero time.
Synchronization level: Interrupt safe

Definition at line 167 of file Timer.h.

Member Function Documentation

◆ start()

void start ( )
inherited

Start the timer.

Will lock deep sleep if this is a Timer instance

◆ stop()

void stop ( )
inherited

Stop the timer.

Will release the deep sleep lock if this is a Timer instance

◆ reset()

void reset ( )
inherited

Reset the timer to 0.

If it was already running, it will continue

◆ read()

float read ( ) const
inherited

Get the time passed in seconds.

Returns
Time passed in floating-point seconds
Deprecated:
"Floating point operators should normally be avoided for code size. If really needed, you can use `duration<float>{elapsed_time()}.count()`" [since "mbed-os-6.0.0" ]

◆ read_ms()

int read_ms ( ) const
inherited

Get the time passed in milliseconds.

Returns
Time passed in integer milliseconds
Deprecated:
"Use the Chrono-based elapsed_time method. If integer milliseconds are needed, you can use `duration_cast<milliseconds>(elapsed_time()).count()`" [since "mbed-os-6.0.0" ]

◆ read_us()

int read_us ( ) const
inherited

Get the time passed in microseconds.

Returns
Time passed in integer microseconds
Warning
Limited to 31 bits – will not return correct values for times larger than about 35 minutes. Use read_high_resolution_us or elapsed_time instead as they are not vulnerable to this problem.
Deprecated:
"Use the Chrono-based elapsed_time method. If integer microseconds are needed, you can use `elapsed_time().count()`" [since "mbed-os-6.0.0" ]

◆ operator float()

operator float ( ) const
inherited

An operator shorthand for read()

Returns
Time passed in floating-point seconds
Deprecated:
"Floating point operators should normally be avoided for code size. If really needed, you can use `duration<float>{elapsed_time()}.count()`" [since "mbed-os-6.0.0" ]

◆ read_high_resolution_us()

us_timestamp_t read_high_resolution_us ( ) const
inherited

Get (in a high resolution type) the time passed in microseconds.

Returns
Time passed as 64-bit microseconds.
Deprecated:
"Use the Chrono-based elapsed_time method. If integer microseconds are needed, you can use `elapsed_time().count()`" [since "mbed-os-6.0.0" ]

◆ elapsed_time()

std::chrono::microseconds elapsed_time ( ) const
inherited

Get the time passed as chrono microseconds.

Returns
Time passed as std::chrono::microseconds