| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- #include "mbed.h"
- #include "rtos.h"
- /*
- * https://os.mbed.com/questions/77791/Correct-construction-for-callback-on-a-m/
- */
- class OtherClass
- {
- public:
- OtherClass(DigitalOut* out) :
- _out(out) {};
- ~OtherClass(void) {}
- DigitalOut* digitalOut() {
- return _out;
- }
- private:
- DigitalOut* _out;
- };
- class MyClass
- {
- public:
- MyClass(OtherClass* other) :
- _other(other) {}
- ~MyClass(void) {}
- void myCallback(void) {
- while (1) {
- *_other->digitalOut() = !*_other->digitalOut();
- wait(1);
- }
- }
- private:
- OtherClass* _other;
- };
- int main(void) {
- OtherClass otherClass(&led2);
- MyClass myClass(&otherClass);
- thread.start(callback(&myClass, &MyClass::myCallback)); // callback using member function
- while (true) {
- led1 = !led1;
- wait_ms(500);
- }
- }
|