Go Down

Topic: Help needed: How to make a library from a .ino-file? (Read 1 time) previous topic - next topic

PaulS

Quote
I posted the code above.

You posted some of the source code (the .cpp file), but nothing of the header file (where the class, if there is one, is defined).

SierraWhiskey

Ah, ok...

Code: [Select]

class EDIPTFT {
  public:
    EDIPTFT(int port);
    void sendData(char* data, int len);
    void clear();
    void invert();
    void setDisplayColor(char fg, char bg);
    void fillDisplayColor(char bg);
    void terminalOn(boolean on);
    void cursor(boolean on);
    void setCursor(char col, char row);
    void defineBargraph(char dir, char no, int x1, int y1, int x2, int y2, byte sv, byte ev, char type);
    void updateBargraph(char no, char val);
    void setBargraphColor(char no, char fg, char bg, char fr);
    void makeBargraphTouch(char no);
    void linkBargraphLight(char no);
    void deleteBargraph(char no,char n1);
    void setLine(char fg, char bg);
    void setTextColor(char fg, char bg);
    void setTextFont(char font);
    void setTextAngle(char angle);
    void drawText(int x1, int y1, char justification,String text);
    void drawLine(int x1, int y1, int x2, int y2);
    void drawRect(int x1, int y1, int x2, int y2);
    void Rectf(int x1, int y1, int x2, int y2, char color);
    void defineTouchKey(int x1, int y1, int x2, int y2, char down, char up, String text);
    void setTouchkeyColors(char n1, char n2, char n3, char s1, char s2, char s3);
    void setTouchkeyFont(char font);
    void setTouchkeyLabelColors(char nf,char sf);
    void removeTouchArea(char code,char n1);
  private:
    int _port;
    boolean _smallprotocol;

PaulS

Some of the methods in the class should probably be private, instead of all of them being public.

sendData() is a method the belongs to a specific instance of a class. As opposed to a static method that is shared by all instances of the class.

Therefore, to call it from Clear(), just use the method name. The ClassName:: bit up front, called a scope resolution operator, is not needed.
Code: [Select]
void EDIPTFT::Clear()
{
  char command [] = { 12 };
  sendData(command, 1);
}


The space key is not important to the compiler, usually, but it does make for more readable code.

There are some other things you could do. command does not need to be an array to hold one value. You could use:
Code: [Select]
char cmd = 12;
sendData(&cmd, 1);

But, if you wish to keep it an array, for consistency with other functions where the array contains more than one element, you should use the sizeof() macro, and let the compiler do the counting:
Code: [Select]
sendData(command, sizeof(command));
That way, if you copy/paste the function and change the size of the array, you don't need to count the number of elements in the new array.

SierraWhiskey

Thanks a lot!

I was avoiding sizeOf() yet because I'm not sure how it reacts to null elements, i.e. if it sees null elements as terminators and thus may deliver wrong results.

Which functions do you think should be private? The only function I could think of would be sendData() but I kept it yet public intentionally because I haven't implemented functions for all commands yet. So, this way, the user can still use sendData() to send commands "by hand". This will become especially important when I succeeded implementing EA's proprietary smallprotocol which includes some checksum calculations and such.

SierraWhiskey

Now I ran into a problem which doesn't mean anything to me...

I want to pass the serial port to use for comms to the library in a parameter. In sendData(), I then check this parameter and decide, which Serial to use:
Code: [Select]
EDIPTFT::EDIPTFT(int port, int smallprotocol) {
  _port = port;
  _smallprotocol = smallprotocol;
}

void EDIPTFT::sendData(char* data, int len) {
  if (_smallprotocol > 0) {
  }
  else {
    unsigned char i;
    for(i=0; i < len; i++) {
      switch (_port) {
case 0 : {
  Serial.write(data[i]);
  break;
}
case 1 : {
  Serial1.write(data[i]);
  break;
}
case 2 : {
  Serial2.write(data[i]);
  break;
}
case 3 : {
  Serial3.write(data[i]);
  break;
}
      }
    }
  }
}


I use this library from a sketch that also uses the FastSerial library. Now, when I try to compile everything, I get this errors:
Code: [Select]
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `__vector_25'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
/opt/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../avr/bin/ld: Disabling relaxation: it will not work with multiple definitions
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `__vector_36'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `__vector_51'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `__vector_54'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `Serial'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
/opt/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../avr/bin/ld: Warning: size of symbol `Serial' changed from 29 in UxV_CS.cpp.o to 34 in core.a(HardwareSerial.cpp.o)
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `Serial1'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
/opt/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../avr/bin/ld: Warning: size of symbol `Serial1' changed from 29 in UxV_CS.cpp.o to 34 in core.a(HardwareSerial.cpp.o)
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `Serial2'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
/opt/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../avr/bin/ld: Warning: size of symbol `Serial2' changed from 29 in UxV_CS.cpp.o to 34 in core.a(HardwareSerial.cpp.o)
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `Serial3'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
/opt/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../avr/bin/ld: Warning: size of symbol `Serial3' changed from 29 in UxV_CS.cpp.o to 34 in core.a(HardwareSerial.cpp.o)
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `__vector_26'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `__vector_37'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `__vector_52'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here
core.a(HardwareSerial.cpp.o): In function `global constructors keyed to rx_buffer':
/opt/arduino-1.0.3/hardware/arduino/cores/arduino/HardwareSerial.cpp:515: multiple definition of `__vector_55'
UxV_CS.cpp.o:/opt/arduino-1.0.3/UxV_CS.ino:79: first defined here


This is way out of my league... What do I do now?

Go Up