NewhavenDisplay / EVE-TFT-Modules

Select example projects from FTDI/Bridgetek’s EVE Software Examples webpage, pre-configured to work on our EVE TFT Modules.
10 stars 16 forks source link

Usage of Arduino Wire library #2

Open Koepel opened 2 years ago

Koepel commented 2 years ago

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 ?
  }

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.

CJoh-NHD commented 2 years ago

Koepel,

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.