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.
A simple program to setup a Timeout to invert an LED after a given timeout...
#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;
wait(0.2);
}
}
API summary
| Timeout | A Timeout is used to call a function at a point in the future |
| Functions | |
| attach | Attach a function to be called by the Timeout, specifiying the delay in seconds |
| attach | Attach a member function to be called by the Timeout, specifiying the delay in seconds |
| attach_us | Attach a function to be called by the Timeout, specifiying the delay in micro-seconds |
| attach_us | Attach a member function to be called by the Timeout, specifiying the delay in micro-seconds |
| detach | Detach the function |
A Timeout is used to call a function at a point in the future
class Timeout : public Ticker
Attach a function to be called by the Timeout, specifiying the delay in seconds
void attach( void (*fptr)(void), float t )
Attach a function to be called by the Timeout, specifiying the delay in micro-seconds
void attach_us( void (*fptr)(void), unsigned int t )
Detach the function
void detach()
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.
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);
}
}
No tags
|
10 comments
Please login to post comments.
That Demo code is not working on my mbed board. The Led 2 is not flashing every 2 seconds as it should do.