Pages: [1] 2 3   Go Down
Author Topic: How to use LiquidCrystal into an own library  (Read 2651 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Suppose I want to use LiquidCrystal arduino library through a library written by me. I don't want to use LiquidCrystal in the sketch program.

If I call LiquidCrystal on myLib.h like this:

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

#include <inttypes.h>

class LiquidCrystal; // Instead of #include <LiquidCrystal.h>. Booth runs ok

#ifndef MyLib_h
#define MyLib_h

class MyLib{
    public:
        MyLib();
        ~MyLib();

    private:
        LiquidCrystal lcd(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
};

#endif

This compiles properly on the sketch program. I initialize LiquidCrystal constructor parameters because this library does not have a constructor without parameters, and if I call lcd like this:
'LiquidCrystal lcd;'
The following error appears: "field ‘lcd’ has incomplete type"

This is the simple skecth program, just to test MyLib:
Code:
#include <MyLib.h>

MyLib novaCpu = MyLib();

void setup(){
}

void loop(){
}

The problems appears with MyLib.cpp
If I try this on the constructor cpp, i.e.
Code:
...
#include <LiquidCrystal.h>
...
MyLib::MyLib(){
     lcd(1,2,3,4,5,6,7,8);
}
...

The following errors are received:
Quote
in .cpp:  error: invalid use of incomplete type ‘struct LiquidCrystal’
in .h:     error: forward declaration of ‘struct LiquidCrystal’

And If I try to write this, instead of previous definition:
Code:
...
#include <LiquidCrystal.h>
...
MyLib::MyLib(){
     LiquidCrystal lcd(1,2,3,4,5,6,7,8);
}
...

I have an only error in .cpp:
Quote
error: variable ‘LiquidCrystal lcd’ has initializer but incomplete type


What can I do? Thanks people
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't want to use LiquidCrystal in the sketch program.
You have to. Get over it.

Hiding the use of libraries is not in keeping with open source, so it's not allowed.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul could you perhaps elaborate on your, typically, terse reply?
I'm not following it in the slightest.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Paul could you perhaps elaborate on your, typically, terse reply?
That was not typical. It was way terser that usual.

The IDE looks at the sketch, to find what to copy to the build directory. Any header file that is referenced in the sketch is copied, along with all the other files in the same folder that the header file is found in.

Include statements in other files do not result in any files being copied.

So, if the sketch does not include LiquidCrystal.h, the LiquidCrystal library is not available for use. When another library, such as OP's library wants to use LiquidCrystal, it is not available. Only libraries whose header files are included in the sketch are available for use.

There are arguments that can be made that this is not the best way, but suppose that other files are searched, and OP releases his library that uses two or three other libraries, and each of them uses 3 or 4 more. Tracking down missing libraries gets to be a nightmare, as does determining how much memory the libraries are using.

Seeing a sketch posted that includes one library doesn't usually raise low memory warnings. Seeing a sketch that uses 22 libraries does. If the header files are not all in the sketch, it becomes much more difficult to track them down.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for making the effort and taking the time.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, I understand PaulS, very thankful

However I'm thinking that I already have an own library, which includes one library to use, this second library is not included on my sketch program, only first library has, and skecth compiles ok. I have to specify that all files are in the same directory, and this 'sub library' is also an own library, not an 'oficial' arduino library like LiquidCrystsal (this is the threat forum that you helped me to solve few days ago).

So anyway, this 'hack' is only possible with own libraries, not with another libraries?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyway, I'm working on it including myLib and LiquidCrystal in the sketch program.

As a private member on MyLib.h, I declare
Code:
LiquidCrystal lcd(uint8_t,uint8_t,uint8_t,uint8_t,uint8_t,uint8_t,uint8_t,uint8_t);
just like the begining, and compiles ok.

In MyLib constructor, on MyLib.cpp, I write
Code:
lcd(d4, d5, d6, d7, a2, a3, a4, a5);
and compiles ok (d's are uint8_t variables)

But this 'lcd' is not able to use. Any liquid crystal functions make an error.

If I dont write anything about LiquidCrystal in .h, instead of write this in MyLig.cpp constructor:
Code:
LiquidCrystal lcd(d4, d5, d6, d7, a2, a3, a4, a5);
lcd.begin(40,4);
This complies so I also can use LiquidCrystal functions. But the problem is that this is not a class global variable, and if I try to call
Code:
"lcd.print("hello");"
in one of the MyLib.cpp class methods, I have the following error:
Quote
error: ‘lcd’ was not declared in this scope

So what can I do? I want a LiquidCrystal global variable. Thanks to all
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want a LiquidCrystal global variable.
You want a global variable or a member variable?

Quote
Any liquid crystal functions make an error.
Which is?

Quote
In MyLib constructor, on MyLib.cpp, I write
That doesn't create a global variable. That creates a local variable that goes out of scope when the constructor ends.

Quote
So what can I do?
Post ALL of your code, not just snippets.
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 799
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't want to use LiquidCrystal in the sketch program.
You have to. Get over it.

Hiding the use of libraries is not in keeping with open source, so it's not allowed.

I would say that a person is "allowed" to write whatever kind of code he or she wants to.

Hiding code is certainly not in keeping with the concept of "open source" and should be discouraged, but it cannot be disallowed.
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hiding code is certainly not in keeping with the concept of "open source" and should be discouraged, but it cannot be disallowed.
You could argue that it shouldn't be disallowed. You can't argue that it can't be disallowed. Currently, it is.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so I post you the code:

MyLib.h
Code:
#if(ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include <inttypes.h>

class LiquidCrystal; // Instead of #include <LiquidCrystal.h>. Booth runs ok

#ifndef MyLib_h
#define MyLib_h

class MyLib{
    public:
        MyLib();
        ~MyLib();

    private:
        LiquidCrystal lcd(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
};

#endif

sketch program (his goal is to test MyLib library)
Code:
#include <MyLib.h>
#include <LiquidCrystal.h>

MyLib libObject = MyLib();

void setup(){
}

void loop(){
}

Considering previous code, can you write a simple MyLib.cpp? I just want to undersant how to treat, in .cpp file, the lcd variable previously defined in .h. The goal is to create a LiquidCrystal member variable (I mean, LiquidCrystal object as a member of myLib class, to be used through myLib methods when myLib methods are called in sketch program)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It doesn't really matter what the cpp file looks like. You are creating lcd as a private member of the class. That contradicts with your stated goal of a global variable.

So, what is it you are trying to do?

Code:
MyLib libObject = MyLib();
Wrong. You NEVER call a constructor directly.
You create an instance of the class like:
Code:
MyLib libObject;

If you change the constructor to require arguments, you pass them to the instance, directly:
MyLib libObject(theArgsGoHere);

Or, you use pointers:
Code:
MyLib *libObject = new MyLib();
or
Code:
MyLib *libObject = new MyLib(theArgsGoHere);

(Just exactly like LiquidCrystal objects are created.)


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The goal is to create a LiquidCrystal member variable (I mean, LiquidCrystal object as a member of myLib class, to be used through myLib methods when myLib methods are called in sketch program)

My goal is just that: having a LiquidCrystal object to be used by myLib methods, when myLib methods are called in sketch program (using the libObject object previously created).

I write an example but using an uint8_t type isntead of a LiquidCrystal type, to show you what I want to do, because whit a simple type runs ok:

Here is the code:

Sketch program (thanks for the libObject call specification)
Code:
#include <MyLib.h>
#include <LiquidCrystal.h>

MyLib libObject;

void setup(){
  Serial.begin(9600);
  libObject.methodData();
}

void loop(){
}

MyLib.h:
Code:
#include <inttypes.h>

class LiquidCrystal; // Instead of #include <LiquidCrystal.h>. Booth runs ok

#ifndef MyLib_h
#define MyLib_h

class MyLib{
    public:
        MyLib();
        ~MyLib();
        void methodData();

    private:
        uint8_t data;
        LiquidCrystal lcd(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
};

#endif

MyLib.cpp:
Code:
#if(ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include <inttypes.h>
#include <LiquidCrystal.h>

MyLib::MyLib(){
data = 10;
}

void MyLib::methodData(){
data = 50;
}

MyLib::~MyLib(){
}

There is no problem with 'data' as private member. It can be used in 'methodData()' as a recognized declaration in this scope. My question is: Can I do the same with the 'lcd' private variable, instead of 'data'? In case I can, how I have to declare and definite lcd variable? In case it's not possible, what could I do?

Thanks for the patience
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can I do the same with the 'lcd' private variable,
Of course.

I'm failing to see what the problem is. If the code you posted compiles and executes, then there IS an instance of LiquidCrystal being created, so you could add lcd.print() to MyLib::methodData() and it should work,

If not, you'll need to tell us exactly what happens.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6619
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Suppose I want to use LiquidCrystal arduino library through a library written by me. I don't want to use LiquidCrystal in the sketch program.

Simple, just copy the LiquidCrystal library source code into your own library. But you have to respect the open source license agreement if/when you distribute your own library.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1] 2 3   Go Up
Jump to: