xv4y

Writing a multi-plateform library with IF DEFINED

5 posts in this topic

Hi,

 

I am trying to write a library that could compile with both Arduino and Energia (MSP430).

It should be easy because there are only a few changes involved in the .h header file (calls to different libraries) and the .cpp implementation file. I try to use the following preprocessor directives. 

#if defined (ARDUINO)

#elif defined (ENERGIA)

#endif

 

However this does not work as Energia still try to compile the arduino code.

Am I doing something wrong ?

Did someone experience the same ?

 

Thanks,

Yan.

 

Share this post


Link to post
Share on other sites

Well, I read a little bit more about GCC preprocessor directives and found that even if the code is not compiled, it has to be valid since it goes through transformations and tokenization.

That poses a problem because the AVR interrupt declaration are not valid for mspgcc...

SIGNAL(TIMER1_COMPA_vect)
{
...
}

 

I will have to dig in the AVR code a little bit more than I though.

 

Yan.

Share this post


Link to post
Share on other sites

Well, finally it is related to the inclusion of legacymsp430.h.

If I put it in the main code, it works. If I put it in a #if defined it does not work.

Please can someone have a look to my code at tell me if I am doing something wrong here :

#include "VirtualWire.h"
#include <util/crc16.h>

#if ARDUINO >= 100		// Recent Arduino IDE versions
#include <Arduino.h>
#elif ARDUINO < 100		// Older
#include <wiring.h>
#include <WProgram.h>
#elif defined(ENERGIA)	// Energia for LaunchPad (not tested on FraunchPad and StellarPad)
#include <Energia.h>
//#include <legacymsp430.h>
#define SIGNAL()
	void()
#else // error
#error Platform not supported
#endif

 

It is late now and I must go to sleep. Tomorrow kids get up at 5:30 at go to school at 7...

 

Thanks,

Yan.

Share this post


Link to post
Share on other sites

Energia uses both ARDUINO and ENERGIA variables to compile.

 

So you need to test ENERGIA first and ARDUINO after:

 

 

#if defined(ENERGIA)
...
#elif defined(ARDUINO) && (ARDUINO>100)
...
#endif

 

 

Please refer to this tutorial: Manage Code for Multiple Platforms with two approaches, MCU-based and IDE-based.

 

If you have a Mac computer, why not to try the template I've developed for Xcode? Have a look at embedXcode, embedded computing on Xcode.

xv4y likes this

Share this post


Link to post
Share on other sites

Hi Rei Vilo,

 

Once again you're a master.

You tutorial is really helping.

At first I took your 5110 lib as example, but going the IDE specific way is not the best since this library (VirtualWire) use Timers that are specific to processors.

Going with the MCU specific macros did the trick.

 

To be honest, yesterday right after turning off the computer and going downstair heading to the bed I found a solution with more macro def...

I tested it this morning and it was compiling ok. I have not checked it was running ok thought.

Anyhow, it was more a workaround than a neat solution.

 

Thanks,

Yan.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now