Pages: [1]   Go Down
Author Topic: What's the easiest way of making the Wire library backwards compatible?  (Read 748 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to update some examples that use the old Wire (I2C) library. Unfortunately because of the way functions have been renamed it is hard to do it cleanly.

For example:

Code:

    Wire.beginTransmission (SLAVE_ADDRESS);
    Wire.send (x);
    Wire.endTransmission ();

Has to be changed to:

Code:

    Wire.beginTransmission (SLAVE_ADDRESS);
    Wire.write (x);
    Wire.endTransmission ();

But for people wanting to use the old 0022 IDE that won't work. So this perhaps:

Code:

    Wire.beginTransmission (SLAVE_ADDRESS);
#if defined(ARDUINO) && ARDUINO >= 100
    Wire.write (x);
#else
    Wire.send (x);
#endif
    Wire.endTransmission ();

But that looks really clunky if you have lots of reads/writes throughout the code. It obscures what you are trying to show.

I tried this but it doesn't compile:

Code:
#if !(defined(ARDUINO) && ARDUINO >= 100)
  #define Wire.write(x) Wire.send(x)
  #define Wire.read(x) Write.receive(x)
#endif

Error:

Code:
sketch_feb15a.cpp: In function 'void setup()':
sketch_feb15a:15: error: expected primary-expression before '.' token
sketch_feb15a:15: error: 'x' was not declared in this scope
sketch_feb15a:15: error: expected `;' before 'Write'

So far I used a "glue" function that compiles, but looks kinda weird:

Code:
// glue routines for version 1.0+ of the IDE
uint8_t i2c_read ()
  {
#if defined(ARDUINO) && ARDUINO >= 100
  return Wire.read ();
#else
  return Wire.receive ();
#endif
  } // end of i2c_read
 
void i2c_write (int data)
  {
#if defined(ARDUINO) && ARDUINO >= 100
  Wire.write (data);
#else
  Wire.send (data);
#endif
  } // end of i2c_write

Now I can write:

Code:
  Wire.beginTransmission (SLAVE_ADDRESS);
  i2c_write(x);
  Wire.endTransmission();

But it still is a bit clunky. Is there a neater way?
Logged

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

Quote
But it still is a bit clunky. Is there a neater way?
Other that the name, I disagree. I think that if the function names were Wire_read and Wire_write, or something along those lines, that the code looks quite clean. You could hide those functions away in a separate file, and clean it up even more.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't forget the Wire.write (0); bug. That hit me on my first attempt to convert a V22 sketch to V1.0 which used the I2C library, even after I changed all the send/write and read/receive nonsense. It sounds like they have a patch for the new I2C library to eliminate that bug but who knows when the next release will be and if in fact it will include the fix?

Lefty
Logged

Pages: [1]   Go Up
Jump to: