ArduinoのattachInterruptにメンバー関数を割り当てる方法について質問です。
このページの回答を参考に
test_class.ino
#include "test.h" Foo foo; typedef void (*fnInterrupt)(); fnInterrupt refInterrupt; void setup(){ Serial.begin(115200); attachInterrupt(0,callMethod,RISING); } void loop(){ } //--------Foo Class-------------- void Foo::bar(){ static uint8_t a = 0; a++; } void Foo::setMethod(){ setReference(this->*bar); } //------------------------------ void setReference(void (*method)()){ refInterrupt = method; } void callMethod(){ refInterrupt(); }
test.h
#ifndef __TEST_H_ #define __TEST_H_ class Foo{ public: void bar(); void setMethod(); }; #endif
というスケッチとヘッダーを作ってみました。
リンク先の回答と違い、インスタンス名を直接指定してメンバ関数の参照を渡すのではなく、インスタンス内からインスタンス自身.bar()の参照を渡して、そのメンバ関数を呼び出す関数を割り込み関数に当てたいと考えています。
/Users/username/Documents/Arduino/test_class/test_class.ino: In member function 'void Foo::setMethod()': test_class:22: error: '((Foo*)this)->Foo::bar' cannot be used as a member pointer, since it is of type '<unresolved overloaded function type>' setReference(this->*bar); ^ test_class:22: error: 'setReference' was not declared in this scope setReference(this->*bar); ^ /Users/username/Documents/Arduino/test_class/test_class.ino: At global scope: test_class:27: error: variable or field 'setReference' declared void void setReference((*method)){ ^ test_class:27: error: 'method' was not declared in this scope void setReference((*method)){ ^ exit status 1 '((Foo*)this)->Foo::bar' cannot be used as a member pointer, since it is of type '<unresolved overloaded function type>'
しかしというエラーが発生してしまいます。
まだArduinoもCも入門レベルなので質問に問題がありましたら申し訳ありません。
この問題についてわかる方がいらっしゃいましたら、ご回答いただけると嬉しいです。
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/01 09:30