When the Arduino Slave receives data, the interrupt of the Wire library stores the data in a buffer and when that is finishes the receiveEvent() function is called. Then that buffer gets emptied with Wire.read() and you store it in a message[] buffer. Then you copy that into the i2cBuffer.
There are three buffers involved, and maybe another one in the Wire library.
Can you do make it simpler:
void receiveEvent(int byteNum)
{
if (byteNum == I2C_BUFFER_DEPTH)
{
// set some variables, and so on
Wire.readBytes( i2cBuffer.data[i2cBuffer.writeIdx], byteNum);
// increase a few other variables, and so on
}
}
The buffer gives you time to deal with data, but if you don't have that time, then it is not fail safe. Without 'volatile' and without disabling the interrupts to copy variables, you will not know for sure what went wrong and how many data is wrong.
Examples where you don't have the time to process data are:
- If someone adds code that writes a lot of data to a SD memory card.
- If the Master pounds the Slave with I2C sessions, then the loop() will barely run (with an Arduino Uno).
I still think that a buffer is trivial. A good sketch in the Master should not continuously pound the I2C Slave with I2C messages.