Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 9 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.
Thanks!
1 Answer
6 years, 9 months ago.
Hello, Alfred
Because no attach method is available for DigitalIn I selected InterruptIn:
#include "mbed.h"
class MyClass {
DigitalOut* _led;
public:
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) {}
}
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 19 Jan 2019