I2C_anything Library problems

Hello

Does anyone know how to apply the I2C_anything library in the web IDE?
I have added the i2c_anything_ng.h library, and if you click on the header tab,
the code indeed seems to be there, but if you click the header in the left side,
it’s just a structure of some kind. I don’t think the header containing the
functions is added, or am i doing something wrong?

@TheLostProgrammer, welcome to the Particle community!! I’m not sure that library adds any value. What’s your use case?

I receive data from an Atmega2560 via. I2C, but the data isn’t sent as bytes as usual, and i therefore have to use the I2C_readAnything() function. Problem is that when the library has been added, the compiler doesn’t recognize the function.

@TheLostProgrammer, I just compiled the example (usage.ino) targetting a Photon with DeviceOS 1.5.0-rc.2 and it compiled just fine. Did you attach the library to your application as just adding a #include i2c_anything_ng.h is not enough. Look HERE under “Using Libraries” for help.

Can you elaborate?
What if not bytes can be sent via the I2C bus?

As far as I’ve understood from my colleague, he sends the data via the I2C connection, using the I2C_writeAnything function. Typically one would send a string or some value such as a single byte via. the wire. This is not entirely clear to myself, but it’s not really a part of my problem. The problem I’m facing is the fact that I can’t use the I2C_readAnything() function from the library :slight_smile:

I have a test where i apply the I2C_readAnything() function on another ATmega2560, and data is received perfectly. So again, I would like to be able to use the I2C_readAnything function in the same way as the following:

image

But when I add the library and try to use the function in my
receive_event function, the compiler informs me that the I2C_readAnything
isn’t declared in this scope.

If you mean whether i’ve followed the steps of adding the library in
the same way as the tutorial shows, then yes. I have not just included the
library. I also just tried to compile and flash the electron with the usage.ino
and it also works. The problem arises when I try to use the I2C_readAnything function.

I’d like to use it like this:

image

But when I compile my code, it states that I2C_readAnything isn’t declared
in this scope.

@TheLostProgrammer, looking close at the Web IDE library, I believe it is incomplete. Have a look at my shared example which includes just the I2C_Anything.h file, as is done in the original Arduino library.

https://go.particle.io/shared_apps/5e7ac1d245a37c0014837ead

In your shared example it seems like you have included the specific library file
that I’d like to add to my application. How have you done this? and more importantly,
does it work as exprected?

I to believe the library I’ve tried to add previously is incomplete!

Thanks peekay123, for that example, I have now added only the library file :slight_smile:
I’ll return later today if I’m still having issues! This was very informative, thanks!

You can actually send any datatype via I2C since each and every datatype comprises of individual bytes.
While this library may make things easier, it’s always best to first know/understand the fundamentals before using “black-box-magic”. This way you’ll be armed to tackle any eventual bumps or shortcomings of any given library and may even be able to adapt a library to fit your needs.

e.g. in order to send an int you’d just write

  int i;
  Wire.write((const uint8_t*)&i, sizeof(i));

And since the TwoWire class inherits from Stream you can also use this to read data back

  Wire.readBytes((char*)&i, sizeof(i));

you should be set without any additional library.
(endianness may be something that requires extra attention tho’)

BTW, in principle this also is what the “library” does (also without accounting for endianness - which may be your issue - what datatype do you intend to transfer) but IMO the use of the “buffer read” function seems more refined than the loop approach.

However, these templated functions might be worth considering to be added to the Stream class as they might be quite useful for all stream based classes.
@avtolstoy, what’d you think?
Might even be worth a contribution to the Arduino Stream implementation?

1 Like

ah cool, thanks for the answer :slight_smile: I’m am quite new to programming so my questions might seem trivial to most programmers. So again thanks!

1 Like