diff --git a/adafruit_lc709203f.py b/adafruit_lc709203f.py index 72fc171..002a223 100644 --- a/adafruit_lc709203f.py +++ b/adafruit_lc709203f.py @@ -62,6 +62,8 @@ LC709203F_CMD_ALARMPERCENT = const(0x13) LC709203F_CMD_ALARMVOLTAGE = const(0x14) +LC709203F_I2C_RETRY_COUNT = 10 + class CV: """struct helper""" @@ -156,17 +158,26 @@ def init_RSOC(self) -> None: # pylint: disable=invalid-name @property def cell_voltage(self) -> float: """Returns floating point voltage""" - return self._read_word(LC709203F_CMD_CELLVOLTAGE) / 1000 + try: + return self._read_word(LC709203F_CMD_CELLVOLTAGE) / 1000 + except OSError: + return None @property def cell_percent(self) -> float: """Returns percentage of cell capacity""" - return self._read_word(LC709203F_CMD_CELLITE) / 10 + try: + return self._read_word(LC709203F_CMD_CELLITE) / 10 + except OSError: + return None @property def cell_temperature(self) -> float: """Returns the temperature of the cell""" - return self._read_word(LC709203F_CMD_CELLTEMPERATURE) / 10 - 273.15 + try: + return self._read_word(LC709203F_CMD_CELLTEMPERATURE) / 10 - 273.15 + except OSError: + return None @cell_temperature.setter def cell_temperature(self, value: float) -> None: @@ -281,14 +292,31 @@ def _read_word(self, command: int) -> int: self._buf[1] = command # command / register self._buf[2] = self._buf[0] | 0x1 # read byte - with self.i2c_device as i2c: - i2c.write_then_readinto( - self._buf, self._buf, out_start=1, out_end=2, in_start=3, in_end=7 - ) - crc8 = self._generate_crc(self._buf[0:5]) - if crc8 != self._buf[5]: - raise OSError("CRC failure on reading word") - return (self._buf[4] << 8) | self._buf[3] + for x in range(LC709203F_I2C_RETRY_COUNT): + try: + with self.i2c_device as i2c: + i2c.write_then_readinto( + self._buf, + self._buf, + out_start=1, + out_end=2, + in_start=3, + in_end=7, + ) + + crc8 = self._generate_crc(self._buf[0:5]) + if crc8 != self._buf[5]: + raise OSError("CRC failure on reading word") + return (self._buf[4] << 8) | self._buf[3] + except OSError as exception: + # print("OSError in read: ", x+1, "/10: ", exception) + if x == (LC709203F_I2C_RETRY_COUNT - 1): + # Retry count reached + # print("Retry count reached in read: ", exception) + raise exception + + # Code should never reach this point, add this to satisfy pylint R1710. + return None def _write_word(self, command: int, data: int) -> None: self._buf[0] = LC709203F_I2CADDR_DEFAULT * 2 # write byte @@ -297,5 +325,14 @@ def _write_word(self, command: int, data: int) -> None: self._buf[3] = (data >> 8) & 0xFF self._buf[4] = self._generate_crc(self._buf[0:4]) - with self.i2c_device as i2c: - i2c.write(self._buf[1:5]) + for x in range(LC709203F_I2C_RETRY_COUNT): + try: + with self.i2c_device as i2c: + i2c.write(self._buf[1:5]) + return + except OSError as exception: + # print("OSError in write: ", x+1, "/10: ", exception) + if x == (LC709203F_I2C_RETRY_COUNT - 1): + # Retry count reached + # print("Retry count reached in write: ", exception) + raise exception