Using Libraries within libraries

Hi Everyone,

Can anyone explain if I can use Arduino libraries within my own personal library, without have to #include the Arduino library in the made code file?

In the example below, I would like to use SoftwareSerial library within my personal LCD library, however I cannot get this thing to work without calling #include <SoftwareSerial.h> within the main body of the code.

//LCD_20x4.h
#include "SoftwareSerial.h"

#ifndef LCD_20x4_h
#define LCD_20x4_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

void softSerialStart();
void clearLCD();



#endif

And here's the library .cpp file:

//LCD_20x4.cpp

#include "LCD_20x4.h"
#include "SoftwareSerial.h"
extern SoftwareSerial mySerial;

//-------------------------------------------------------------------------------------------
void softSerialStart()
{
	mySerial.begin(9600); // set up serial port for 9600 baud
}
//-------------------------------------------------------------------------------------------
void clearLCD()
{
  mySerial.write(0xFE);
  mySerial.write(0x01);
}
//-------------------------------------------------------------------------------------------

Then within the main file:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3,6);

unsigned long t1,t2;

void setup()
{
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  
  Serial.begin(9600);
  delay(500); // wait for display to boot up
  softSerialStart();
  
  t1 = millis();
  t2 = t1;
  
}

void loop()
{
  
  t1 = millis();
  
  if(((t1-t2) > 3000) or (t2 > t1))
  {
    clearLCD();
    t2 = t1;
  }
}

One more thing - Does any reason why I should call the #includes within a header vs. cpp file?

Thanks!!!

Can anyone explain if I can use Arduino libraries within my own personal library,

Yes, you can.

without have to #include the Arduino library in the made code file?

No, you can't. For good reasons. You should not try to hide the fact that the sketch needs library Xxx.

Does any reason why I should call the #includes within a header vs. cpp file?

A header file generally defines a class. You need to include that header file in the source file that implements the class. Other header files, like the one for SoftwareSerial, can be included in the source file for your class, or in the header file for your class. It's a personal preference. I prefer not to clutter up source files with a long list of #include statements.

Thanks Paul.

However - why is it that I can use functions like Serial.write() in my libraries by only including Arduino.h?

I have to assume that the core Arduino functions (digitalWrite, AnalogRead, Serial.begin, etc) are #included in main.cpp, so as to be the reason why we can use them in our libraries without explicit #includes.

Thank You!!

dimitri:
However - why is it that I can use functions like Serial.write() in my libraries by only including Arduino.h?

That's because it's an IDE and the IDE always includes that. Even if you don't use serial. (But the linker is smart enough not to pack it into the compiled code if you don't use it).

An addition, if you have a lirary.properties file you can use the "includes" key to let the IDE include (if you use Sketch->Include library->your library) a .h that's not even part of your library. That way you can make it easy for a user to include it and to show he needs it.

The only way around it is to put a copy of the library you want to include in the folder with the sketch. Then you include with quotes instead of angle brackets. You can include anything you want from inside the sketch folder anywhere you want. But you have to include a copy of the library. That makes sense. It isn't right to give a user a code and not tell them that they need to go download a library to make it work. But if you give them a copy of the library with the source then they don't need to know anything about it.

This does however clutter everything up because now you have multiple copies of all these libraries floating around. And if a library changes you'll have to go change your source to include the new version. So you'll have to continually be updating the source for your code. Generally, it is better to just include it in the code and let the user know that they need it. That saves a ton of headache later.