Seeed-Studio / Grove_Digital_Light_Sensor

TSL2561 & ISL29035
MIT License
13 stars 14 forks source link

Endless hang when the light sensor is not connected or not responding #5

Open Abestanis opened 2 years ago

Abestanis commented 2 years ago

Hello, thank you for this awesome library!

I recently used it in a project, but I had to make some adjustments to the code, because I observed the following behavior: I'm calling TSL2561_CalculateLux::readIRLuminosity which would never return, if the light sensor wasn't connected to the Arduino. This happened often during development, when the system was only partially assembled and it could have happened in the field, if a cable got loose or if the light sensor somehow wouldn't respond.

The reason for the hang is the following loop in TSL2561_CalculateLux::readRegister: https://github.com/Seeed-Studio/Grove_Digital_Light_Sensor/blob/457fcf7454b02fb33d95b1154ce73c27e2346cb2/Digital_Light_TSL2561.cpp#L41

If the light sensor never responds, Wire.available() will never return true, making the loop an endless loop.

For this reason, I added a timeout to the while loop and set an error flag on the TSL2561_CalculateLux in the event of a timeout, which my code could check.

Would you be willing to accept a pull request which adds a timeout and some way to detect the timeout?

lakshanthad commented 2 years ago

Hello,

Please PR and I will check it.

Thank you.