Mistake on this page?
Report an issue in GitHub or email us

Timeout

Timeout class hierarchy

Use the Timeout interface to set up an interrupt to call a function after a specified delay.

You can create any number of Timeout objects, allowing multiple outstanding interrupts at the same time.

Warnings and notes

  • No blocking code in ISR: avoid any call to wait, infinite while loop or blocking calls in general.

  • No printf, malloc or new in ISR: Avoid any call to bulky library functions. In particular, certain library functions (such as printf, malloc and new) are not re-entrant, and their behavior could be corrupted when called from an ISR.

  • While a Timeout is running, deep sleep is blocked to maintain accurate timing. If you don't need microsecond precision, consider using the LowPowerTimeout class instead because this does not block deep sleep mode.

Timeout class reference

Public Types
using clock = HighResClock
 Clock to use with attach_absolute, guaranteeing running only while attached or manually locked. More...
Public Member Functions
HighResClock::time_point scheduled_time () const
template<class F >
void attach_absolute (F &&func, HighResClock::time_point abs_time)
 Attach a function to be called by the Timeout, specifying the absolute time. More...
std::chrono::microseconds remaining_time () const
 Return time remaining until callback. More...
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 (Callback< void()> func, std::chrono::microseconds t)
 Attach a function to be called by the Ticker, specifying the interval in microseconds. 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 detach ()
 Detach the function. More...

Timeout hello, world

Set up a Timeout to invert an LED after a given timeout:

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"

Timeout flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);

void flip()
{
    led2 = !led2;
}

int main()
{
    led2 = 1;
    flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds

    // spin in a main loop. flipper will interrupt it to call flip
    while (1) {
        led1 = !led1;
        ThisThread::sleep_for(200);
    }
}

Timeout example

Try this example to attach a member function:

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"

// A class for flip()-ing a DigitalOut
class Flipper {
public:
    Flipper(PinName pin) : _pin(pin)
    {
        _pin = 0;
    }
    void flip()
    {
        _pin = !_pin;
    }
private:
    DigitalOut _pin;
};

DigitalOut led1(LED1);
Flipper f(LED2);
Timeout t;

int main()
{
    // the address of the object, member function, and interval
    t.attach(callback(&f, &Flipper::flip), 2.0);

    // spin in a main loop. flipper will interrupt it to call flip
    while (1) {
        led1 = !led1;
        ThisThread::sleep_for(200);
    }
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.