簡體   English   中英

編譯錯誤Arduino C ++對象庫

[英]Compilation error Arduino C++ object library

我正在編寫用於藍牙通信的C ++ Arduino庫。 我實現了一種機制,以注冊當某個字節來自BT連接時要執行的自定義功能。 通常是一種Java Hashmap。 一切正常,但是當我嘗試在構造函數中注冊庫對象的方法時,出現了一些無法理解的編譯錯誤。 我正在將Arduino IDE 1.6.9與Ubuntu 16.04 LTS 64bit一起使用。 該板是Arduino UNO。 在下面的代碼標題下:

#ifndef BlueHartwin_H
#define BlueHartwin_H

    #include <SoftwareSerial.h>
    #include <Streaming.h>;
    #include <Arduino.h>

    #define START_TX 254
    #define STOP_TX 255

    typedef void (* CmdFuncPtr) (); // this is a typedef to command functions


    class BlueHartwin {

    public:
        BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate);
        ~BlueHartwin();
        boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
        boolean unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr);
        boolean runCmd(byte id);
        void setDataLength(byte dataLength);
        byte getDataLength();
        byte getIncomingByte();
        void txSwitchOff(void);
        void txSwitchOn(void);
        bool isTx();
        boolean available();
        boolean read();
        void write(byte data,boolean endLine);


    private:

        CmdFuncPtr setOfCmds[255];
        byte mDataLength;
        bool tx;
        byte incomingByte;
    };

    #endif

身體:

#include "BlueHartwin.h";

/*
 * Reserved command IDs:
 * 14,
 * 15
*/


SoftwareSerial bluetooth(7,8);


BlueHartwin::BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate){
    bluetooth = SoftwareSerial(bluetoothTx,bluetoothRx);
    bluetooth.begin(baudRate);
    for (int i=0;i<=255;i++) {
        setOfCmds[i]=NULL;
    }
    tx=false;

    registerCmd(14,&txSwitchOff);
    registerCmd(15,&txSwitchOn);
}

BlueHartwin::~BlueHartwin(){

}

boolean BlueHartwin::registerCmd(byte id,CmdFuncPtr cmdFuncPtr){
    if (setOfCmds[id]==NULL) {
        setOfCmds[id]=cmdFuncPtr;   
        return true;
    } else return false;
}

boolean BlueHartwin::unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr){
    setOfCmds[id]=NULL; 
    return true;

}


boolean BlueHartwin::runCmd(byte id){
    if (setOfCmds[id]!=NULL) {
        setOfCmds[id]();
        return true;
    } else return false;
}


void BlueHartwin::setDataLength(byte dataLength) {
    mDataLength=dataLength;
}

byte BlueHartwin::getDataLength(){
    return mDataLength;
}

byte BlueHartwin::getIncomingByte(){
    return incomingByte;
}

boolean BlueHartwin::isTx(){
    return tx;
}

void BlueHartwin::txSwitchOff(void){
  tx=false;
  Serial<<"now tx is "<<tx<<endl;
}

void BlueHartwin::txSwitchOn(void){
  tx=true;
  Serial<<"now tx is "<<tx<<endl;
}

boolean BlueHartwin::available(){
    return bluetooth.available();
}

boolean BlueHartwin::read(){

    if (bluetooth.available()>0)
        {
         incomingByte=bluetooth.read();
         Serial << "Incoming byte " << incomingByte<<endl;
         return runCmd(incomingByte);
        }
    return false;
}


void BlueHartwin::write(byte data,boolean endLine){
    if (tx) {
        if (endLine) bluetooth << data << endl;
        else bluetooth<<data;
    }
}

錯誤:

/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp: In constructor 'BlueHartwin::BlueHartwin(byte, byte, long int)':
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:18: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&BlueHartwin::txSwitchOff' [-fpermissive]
  registerCmd(14,&txSwitchOff);
                  ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:29: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
  registerCmd(14,&txSwitchOff);
                             ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:29: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
  boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
          ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note:   no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:18: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&BlueHartwin::txSwitchOn' [-fpermissive]
  registerCmd(15,&txSwitchOn);
                  ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:28: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
  registerCmd(15,&txSwitchOn);
                            ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:28: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
  boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
          ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note:   no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
exit status 1
Errore durante la compilazione per la scheda Arduino/Genuino Uno.

如果我在構造函數中更改行:

registerCmd(14,&txSwitchOff);
registerCmd(15,&txSwitchOn);

registerCmd(14,&BlueHartwin::txSwitchOff);
registerCmd(15,&BlueHartwin::txSwitchOn);

我收到此錯誤:

/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp: In constructor 'BlueHartwin::BlueHartwin(byte, byte, long int)':
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:42: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
  registerCmd(14,&BlueHartwin::txSwitchOff);
                                          ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:42: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
  boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
          ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note:   no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:41: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
  registerCmd(15,&BlueHartwin::txSwitchOn);
                                         ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:41: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
  boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
          ^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note:   no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
exit status 1
Errore durante la compilazione per la scheda Arduino/Genuino Uno.

在此先感謝您的幫助!

配合,您的txSwitchOn / Off方法是實例方法-它們需要使用this才能正常工作。

另一方面,您的CmdFuncPtr是不依賴任何內容的函數的定義。


假定Arduino一次可以處理多少個此類設備(由您的庫支持)?

如果是一個,則將所有內容聲明為靜態,將構造函數重命名為“ static void init(),將析構函數重命名為” static void Disconnect()”,依此類推。您將使該類僅充當函數的命名上下文(全部靜態)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM