Creating a Library - Few Questions

Hello

I have written some code for myself for producing large fonts on a specific 24x8 LCD I have, which require specific commands to store the info on the LCD's RAM, and display stuff.

The long and short of it is, I ideally would like to store all of this code in a header file or something, rather than having it visually taking up room in the main program.

I am still learning the ways of the Arduino and the IDE, and I am just not sure if I am able to simply create a .h file, and paste in the functions I dont want in my main code, and then include the .h file.

I had a quick look at the Morse Tutorial, and it seems there are .h files and .cpp files for the libraries. On inspection of thes files, the syntax is different from the normal program code - of which I am not yet familar with writing.

My question is, can I simply paste in the functions I dont want to have in my main code, into a .h file, include it and all will be OK - or do I have to do some work and produce .h and .cpp files in a specific format?

I am just a little unsure of what I need to do.

Thanks

You can do it that way.

However, you should try and take advantage of the object orientated programming that the Arduino offers.

If parts of your code can be described as an object (eg menu handling), you can make an object called menu_handle.

It is really worth wild pursuing this further!

Ok cool thanks - that sounds like a plan.

I will investigate this over the next few days and try and piece something together :)

Good to know you can simply dump functions into a .h file still though, but as you say, worth investigating how to do it right too.

Thanks

The way to do it is to put all your function definitions in the .h file

void function1(void);
int funtion2(void);

… ect

and put the function in a .c file
function1() {

/* Do something */

}

I think the C code files have to use a .cpp extension for libraries. Pretty sure that's where I went wrong initially.

Good libraries tutorial - http://arduino.cc/en/Hacking/LibraryTutorial

Edit: Although that's probably the same one as the OP said he'd read. Doh! :D

I'll be honest, I have only made .cpp libraries whilst using the arduino, and .c libraries using an AVR.

You can use .c libraries on the Arduino though, as the SD card library is in .c format. But as for creating them, I don't know...

Hey again

I have done some learning and managed to create my library using the .h and .cpp files - awesome.

However I have a question about the constructor and calling the instance in the IDE.

Basically my library I have called ‘LCDFonts’. I am not wanting to pass any values to the library when making the instance, as all the variables are passed in with the functions as required. My issue however is that it wouldnt compile if I did the following…

The LCDFonts.h file I tried to make work:

#ifndef LCDFonts_h
#define LCDFonts_h

#include "WProgram.h"

class LCDFonts
{
  public:
    [glow]LCDFonts();[/glow]
    void CursorPosition(int Column, int Row);
    void SetFont(int Font);
    void Font9();
    void Font10();
    void Font11();
    void print2x3Number(int number, int column, int row);
    void print3x3Number(int number, int column, int row);
    void print4x4Number(int number, int column, int row);
  private:
    void _printOneSmlNumber(int digit, byte Column, byte Row);
    void _printOneMedNumber(int digit, byte Column, byte Row);
    void _printOneBigNumber(int digit, byte Column, byte Row);
};

#endif

My LCDFonts.cpp I tried to make work (only relevant bit pasted):

#include "WProgram.h"
#include "LCDFonts.h"

LCDFonts::LCDFonts()
{
}

My IDE to call the instance that I tried to make work:

#include <LCDFonts.h>

LCDFonts lcdfonts();

It didnt like it though. I dont want to have to pass a value in but it looks like I had to. I did the following, even though I didnt use the value I passed, and it worked…

The LCDFonts.h file that worked:

#ifndef LCDFonts_h
#define LCDFonts_h

#include "WProgram.h"

class LCDFonts
{
  public:
    [glow]LCDFonts(int pin);[/glow]
    void CursorPosition(int Column, int Row);
    void SetFont(int Font);
    void Font9();
    void Font10();
    void Font11();
    void print2x3Number(int number, int column, int row);
    void print3x3Number(int number, int column, int row);
    void print4x4Number(int number, int column, int row);
  private:
    void _printOneSmlNumber(int digit, byte Column, byte Row);
    void _printOneMedNumber(int digit, byte Column, byte Row);
    void _printOneBigNumber(int digit, byte Column, byte Row);
    [glow]int _pin;[/glow]
};

#endif

My LCDFonts.cpp I tried to make work (only relevant bit pasted):

#include "WProgram.h"
#include "LCDFonts.h"

LCDFonts::LCDFonts(int pin)
{
  _pin = pin;
}

My IDE to call the instance that worked:

#include <LCDFonts.h>

LCDFonts lcdfonts(1);

Is there something that I can do so I dont have to put in the ‘1’, and have pin and _pin variables in the .h and .cpp when I dont need them?

Still learning so any help is appreciated.

Thanks

James

#include <LCDFonts.h>

LCDFonts lcdfonts;

Would have been fine (no brackets).

Done, Fixed and Working - thank you kindly.

Now to tweak and improve the code so its tidy.

Thanks

LCDFonts lcdfonts();

The lcdfonts() part of this statement is a function call. Since there is no function anywhere called lcdfonts, the compiler issues an error.

Using a function in place of a variable name, which is what is required after the type declaration, is also an error, so the compiler issues an error.

One day, you'll be able to make sense of the error messages.