You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In many files where Wire.requestFrom() is used, I see the comment: "/* blocking call - at least one byte must be available */"
That is not how the Arduino Wire library works.
The Wire.requestFrom() does the whole I2C session and that function itself is blocking. It only returns when the I2C session has completely finished.
After that, the received data is in a buffer in the Wire library. The Wire.read() and Wire.available() operate on that buffer.
So there is no need to wait for something. The data is in the buffer, or not.
Example to check if there was data:
int n = Wire.requestFrom(0x6f, length);// request length bytes from slave device and end the transmission after this
if(n==(int)length) // test if the data was received
{
for(i=0;i<length;i++)
{
buffer[i] = Wire.read(); // the data is there for sure, because that was tested
}
}
else
{
// return some kind of error ?
}
Thank you for bringing this to our attention and the in-depth explanation.
The example programs seen in this repository were written by FTDI/Bridgetek. Therefore, fixing this bug in all the example sketches is time-consuming and low-priority at the moment.
In many files where
Wire.requestFrom()
is used, I see the comment: "/* blocking call - at least one byte must be available */"That is not how the Arduino Wire library works.
The
Wire.requestFrom()
does the whole I2C session and that function itself is blocking. It only returns when the I2C session has completely finished.After that, the received data is in a buffer in the Wire library. The
Wire.read()
andWire.available()
operate on that buffer.So there is no need to wait for something. The data is in the buffer, or not.
Example to check if there was data:
After a
Wire.requestFrom()
, there is no need to wait for something. I call that common mistake number 1.See also my alternative explanation of the Wire library.
The text was updated successfully, but these errors were encountered: