The Mbed Timers Library allows you to measure the time between start and stop commands. The time can measured in milli or micro seconds.
- Downlaod from the Release site
- Unzip
- Move the folder to your mbed project folder
First, include the Timer library to your project:
#include "Timers.h"
Complete example. Here we created two timers, you can run it and get the result in the Serial monitor.
#include "mbed.h"
#include "Timers.h"
Timers timer1;
Timers timer2(MICROS);
int main() {
timer1.start();
timer2.start();
if(timer1.state() == RUNNING) printf("timer 1 running\n");
if(timer2.state() == RUNNING) printf("timer 2 running\n");
ThisThread::sleep_for(1s);
timer1.pause();
timer2.pause();
if(timer1.state() == PAUSED) printf("timer 1 pause\n");
if(timer2.state() == PAUSED) printf("timer 2 pause\n");
printf("timer 1 elapsed ms: %u\n", timer1.elapsed());
printf("timer 2 elapsed us: %u\n", timer2.elapsed());
ThisThread::sleep_for(1s);
timer1.resume();
timer2.resume();
ThisThread::sleep_for(1s);
timer1.stop();
timer2.stop();
if(timer1.state() == STOPPED) printf("timer 1 stopped\n");
if(timer2.state() == STOPPED) printf("timer 2 stopped\n");
printf("timer 1 elapsed ms: %u\n", timer1.elapsed());
printf("timer 2 elapsed us: %u\n", timer2.elapsed());
while (true) {
}
}enum resolution_t {
MICROS,
MILLIS
};You can use milli or micro seconds for the resolution, internal the library use the us_ticker_read() function. Standard is MILLIS;
enum status_t {
STOPPED,
RUNNING,
PAUSED};The library uses differnet states.
Timers::Timers(resolution_t resolution)Creates a Timer object
- resolution sets the internal resolution of the Timers, it can MICROS, or MILLIS, standard is MILLIS.
Examples
Timers timer1;
Timers timer2(MICROS);Timers::~Timers()Destructor for Timers objects.
Example
Timers ~timer1;void Timers::start() Start the Timers. If it is paused, it will restarted the Timers.
Example
timer1.start();void Timers::pause() Pause the Timers.
Example
timer1.pause();void Timers::resume() Resume the Timers after a pause.
Example
timer1.resume();void Timers::stop() Stops the Timers.
Example
timer1.stop();uint32_t Timers::elapsed() Returns the time after start, you can read the elapsed time also while running.
Example
uint32_t elapse;
elapse = timer1.elapsed();status_t Timers::state()Get the Timers state (RUNNING, PAUSED, STOPPED).
Example
status_t status;
status = timer1.state();