Skip to content

Commit 38bd02f

Browse files
authored
Merge pull request #21 from dhalbert/esp32-s3-retries
add retries and delays to work around ESP32-S3 problems
2 parents 1747448 + 1007e4c commit 38bd02f

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

adafruit_lc709203f.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
3333
"""
3434

35+
import time
36+
3537
from micropython import const
3638
from adafruit_bus_device import i2c_device
3739

@@ -124,12 +126,25 @@ class LC709203F:
124126
"""
125127

126128
def __init__(self, i2c_bus: I2C, address: int = LC709203F_I2CADDR_DEFAULT) -> None:
127-
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
129+
value_exc = None
130+
for _ in range(3):
131+
try:
132+
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
133+
break
134+
except ValueError as exc:
135+
value_exc = exc
136+
# Wait a bit for the sensor to wake up.
137+
time.sleep(0.1)
138+
else:
139+
raise value_exc
140+
128141
self._buf = bytearray(10)
129142
self.power_mode = PowerMode.OPERATE # pylint: disable=no-member
130143
self.pack_size = PackSize.MAH500 # pylint: disable=no-member
131-
self.battery_profile = 1
144+
self.battery_profile = 1 # 4.2V profile
145+
time.sleep(0.1)
132146
self.init_RSOC()
147+
time.sleep(0.1)
133148

134149
def init_RSOC(self) -> None: # pylint: disable=invalid-name
135150
"""Initialize the state of charge calculator"""
@@ -154,7 +169,7 @@ def cell_temperature(self) -> float:
154169
def cell_temperature(self, value: float) -> None:
155170
"""Sets the temperature in the LC709203F"""
156171
if self.thermistor_enable:
157-
raise AttributeError("temperature can only be set in i2c mode")
172+
raise ValueError("temperature can only be set in i2c mode")
158173
self._write_word(LC709203F_CMD_CELLTEMPERATURE, int(value + 273.15) * 10)
159174

160175
@property
@@ -170,7 +185,7 @@ def power_mode(self) -> Literal[1, 2]:
170185
@power_mode.setter
171186
def power_mode(self, mode: Literal[1, 2]) -> None:
172187
if not PowerMode.is_valid(mode):
173-
raise AttributeError("power_mode must be a PowerMode")
188+
raise ValueError("power_mode must be a PowerMode")
174189
self._write_word(LC709203F_CMD_POWERMODE, mode)
175190

176191
@property
@@ -181,7 +196,7 @@ def battery_profile(self) -> Literal[0, 1]:
181196
@battery_profile.setter
182197
def battery_profile(self, mode: Literal[0, 1]) -> None:
183198
if not mode in (0, 1):
184-
raise AttributeError("battery_profile must be 0 or 1")
199+
raise ValueError("battery_profile must be 0 or 1")
185200
self._write_word(LC709203F_CMD_BATTPROF, mode)
186201

187202
@property
@@ -192,7 +207,7 @@ def pack_size(self) -> int:
192207
@pack_size.setter
193208
def pack_size(self, size: int) -> None:
194209
if not PackSize.is_valid(size):
195-
raise AttributeError("pack_size must be a PackSize")
210+
raise ValueError("pack_size must be a PackSize")
196211
self._write_word(LC709203F_CMD_APA, size)
197212

198213
@property
@@ -214,7 +229,7 @@ def thermistor_enable(self) -> bool:
214229
def thermistor_enable(self, status: bool) -> None:
215230
"""Sets the temperature source to Tsense"""
216231
if not isinstance(status, bool):
217-
raise AttributeError("thermistor_enable must be True or False")
232+
raise ValueError("thermistor_enable must be True or False")
218233
self._write_word(LC709203F_CMD_STATUSBIT, status)
219234

220235
# pylint: disable=no-self-use
@@ -243,7 +258,7 @@ def _read_word(self, command: int) -> int:
243258
)
244259
crc8 = self._generate_crc(self._buf[0:5])
245260
if crc8 != self._buf[5]:
246-
raise RuntimeError("CRC failure on reading word")
261+
raise OSError("CRC failure on reading word")
247262
return (self._buf[4] << 8) | self._buf[3]
248263

249264
def _write_word(self, command: int, data: int) -> None:

examples/lc709203f_simpletest.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515

1616
print("IC version:", hex(sensor.ic_version))
1717
while True:
18-
print(
19-
"Battery: %0.3f Volts / %0.1f %%" % (sensor.cell_voltage, sensor.cell_percent)
20-
)
18+
try:
19+
print(
20+
"Battery: %0.3f Volts / %0.1f %%"
21+
% (sensor.cell_voltage, sensor.cell_percent)
22+
)
23+
except OSError:
24+
print("retry reads")
25+
2126
time.sleep(1)

0 commit comments

Comments
 (0)