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

A Ticker is used to call a function at a recurring interval. More...

#include <Ticker.h>

Inheritance diagram for TickerBase:
TimerEvent NonCopyable< TickerBase > NonCopyable< TimerEvent > LowPowerTicker Ticker TimeoutBase LowPowerTimeout Timeout

Public Member Functions

template<typename F >
MBED_FORCEINLINE void attach (F &&func, float t)
 Attach a function to be called by the Ticker, specifying the interval in seconds. More...
 
void attach_us (Callback< void()> func, us_timestamp_t t)
 Attach a function to be called by the Ticker, specifying the interval in microseconds. More...
 
void attach (Callback< void()> func, std::chrono::microseconds t)
 Attach a function to be called by the Ticker, specifying the interval in microseconds. More...
 
void detach ()
 Detach the function. More...
 

Detailed Description

A Ticker is used to call a function at a recurring interval.

You can use as many separate Ticker objects as you require.

Note
Synchronization level: Interrupt safe

Example:

// Toggle the blinking LED after 5 seconds
#include "mbed.h"
using namespace std::chrono;
Ticker timer;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
int flip = 0;
void attime() {
flip = !flip;
}
int main() {
timer.attach(&attime, 5us);
while(1) {
if(flip == 0) {
led1 = !led1;
} else {
led2 = !led2;
}
ThisThread::sleep_for(200ms);
}
}
A digital output, used for setting the state of a pin.
Definition: DigitalOut.h:55
MBED_FORCEINLINE void attach(F &&func, float t)
Attach a function to be called by the Ticker, specifying the interval in seconds.
Definition: Ticker.h:92

Definition at line 73 of file Ticker.h.

Member Function Documentation

◆ attach() [1/2]

MBED_FORCEINLINE void attach ( F &&  func,
float  t 
)

Attach a function to be called by the Ticker, specifying the interval in seconds.

The method forwards its arguments to attach_us() rather than copying them which may not be trivial depending on the callback copied. The function is forcibly inlined to not use floating-point operations. This is possible given attach_us() expects an integer value for the callback interval.

Parameters
funcpointer to the function to be called
tthe time between calls in seconds
Deprecated:
Pass a chrono duration, not a float second count. For example use 10ms rather than 0.01f.

Definition at line 92 of file Ticker.h.

◆ attach_us()

void attach_us ( Callback< void()>  func,
us_timestamp_t  t 
)

Attach a function to be called by the Ticker, specifying the interval in microseconds.

Parameters
funcpointer to the function to be called
tthe time between calls in micro-seconds
Note
setting t to a value shorter than it takes to process the ticker callback causes the system to hang. Ticker callback is called constantly with no time for threads scheduling.
Deprecated:
Pass a chrono duration, not an integer microsecond count. For example use 10ms rather than 10000.

◆ attach() [2/2]

void attach ( Callback< void()>  func,
std::chrono::microseconds  t 
)

Attach a function to be called by the Ticker, specifying the interval in microseconds.

Parameters
funcpointer to the function to be called
tthe time between calls in micro-seconds
Note
setting t to a value shorter than it takes to process the ticker callback causes the system to hang. Ticker callback is called constantly with no time for threads scheduling.

◆ detach()

void detach ( )

Detach the function.