[Arduino] Develop Library with Arduino eclipse plugin

Photobucket
eclipse.baeyens.it presents an awesome plugin for eclipse to develop Arduino project. In my opinion, it can replace ADE, Arduino Development Environment, and moreover, it has great compatibility to work with ADE. The library being created and tested in Eclipse can be easily and smoothly be imported to ADE libraries and be shared with the community.

This solution does not contain Serial Monitor yet, but there are plenty alternatives for that. And the serial monitor in ADE only send and display visible char. It is recommended to find a serial terminal that can send, receive and show data both in both text and hex code.

Eclipse can do Arduino Development before this plugin is created. However the current guide on Arduino playgound is way too long to process and let me want to shoot myself. Arduino eclipse pulgin has make it much much easier.

This blog will be a combination of Arduino eclipse Plugin guide and LibraryTutorial on arduino playground to present a setp-by-step tutorial. Check them first would be helpful for understanding.

1. Setup Eclipse and Arduino Plugin.
Photobucket
The official installation page is here. In my experience, all you need is a Eclipse with CDT (C/C++ Development Kit). Generally it does not really matter if using the same copy of eclipse to develop projects in Java, Ruby or others. But CDT is essential, as Arduino sketch and it libs are written and compiled in C++. There is no need to install the AVR plugin in advanced. The Arduino Plugin has somehow included it already. In this case, I setup a new “pure” environment for Arduino development: Eclipse 3.7 Indigo + CDT + Arduino Plugin + Subclipse

2. Begin with a Sketch

After plugin installation and configuration,

1) Create a workspace, in this case, “H:\workspace\myArduinoLibs”.
Photobucket
2) Create a sketch, in this case, “MorseTest”
Photobucket
Photobucket
Two Folders will show up in the Project Explorer. One would be named as the Arduino board you got, as the Arduino Core. The other one would be the project just created. The core folder will be created when starting working with a new workspace. If adding another sketch/Arduino Project in this workspace, the core folder would not be created again. It will be shared with all sketchs in the workspace. In the creation of another project using an other type of board, a new core folder will be created then.
Photobucket
3) Build the core, but that is not needed. To do this, click the Core project (“Arduino_UNO”), and click the hammer icon in the Toolbar.
4) Build the sketch. Click the sketch (“MorseTest”), and click the hammer icon in the Toolbar. If the core has not been built yet, it will be built now.
Photobucket
There should not be any error or warning in the console showing up by doing only these. Apparently, there is barely nothing in the sketch.

It is true that the core is not clean (warning free) yet as the plugin author said. But the program can be compiled, and works fine. In my experence, I have to add “extern HardwareSerial Serial;” to make Serial work. No big deal. ADE feels no problem on that too.

5) It is also recommended to upload, by clicking the “AVR” button in the toolbar, this “empty” sketch to the Arduino Board right now to check whether the communication with the board works properly.

6) Copy and paste the sample code on librayTutorial into “MorseTest.cpp”. The header file, “MorseTest.h”, can be left alone since the very beginning. No need to change the header file at all.

// Do not remove the include below
#include "MorseTest.h"
 
void dot();
void dash();
 
int pin = 13;
 
//The setup function is called once at startup of the sketch
void setup() 
{
// Add your initialization code here
	pinMode(pin, OUTPUT);
}
 
// The loop function is called in an endless loop
void loop()
{
//Add your repeated code here
	dot();	 dot(); dot();
	dash(); dash(); dash();
	dot();	 dot(); dot();
	delay(3000);
}
 
void dot()
{
	digitalWrite(pin, HIGH);
	delay(250);
	digitalWrite(pin, LOW);
	delay(250);
}
 
void dash()
{
	digitalWrite(pin, HIGH);
	delay(1000);
	digitalWrite(pin, LOW);
	delay(250);
}

I have added declaration for dot() and dash(), because they were originally located after the loop(). Eclipse could not compile it, unless adding the declarations or putting these functions ahead of loop(). ADE 1.0 does not have such “strict” rules. But adding declarations will not cause any trouble in ADE. Too loose will make exprienced C/C++ programmers feel very unsafe. Believe me on that.

The greatness and convenience of Arduino development on Eclipse can experienced right now:

  • “Tab” key works great.
  • Colorful high-lighted code.
  • Ctrl+Shift+F for auto-formatting.
  • Alt+/ to auto-complete.
  • Realtime warning and error hint.
  • Ctrl+Left Click for reference, this one is my favorite. Fantastic!

7) Build it and upload it. The onboard LED should be sending SOS signal now.

4. From Process-Oriented to Object-Oriented

1) Create a sub-Folder for library in the workspace project, in this case, “Morse”.
Photobucket
2) Add a new Class in the library folder, namely “Morse”.
Photobucket
There would be two files be created at the same time, the .h one is the header file, and the other .cpp one is the source file.

Photobucket

3) Include “Arduino.h” in the lib header.

There is no need to build the library from nothing. And we still want to use the functions that the Arduino core provides. In my point of view, “Arduino.h” would be included in any sketch anyway, including it in the lib would not make the hex or elf output any bigger. Just image developing a sketch without setup() and loop(). Including only “Arduino.h” seems will cover all the headers that may be needed.

4) Create the class.

As the librayTutorial, Morse.h:

/* Morser.h */
 
#ifndef MORSE_H_
#define MORSE_H_
 
#include "Arduino.h"
 
class Morse
{
public:
	Morse(int pin);
	virtual ~Morse();
	void dot();
	void dash();
 
private:
	int _pin;
 
};
 
#endif /* MORSE_H_ */

Morse.cpp:

/* Morse.cpp */
 
#include "Morse.h"
 
Morse::Morse(int pin)
{
	// TODO Auto-generated constructor stub
	_pin = pin;
	pinMode(_pin, OUTPUT);
}
 
Morse::~Morse()
{
	// TODO Auto-generated destructor stub
}
 
void Morse::dot()
{
	digitalWrite(_pin, HIGH);
	delay(250);
	digitalWrite(_pin, LOW);
	delay(250);
}
 
void Morse::dash()
{
	digitalWrite(_pin, HIGH);
	delay(1000);
	digitalWrite(_pin, LOW);
	delay(250);
}

The main difference between the code in eclispe and that in ADE is that we have a vitual destructor here. It is very common in C++ program. “vitual” helps the class be capable to have sub-class. I believe the GCC in ADE will love this way eclipse complete the class.

Photobucket
Right now the class has been created independently.

5) Use the class.

Return to “MorseTest.cpp”, since most of the functions have been moved to the class of the library, the sketch got to be much nice and clean now. Do not fotget to include the library header file here.

/* MorseTest.cpp */
// Do not remove the include below
#include "MorseTest.h"
 
#include "Morse.h" // The header of the class just created
 
Morse morse(13);
 
//The setup function is called once at startup of the sketch
void setup()
{
// Add your initialization code here
}
 
// The loop function is called in an endless loop
void loop()
{
//Add your repeated code here
	morse.dot(); morse.dot(); morse.dot();
	morse.dash(); morse.dash(); morse.dash();
	morse.dot(); morse.dot(); morse.dot();
	delay(3000);
}

6) Try to build it

Click hammer to build the MorseTest.cpp, You would see errors popped up.

Photobucket
The main reason is that the sketch could not find the library, even though it seems being included already. We need to add the library path to the project.

a) Right click the Sketch folder in the project explorer, “MorseTest”.
b) Choose “Properties”.
c) Go to “C/C++ General -> Path and Symbols”,
d) In the “Includes” tab, choose “GNU C++” in the language column, and click “Add…” at the right side.

Photobucket
e) Click “Workspace…” in the “Add directory path” window.
Photobucket
f) Choose the library folder, “Morse”, and click “OK”.
Photobucket
g) A new path has been added, as the screenshot below. click “OK” to close the window.
Photobucket
h) When this window pops up, click “Yes”.
Photobucket
i) Build it by Clicking the hammer again. The sketch should be built without any error or warning this time.
j) Upload it, it should work as same as the All-in-one, process-oriented sketch.

5. Use the library in ADE

1) Copy the class library folder (Morse) to the libraries folder of ADE (“H:\arduino-1.0\libraries\”)
2) Open ADE, you would find your library in the “Sketch->Import Library…” menu.
Photobucket
3) Click it, “#include <Morse.h>” would be added into the new sketch.
4) Copy the code from the sketch in eclipse, MorseTest.cpp, to ADE, without
#include “MorseTest.h” ADE will general a similar header file and hide it, or ADE does not need such header.
#include “Morse.h” Because it is included by importing lib already.
5) Compile this sketch and upload it. It should work~
Photobucket
6) Create a folder named “examples” in Your ADE library folder, “Morse”, and save this sketch into it, Named “SOS”.
Photobucket
7) Exit ADE, and Open it again. The SOS sketch would be found as a example for library Morse. It means this lib can be imported and sample-provided just as other libraries in ADE.
Photobucket

6. Use the library back in Eclipse

1 sketch = 1 main = 1 project. Main() is the entry point of your code in C/C++. Having more than 1 main is unacceptable to the compiler.

In the development of a whole new project with self-created libs, it is better and safer to import libraries from libraries folder of ADE, as the official guide.

If aim to write several examples for a single lib, I would copy the test/sample code to ADE and save a ino file in the ADE examples folder. So my test program, “MorseTest.cpp”, keeps being changed all the time. But when a test program is proved ok, I would save it as a individual ino file by ADE. As most of the test programs are simple and short. I personally do not want to create projects for each of them. Everytime the libs are updated, remember to copy them from the eclipse workspace to the ADE libraries to replace the old ones.

It is true that ADE does do a lot of jobs and help beginner to feel programming is easy, but its editor makes exprienced programmers frustrated. If you want to get more help from IDE, please try eclipse. Thanks to Jantje and his Arduino eclipse plugin, the arduino development become so enjoyable and high-efficient.

PS. Thanks Jantje for encouraging me to write this blog, and prove-reading.



关于aGuegu

阿古 真名:官微宏,技术Geek,玩Arduino,玩Linux,爱Google,爱开源;现居福州
此条目发表在内功心法分类目录,贴了, , , , , , 标签。将固定链接加入收藏夹。

[Arduino] Develop Library with Arduino eclipse plugin》有 6 条评论

  1. Jantje说:

    aGuegu
    Great work and thanks for the effort.
    Best regards
    Jantje

  2. Ben Runnels说:

    I’m working with the new I2C library and I’m unsure how to resolve the unresolved symbols errors I’m getting in I2C.cpp from constants like PORTD and PORTC. Things compile and work fine though. Any pointers?

    • aGuegu说:

      I have faced the same problem if I use ATmega8. When it comes to Atmega168, Atmega328 like Diecimila or UNO, it is fine. Using CTRL + Left click can help you check whether the declaration exsist.

  3. dgrc说:

    Thank you for this excellent tutorial. I’m new to Arduino, Eclipse, and C++. You’ve greatly increased my understanding of all three.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>