How to create a library that requires another libray

How can I create a library that depends on another library, the software serial library in particular?

I am working with a GSM shield. I have the main sketch with lots of functions. I would like to place all of these functions into a separate source file. I suspect that creating a library is the proper way to do this. But, the library depends on the SoftwareSerial library and quite a few functions call the read, write, print and println functions of the SoftwareSerial instance used.

I did see the example on the site here, with the Morse library. One other thing I still need to figure out is which variables should be part of the library and which should be part of the sketch using the library...

here is a link

http://arduino.cc/it/Hacking/LibraryTutorial
where it says to use

#include "WProgram.h"

use instead

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

if your library A includes library B for functionality.

in your .pde/.ino you need to #include both library A and B

//Move your code into a class

class MyLib{
  public:
    //Client stuff here

    MyLib( int some_init_value_if_needed ) //Class constructor
     {
      }

    void DoStuff( void )  //Functionality
      {
      }

  protected:
    //Derived type stuff here
  private:
    //Local private data/functions here 
    int i_Mydata;
};

If you need to keep state info you can use variables in the class like int i_Mydata;

If you only have a set of discrete functions, with no state info required to be kept. use a class with non-member functions

class MySystem{
  public:
    static void DoSomething( void )
      {
      };

    static void DoSomethingElse( void )
      {
      };
};

//Then all you do to use the functions is
MySystem::DoSomething();

MySystem::DoSomethingElse();

This is good for keeping related functionality together

Have a look in the library’s you already have, they use .h and .cpp files to create a library.

The easiest way is to create a .h in your sketch directory and plonk all functions straight in there, make them inline to prevent multiple definition errors. Or have the function prototypes in the header and in a .cpp that includes the .h, add the actual function code.
This isn’t really a library though, it just moves functionality into a different location. Classes are the better approach.

if your library A includes library B for functionality.

in your .pde/.ino you need to #include both library A and B

IIRC you have to put #include B also in library A.

pYro_65: The easiest way is to create a .h in your sketch directory and plonk all functions straight in there, make them inline to prevent multiple definition errors. Or have the function prototypes in the header and in a .cpp that includes the .h, add the actual function code. This isn't really a library though, it just moves functionality into a different location. Classes are the better approach.

I have looked at that possibility. It can work with that and I am quite familiar with. Have done a fair amount of C programming. Just never done anything object oriented or with classes. Tried writing a C class many years ago and it was a very painful experience.

What is the scope of variables if I create a class? Can I pass a pointer to a string array then the class instance is created and have it become global within the scope of the class for the live of the instance of the class?

In particular, I need to work with the NewSoftwareSerial library. I have come to the conclusion that I'd me much farther ahead and saving code space if I used a modified NewSoftwareSerial rather then wrap the read() and available() functions into my own function. I need the serial port reading to be non-blocking. The default 64 byte buffer of NewSoftwareSerial is insufficient for my purposes. Also, I need a timer that tells me how many milliseconds have passed since the last character has been received. Both of these seem to be trivial to accomplish if I just make some changes to the NewSoftwareSerial library.

But, I would like to pass a pointer to a char[] to the NewSoftwareSerial rather then have the definition of the receive buffer within the library itself. Can that be done?

Tried writing a C class many years ago and it was a very painful experience.

That's because you should have been trying to write it in C++ not C. ]:D

retro you're evil ;-P

mromani: retro you're evil ;-P

Lol... my fault for a typo, that was supposed to have been a php class. Are these classes and libraries a Wiring or Arduino specific thing, or is that what C++ is all about?

The second one.

http://arduino.cc/en/Hacking/LibraryTutorial

(note: since Arduino 1.0 you have to use #include <Arduino.h> instead of Wprogram.h).

class Morse
{
  public:
    Morse(int pin);
    void dot();
    void dash();
  private:
    int _pin;
};

This means the variable _pin is only accessible within the class (cannot be accessed outside of the class) but is available to any function within the class.

class Morse
{
  public:
    Morse(int pin);
    void dot();
    void dash();
    int fish;
  private:
    int _pin;
};

I added "fish" to the "public:" thing. Does this mean "fish" is now like "_pin" in that it is accessible by all functions of the class, but is also accessible outside of the class, by whatever created an instance of the class?

And both "_pin" and "fish" only exist while an instance of the class exists. Can I destroy an instance of a class within Arduino?

adilinden: ...

that's all true. To destroy an instance of a class, you can just let it go out of scope, or use delete if you allocated it with new.

To access fish in the second example, you would do something like:

class Morse
{
  public:
    Morse(int pin);
    void dot();
    void dash();
    int fish;
  private:
    int _pin;
};

Morse morse(3);
morse.fish = 5;
int y = morse.fish
morse.dot();
morse.dash();

However, you almost never want to actually have a variable with public scope; people generally make get and set functions.

class Morse
{
  public:
    Morse(int pin);
    void dot();
    void dash();
    int getFish() { return fish; }
    void setFish(int val) { fish = val; }
  private:
    int fish;
    int _pin;
};

Back to my original question. I created the Morse example referenced for Arduino 1.0. It compiles fine with the "Arduino.h" change. Then I added a couple of statements to also output using the SoftwareSerial library. Now I am getting issues. This is what I did for "Morse.cpp":

/*
  Morse.cpp - Library for flashing Morse code.
  Created by David A. Mellis, November 2, 2007.
  Released into the public domain.
*/

#include "Arduino.h"
#include "Morse.h"


Morse::Morse(int pin)
{
  pinMode(pin, OUTPUT);
  _pin = pin;
  morseSerial.begin(9600);
}

void Morse::dot()
{
  digitalWrite(_pin, HIGH);
  delay(250);
  digitalWrite(_pin, LOW);
  delay(250);  
  morseSerial.print('.');
}

void Morse::dash()
{
  digitalWrite(_pin, HIGH);
  delay(1000);
  digitalWrite(_pin, LOW);
  delay(250);
  morseSerial.print('-');
}

Now I need to place

SoftwareSerial morseSerial(2, 3);

somewhere. If i place this into my "testing.ino" file I get this error message:

Morse.cpp: In constructor 'Morse::Morse(int)':
Morse.cpp:14: error: 'morseSerial' was not declared in this scope

When I place it in "Morse.cpp" or "Morse.h" I get this error:

Morse.cpp:9: error: 'SoftwareSerial' does not name a type
Morse.cpp: In constructor 'Morse::Morse(int)':
Morse.cpp:15: error: 'morseSerial' was not declared in this scope

So how do I use SoftwareSerial within another class?

Ok, I had a “duh…” moment. I forgot

#include <SoftwareSerial.h>

The only combination of things that allows me to compile this is by having the include statement in my “testing.ino” file as well as in “Morse.h”. The “SoftwareSerial morseSerial(2, 3);” statement needs to be in “Morse.cpp”. This is the only way I can get this to build. Placing “SoftwareSerial morseSerial(2, 3);” into the main sketch trows errors, placing the include statements anywhere else, and not in the main sketch and the header file throws errors.

Yeah why does it need to be included twice? Any other way and its either “library " not defined or " library” not found
I wasted about 25 minutes just to figure that out, im making a library for the mcp79410 rtc and that was my only hurdle so far, quite frustrating
is this a arduino 1.0 thing?

is this a arduino 1.0 thing?

No, AFAIKT it's always been this way. Don't know why.

When I look at existing libraries and the morse example, variables inside functions are defined using a leading underscore. What is the reason? Since variables defined in functions are always local scoped, there should be no special distinction from "other" variables needed?

adilinden: When I look at existing libraries and the morse example, variables inside functions are defined using a leading underscore. What is the reason? Since variables defined in functions are always local scoped, there should be no special distinction from "other" variables needed?

The compiler doesn't care if a variable starts with an underscore or not. Starting class private variable names with an underscore makes it easier to remember thery're private when reading the code without keeping the class definition in front of you. It's a matter of taste.