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:
Wire.beginTransmission (SLAVE_ADDRESS);
Wire.send (x);
Wire.endTransmission ();
Has to be changed to:
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:
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:
#if !(defined(ARDUINO) && ARDUINO >= 100)
#define Wire.write(x) Wire.send(x)
#define Wire.read(x) Write.receive(x)
#endif
Error:
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:
// 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:
Wire.beginTransmission (SLAVE_ADDRESS);
i2c_write(x);
Wire.endTransmission();
But it still is a bit clunky. Is there a neater way?