diff --git a/adafruit_lc709203f.py b/adafruit_lc709203f.py index efdc1d6..d8fb2e0 100644 --- a/adafruit_lc709203f.py +++ b/adafruit_lc709203f.py @@ -32,6 +32,8 @@ """ +import time + from micropython import const from adafruit_bus_device import i2c_device @@ -124,12 +126,25 @@ class LC709203F: """ def __init__(self, i2c_bus: I2C, address: int = LC709203F_I2CADDR_DEFAULT) -> None: - self.i2c_device = i2c_device.I2CDevice(i2c_bus, address) + value_exc = None + for _ in range(3): + try: + self.i2c_device = i2c_device.I2CDevice(i2c_bus, address) + break + except ValueError as exc: + value_exc = exc + # Wait a bit for the sensor to wake up. + time.sleep(0.1) + else: + raise value_exc + self._buf = bytearray(10) self.power_mode = PowerMode.OPERATE # pylint: disable=no-member self.pack_size = PackSize.MAH500 # pylint: disable=no-member - self.battery_profile = 1 + self.battery_profile = 1 # 4.2V profile + time.sleep(0.1) self.init_RSOC() + time.sleep(0.1) def init_RSOC(self) -> None: # pylint: disable=invalid-name """Initialize the state of charge calculator""" @@ -154,7 +169,7 @@ def cell_temperature(self) -> float: def cell_temperature(self, value: float) -> None: """Sets the temperature in the LC709203F""" if self.thermistor_enable: - raise AttributeError("temperature can only be set in i2c mode") + raise ValueError("temperature can only be set in i2c mode") self._write_word(LC709203F_CMD_CELLTEMPERATURE, int(value + 273.15) * 10) @property @@ -170,7 +185,7 @@ def power_mode(self) -> Literal[1, 2]: @power_mode.setter def power_mode(self, mode: Literal[1, 2]) -> None: if not PowerMode.is_valid(mode): - raise AttributeError("power_mode must be a PowerMode") + raise ValueError("power_mode must be a PowerMode") self._write_word(LC709203F_CMD_POWERMODE, mode) @property @@ -181,7 +196,7 @@ def battery_profile(self) -> Literal[0, 1]: @battery_profile.setter def battery_profile(self, mode: Literal[0, 1]) -> None: if not mode in (0, 1): - raise AttributeError("battery_profile must be 0 or 1") + raise ValueError("battery_profile must be 0 or 1") self._write_word(LC709203F_CMD_BATTPROF, mode) @property @@ -192,7 +207,7 @@ def pack_size(self) -> int: @pack_size.setter def pack_size(self, size: int) -> None: if not PackSize.is_valid(size): - raise AttributeError("pack_size must be a PackSize") + raise ValueError("pack_size must be a PackSize") self._write_word(LC709203F_CMD_APA, size) @property @@ -214,7 +229,7 @@ def thermistor_enable(self) -> bool: def thermistor_enable(self, status: bool) -> None: """Sets the temperature source to Tsense""" if not isinstance(status, bool): - raise AttributeError("thermistor_enable must be True or False") + raise ValueError("thermistor_enable must be True or False") self._write_word(LC709203F_CMD_STATUSBIT, status) # pylint: disable=no-self-use @@ -243,7 +258,7 @@ def _read_word(self, command: int) -> int: ) crc8 = self._generate_crc(self._buf[0:5]) if crc8 != self._buf[5]: - raise RuntimeError("CRC failure on reading word") + raise OSError("CRC failure on reading word") return (self._buf[4] << 8) | self._buf[3] def _write_word(self, command: int, data: int) -> None: diff --git a/examples/lc709203f_simpletest.py b/examples/lc709203f_simpletest.py index 09a0b53..85c4c0f 100644 --- a/examples/lc709203f_simpletest.py +++ b/examples/lc709203f_simpletest.py @@ -13,7 +13,12 @@ print("IC version:", hex(sensor.ic_version)) while True: - print( - "Battery: %0.3f Volts / %0.1f %%" % (sensor.cell_voltage, sensor.cell_percent) - ) + try: + print( + "Battery: %0.3f Volts / %0.1f %%" + % (sensor.cell_voltage, sensor.cell_percent) + ) + except OSError: + print("retry reads") + time.sleep(1)