Pages: [1]   Go Down
Author Topic: Breaking up a project in classes / files  (Read 3204 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm trying to break up a larger project into multiple files. From what I read I understand that I HAVE to create libraries for this. Am I right about that?

In Processing I'm used to just adding a tab and creating a class, but that doesn't seem to work in Arduino? A new tab creates a new pde file by default but for Arduino that seems useless since it won't import that classes from the tabs into my main project it seems.

I have tried to create a library using h and cpp files. Now the problem seems to be that my library doesn't compile because it depends on other libraries. I don't know how to solve this.

For testing I created an empty project which uses MyClass. MyClass uses Messenger. My sketch then includes the

First I created MyClass.h and MyClass.cpp and placed them next to my sketch. My sketch includes the MyClass.h file.

With this approach I get the error:
23: error: Messenger.h: No such file or director

Apparently the code is trying to compile but the include path is not set the same way as my main sketch?

Then I try to put the 2 MyClass files in a folder with the same name inside the hardware/libraries/ folder. When I do this, and try to compile I get:

/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/MyClass/MyClass.cpp:9: error: 'Messenger' was not declared in this scope

What's up with that? How am I supposed to create my own classes that depend on other libraries? Is this not possible? I'm confused.

I'd rather not create a library at all. All I want to do is break up my main sketch into multiple files....

Cheers
 



Logged

MD, USA
Offline Offline
God Member
*****
Karma: 2
Posts: 663
A jack of all trades and a master of none!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why does it need to be broken up into multiple files?
Logged

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

You can have two pde files in one sketch directory. Opening the sketch will open both pde files, in separate tabs. You can put functions in one pde file, and call them from the other pde file.

You can only have one setup and one loop function, though.
« Last Edit: January 24, 2010, 12:41:37 pm by PaulS » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why does it need to be broken up into multiple files?
It doesn't "need" to be broken up in multiple files, but like any other larger software project I want to structure it and be able to reuse code. Besides that, the Arduino IDE is really too basic to work on large files. I hate to scroll back and forth all the time.

Quote
You can have two pde files in one sketch directory. Opening the sketch will open both pde files, in separate tabs. You can put functions in one pde file, and call them from the other pde file.

I know, but I tried it with classes and that didn't work (like in Processing). I guess it's because it's not Java. Still I would expect to be able to #include a header from the same folder as my sketch, have its cpp file compiled referencing the same libraries that my main sketch is able to use.

So really there is no way to create classes that use libraries, and (re)use those classes in a sketch?
Logged

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

It is. After all, a library is just a class file that you have shared.

On thing to keep in mind is that the only files that get compiled are those whose header files are referenced in the sketch. So, if you have a cpp file that includes a header file for another class, and the header file for the cpp file is included in a sketch, the cpp file needed by the cpp file won't be compiled, and you'll get all kinds of errors, unless you also include the referenced header file in the sketch.

Your MyClass.cpp file that includes Messenger.h won't cause Messenger.cpp to be compiled, unless you include Messenger.h in the sketch, too.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul I got it working now. I must say I find this build system a bit quirky at least. I guess the idea is to keep it simple, but it would be nice to have optional control over includes and library paths from a config file or something.

Anyway it's good to know how it works.

Cheers
Logged

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

I have the same question and see no useful answer here. I've broken up the project into several "libraries", which are shared with several "sketches". For instance, a RS485 library that is used by all other modules. This RS485 library now depends on a time library I created and I can't figure out how to include the time header file in the RS485 cpp file.

That is, in my library directory I have, for simplicity's sake let's call it a rs485 and a time directory. Each contain their cpp and h files. The sketch contains "#include" for both time and rs485. All is happy. Now if I want to use the time class within the rs485 class, I can't find a way to include the time header in the rs485 cpp file.

Thanks!

g
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 198
Posts: 12753
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have the same question and see no useful answer here
Not surprising given that it is not the same question.

Quote
I've broken up the project into several "libraries", which are shared with several "sketches". For instance, a RS485 library that is used by all other modules. This RS485 library now depends on a time library I created and I can't figure out how to include the time header file in the RS485 cpp file.

That is, in my library directory I have, for simplicity's sake let's call it a rs485 and a time directory. Each contain their cpp and h files. The sketch contains "#include" for both time and rs485. All is happy. Now if I want to use the time class within the rs485 class, I can't find a way to include the time header in the rs485 cpp file.

#include "..\time\time.h"
Logged

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

Which libraries are compiled and linked is entirely a function of which library's header files are included (directly or indirectly by the IDE) in the sketch.

You can not make one library include another library without the sketch knowing that.
Logged

San Pedro, CA.
Offline Offline
Full Member
***
Karma: 3
Posts: 155
My head HURTS!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ 0x80,

I am not an expert programmer in any way. However, I,ve been working on a large project myself for some time. The way I break up the code into managable chunks is by adding tabs and naming them as .h files only. (no .cpp files) You can manage the tabs in what ever fashion you choose. By related functions, particular hardware device, etc. What you need to do for this to work is put a #include for each tab in the main .pde. Also, #include any libraries used by those tabs in the main .pde. If you get errors in a particular tab for functions used by a library in that tab, then #include the library in that tab instead of the main .pde. This has been working for me very well. One more thing, when you #include the tabs you have created in the main .pde,
use qoutes instead of <>. For example #include "myTab.h" not the usual #include <myTab.h>. I believe (pretty sure) the reason for this is the quotes instruct the compiler to look for the included files in the local folder instead of the libraries folder. Maybe someone with more experience than I can explain this better. As I said above, I've been managing large coding projects successfully in this manner for some time now with no problem (except for my own coding mistakes  :smiley ).

Hope this helps,
DJ
Logged

DigitalJohnson

Pages: [1]   Go Up
Jump to: