I have about 10 values to transfer via I2C. What is the best way to format and transfer it?
4 of these values are Temperatures ( for example 120.3, float). Since the I2C library is just transfering bytes, how do I "convert" them? multiplicating with 10 doesnt work..
3 of the values are values like 1 and 0, no problem.. (uint8_t)
3 others are values from 0 to 6000 ( uint16_t)
The uint16_t and float values are requested in an intervall of 5-7hz, the uint8_t values ~20hz...
So I will do something like this since the "onRequest" funktion doesnt allow to choose what to request
Basically what you are asking is how do you send a multi-byte value through an interface that only sends one byte at a time. Easy way to do it is by leverage unions:
typedef union {
float f;
uint8_t b[sizeof(float)];
} FLOATI2C_t;
You know what is stored in the memory when you have the value "120.3, float"? Take a look to this:
About the real question, the suggestion of Arrch, seams good to me. In other systems (not Arduino) I begin doing the same that Arrch says, but I end doing something like:
These methods of transferring data all work the same way and will
work as long as both ends store the data the same way and in the same byte order.
If you are transferring between different processor types, or processors that have different
register sizes, it isn't this simple and you have to take extra steps.
i.e. this method of transferring the data is not fully portable across
all platforms.