Timeout example: attaching a member function

Dependencies:   mbed

mbed 2 and mbed OS 5

This is an mbed 2 example. For an mbed-os example, please see:

Import programTimeout_Example

Simple Timeout example with class and callback to member function

main.cpp

Committer:
mbed_official
Date:
2013-02-19
Revision:
0:f24282a2495f

File content as of revision 0:f24282a2495f:

#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);
    }
}