Skip to content

add retries and delays to work around ESP32-S3 problems #21

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

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
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
31 changes: 23 additions & 8 deletions adafruit_lc709203f.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

"""

import time

from micropython import const
from adafruit_bus_device import i2c_device

Expand Down Expand Up @@ -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"""
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 8 additions & 3 deletions examples/lc709203f_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)