Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error checking and retries to the read and write functions #27

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions adafruit_lc709203f.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
LC709203F_CMD_ALARMPERCENT = const(0x13)
LC709203F_CMD_ALARMVOLTAGE = const(0x14)

LC709203F_I2C_RETRY_COUNT = 10


class CV:
"""struct helper"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Loading