Arduino 22 and 1.0 backwards compatible libraries

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jersagfast
 
Posts: 83
Joined: Mon May 16, 2011 1:10 pm

Arduino 22 and 1.0 backwards compatible libraries

Post by jersagfast »

Hi everyone, just wanted to share a bit of code to make your libraries compatible with Arduino 1.0 as well as keep them working on Arduino 22. You can make them work easily by simply changing "WProgram.h" to "Arduino.h", but if they are shared, anyone who does not upgrade to the Arduino 1.0 IDE will lose the function of that library.
You will need to change the header file that ends in .h as well as the source file that ends in .cpp. As you might already know, the file "WProgram.h" has been replaced with "Arduino.h" in version 1.0 of the Arduino IDE.
You can make the library backwards compatible by replacing the line #include "WProgram.h" with the following code:

Code: Select all

#if defined(ARDUINO) && ARDUINO >= 100
 #include "Arduino.h"
 #else
 #include "WProgram.h"
 #endif
This simply checks for the version of Arduino and loads the correct file. When there is an upgrade to the Arduino IDE, the version number will be higher than 100, still keeping the function of the code listed above. I have tried this with some small libraries that I have made as well as the PCD8544 (Nokia 5110/3310 LCD from Adafruit) and it works well. Happy coding. :)

stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am

Re: Arduino 22 and 1.0 backwards compatible libraries

Post by stephanie »

For libraries that are affected by some of the other changes, you can still wrap those changes in the same #if #else #endif lines, to keep the library working for both old and new versions of the IDE.

For example, a library that uses the Wire library (I2C) would need to have all the Wire.send and Wire.receive statements changed to Wire.write and Wire.read. Or if your library has a class derived from Print, you'll need to modify how the write method is implemented as write now returns a value.

Any of these modifications can be done to keep the library backwards-compatible, by keeping the old and new code wrapped in the compiler conditionals:

Code: Select all

  byte buffer[20];
  int i;
  for(i=0; i<20; i++) {
#if ARDUINO >= 100
    buffer[i] = Wire.read();
#else
    buffer[i] = Wire.receive();
#endif
  }
I found a good short primer to what changes were necessary over at Makezine blog: http://blog.makezine.com/archive/2011/1 ... ore-120324 That lists the changes that causes 1.0 to break libraries, and how to fix them.

Cheers!

User avatar
pburgess
 
Posts: 4120
Joined: Sun Oct 26, 2008 2:29 am

Re: Arduino 22 and 1.0 backwards compatible libraries

Post by pburgess »

Hi folks,

Many of the Adafruit libraries have already been updated for dual compatibility between Arduino 1.0 and prior versions...but I'll be going down the whole list today and looking for any holdouts. I don't necessarily have every representative piece of hardware here though...so while I can confirm things compile, I can't always confirm they'll interoperate correctly. Please let me know if you encounter any shenanigans with purported "1.0 compatible" libraries after upgrading to Arduino 1.0.

I'll be keeping the ".pde" extension for example sketches for the time being, at least until 1.0 (or later) is more widely adopted in the wild.

User avatar
liudr
 
Posts: 121
Joined: Sat Jan 15, 2011 11:08 pm

Re: Arduino 22 and 1.0 backwards compatible libraries

Post by liudr »

Thanks for the info. I have written a half dozen libraries that need to be made dual compatible. Great joy from the Arduino guys that just like to change things.

User avatar
baldengineer
 
Posts: 127
Joined: Sun Sep 26, 2010 11:49 pm

Re: Arduino 22 and 1.0 backwards compatible libraries

Post by baldengineer »

liudr wrote:Great joy from the Arduino guys that just like to change things.
That's an unfair and unfounded accustation to make. You actually think the changes made in 1.0 were just because the Arduino team felt like it?

It seems pretty clear, to me anyway, the changes all had some purposeful decision behind them.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”