pololu/vl53l0x-arduino

ESP8266 divide by 0 reboot when device not present

jstuewe opened this issue · 1 comments

On my ESP8266, this library caused reboots during init when the device wasn't found on the I2C bus. I tracked it down to the 'setMeasurementTimingBudget' call and then to the return line in the timeoutMicrosecondsToMclks routine.
1016: return (((timeout_period_us * 1000) + (macro_period_ns / 2)) / macro_period_ns);

The 'macro_period_ns' variable was zero which generated a Div0 error\crash.
I added the line below to fix it, but I'm sure there is a better way.
if (!macro_period_ns) macro_period_ns++; //avoid divide by zero

Great library, Thanks for contributing it.

init() now starts by checking an ID register to make sure a VL53L0X is connected (and immediately returns false if it isn't), so I think this shouldn't be an issue anymore.