martin.p
martin.p

Reputation: 363

Compiling error in Arduino library

I'm having a strange error compiling a sketch in Arduino, probably due to a mistake in a library I've created. here's the code:

    #include <BlueHartwin.h>

BlueHartwin bH(7,8,115200);
byte incomingByte; 

void noOp(void)
{
  Serial<<"noOp"<<endl;
};

void leftWindowDown(void)
{
  Serial<<"leftWindowDown"<<endl;
};

void setup() {
  Serial.begin(115200);
  bH.registerCmd(0,&noOp);
  bH.registerCmd(1,&leftWindowDown);

}

void loop() {

  if(bH.available())
  {
    incomingByte = bH.read()-48;
    Serial<<incomingByte<<endl;
    if (incomingByte<nrOfCommands)
      bH.runCmd(incomingByte);

  }
}

Here are the header of the library:

#ifndef BlueHartwin_H
    #define BlueHartwin_H

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

#define nrOfCommands 255

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


class BlueHartwin {

public:
    BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate);
    ~BlueHartwin();
    //void setup(byte bluetoothTx,byte bluetoothRx,long baudRate);
    boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
    boolean unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr);
    boolean runCmd(byte id);
    void setDataLength(byte dataLength);
    byte getDataLength();
    boolean available();
    byte read();

private:
    CmdFuncPtr setOfCmds[255];
    byte mDataLength;

};

#endif

and source

#include "BlueHartwin.h";


SoftwareSerial bluetooth(7,8);
byte mDataLength;

BlueHartwin::BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate){
    bluetooth = SoftwareSerial(bluetoothTx,bluetoothRx);
    bluetooth.begin(baudRate);
}

BlueHartwin::~BlueHartwin(){

}

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

boolean BlueHartwin::unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr){
    if (id<=nrOfCommands) {
        setOfCmds[id]=0;    
        return true;
    } else return false;
}


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


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

byte getDataLength(){
    return mDataLength;
}

boolean available(){
    return true;
}

byte read(){
    return 0;
}

This is the error I get:

sketch/Test_Bluehartwin00.ino.cpp.o: In function `loop':
/home/paolo/Projects/arduino/sketches/TESTS/Test_Bluehartwin00/Test_Bluehartwin00.ino:25: undefined reference to `BlueHartwin::available()'
/home/paolo/Projects/arduino/sketches/TESTS/Test_Bluehartwin00/Test_Bluehartwin00.ino:27: undefined reference to `BlueHartwin::read()'
collect2: error: ld returned 1 exit status
exit status 1

The strange thing is that I get error only in 2 of the public function of the library class. I really can't understand what's wrong.... I'm using Arduino UNO and IDE 1.6.8 under Ubuntu 14.04 64 bit. Normally everything runs fine. Thanks for help in advance.

Upvotes: 0

Views: 241

Answers (1)

Chris A
Chris A

Reputation: 1485

The problem is in the .cpp file.

The two functions giving you a problem are here:

boolean available(){
    return true;
}

byte read(){
    return 0;
}

As these are, they are free/global functions. You need to make them part of the class:

boolean BlueHartwin::available(){
    return true;
}

byte BlueHartwin::read(){
    return 0;
}

Note: you'll also have to modify a few others also:

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

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

These can be fixed the same way.

Upvotes: 3

Related Questions