私はArduino IDEと物事ネットワークarduinoライブラリを使ってLoRa moteを作成しています。クラス関数へのC++コールバック
すべてのLoRa関連の関数を処理するクラスを作成しました。このクラスでは、私がダウンリンクメッセージを受け取った場合、コールバックを処理する必要があります。 ttnライブラリには、init関数で設定するonMessage関数があり、メッセージと呼ばれるクラスメンバである別の関数を解析します。 「非静的メンバー関数の無効な使用」というエラーが表示されます。
// File: LoRa.cpp
#include "Arduino.h"
#include "LoRa.h"
#include <TheThingsNetwork.h>
TheThingsNetwork ttn(loraSerial,debugSerial,freqPlan);
LoRa::LoRa(){
}
void LoRa::init(){
// Set the callback
ttn.onMessage(this->message);
}
// Other functions
void LoRa::message(const uint8_t *payload, size_t size, port_t port)
{
// Stuff to do when reciving a downlink
}
とヘッダファイル
// File: LoRa.h
#ifndef LoRa_h
#define LoRa_h
#include "Arduino.h"
#include <TheThingsNetwork.h>
// Define serial interface for communication with LoRa module
#define loraSerial Serial1
#define debugSerial Serial
// define the frequency plan - EU or US. (TTN_FP_EU868 or TTN_FP_US915)
#define freqPlan TTN_FP_EU868
class LoRa{
// const vars
public:
LoRa();
void init();
// other functions
void message(const uint8_t *payload, size_t size, port_t port);
private:
// Private functions
};
#endif
私が試してみました:私は予想していたとして
ttn.onMessage(this->message);
ttn.onMessage(LoRa::message);
ttn.onMessage(message);
は、しかし、それらのどれも働きました。
非スタティックメンバ関数は、*オブジェクト*を呼び出す必要があります。オブジェクトがない場合は、非静的メンバー関数を使用することはできません。オブジェクトを取得したら、['std :: function'](http://en.cppreference.com/w/cpp/utility/functional/function)と[' std :: bind']( http://en.cppreference.com/w/cpp/utility/functional/bind)。 –