adafruit / RTClib

A fork of Jeelab's fantastic RTC Arduino library
MIT License
798 stars 709 forks source link

Add function isPresent() to check if i2c connection has been established #279

Open resterampeberlin opened 1 year ago

resterampeberlin commented 1 year ago

The current implementation of RTClib provides no way, to check "on demand" if the i2c device has been connected and initialized. The only way is the return value of the "begin" function.

However, the protected variable "i2c_dev" in the class "RTC_I2C" does contain this information. If it is "NULL", the device has not been initialized.

Therefore, I added a function to "RTC_I2C" to check if this variable is NULL or not. Unfortunately, all classes derived from RTC_I2C must be derived "public". Otherwise, the variable is not visible to code outside the derived classes.