4 years, 10 months ago.

How do you attach a callback that's a member function to, say, Digitalin?

As was the case with my earlier question about starting a thread whose thread function is a member function, I see several "hints" but not a specific example.


1 Answer

4 years, 10 months ago.

Hello, Alfred

Because no attach method is available for DigitalIn I selected InterruptIn:

#include "mbed.h"

class MyClass {
    DigitalOut* _led;
    MyClass(DigitalOut* led) : _led(led) {}
    void memberFunction() { *_led = !*_led; }  // toggles led

DigitalOut  led1(LED1);
InterruptIn btn(p10);
MyClass     myClass(&led1);

int main()
    btn.fall(callback(&myClass, &MyClass::memberFunction)); // attaches myClass'es member function to btn (InterruptIn)

    while (true) {}

Accepted Answer

I'm not a fan of the callback templates. Perhaps all that's needed is a little more explanation and examples. I think I can do the attach-a-member function using the attach-a-nonmember function with the argument being the instance address, and then use the call-a-member-function operator. I haven't tried that yet but it's advantage to me is that I can understand what's going on.

posted by Alfred Hume 19 Jan 2019