You are viewing an older revision! See the latest version
Timeout
The Timeout interface is used to setup an interrupt to call a function after a specified delay.
Any number of Timeout objects can be created, allowing multiple outstanding interrupts at the same time.
Hello World!¶
A simple program to setup a Timeout to invert an LED after a given timeout...
Repository: Timeout_HelloWorld
API¶
API summary
Import librarymbed
Warning
Note that timers are based on 32-bit int microsecond counters, so can only time up to a maximum of 2^31-1 microseconds i.e. 30 minutes. They are designed for times between microseconds and seconds. For longer times, you should consider the time()/Real time clock.
No blocking code in ISR
In ISR you should avoid any call to wait, infinitive while loop, or blocking calls in general.
No printf, malloc, or new in ISR
In ISR you should avoid any call to bulky library functions. In particular, certain library functions (like printf, malloc and new) are non re-entrant and their behaviour could be corrupted when called from an ISR.
RTOS Timer
Consider using the mbed RTOS Timer instead of a Timeout. In this way your callback function will not be executed in a ISR, giving you more freedom and safety in your code.
Examples¶
Attaching a member function
#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() { t.attach(&f, &Flipper::flip, 2.0); // the address of the object, member function, and interval // spin in a main loop. flipper will interrupt it to call flip while(1) { led1 = !led1; wait(0.2); } }