32
32
33
33
"""
34
34
35
+ import time
36
+
35
37
from micropython import const
36
38
from adafruit_bus_device import i2c_device
37
39
@@ -124,12 +126,25 @@ class LC709203F:
124
126
"""
125
127
126
128
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
+
128
141
self ._buf = bytearray (10 )
129
142
self .power_mode = PowerMode .OPERATE # pylint: disable=no-member
130
143
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 )
132
146
self .init_RSOC ()
147
+ time .sleep (0.1 )
133
148
134
149
def init_RSOC (self ) -> None : # pylint: disable=invalid-name
135
150
"""Initialize the state of charge calculator"""
@@ -154,7 +169,7 @@ def cell_temperature(self) -> float:
154
169
def cell_temperature (self , value : float ) -> None :
155
170
"""Sets the temperature in the LC709203F"""
156
171
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" )
158
173
self ._write_word (LC709203F_CMD_CELLTEMPERATURE , int (value + 273.15 ) * 10 )
159
174
160
175
@property
@@ -170,7 +185,7 @@ def power_mode(self) -> Literal[1, 2]:
170
185
@power_mode .setter
171
186
def power_mode (self , mode : Literal [1 , 2 ]) -> None :
172
187
if not PowerMode .is_valid (mode ):
173
- raise AttributeError ("power_mode must be a PowerMode" )
188
+ raise ValueError ("power_mode must be a PowerMode" )
174
189
self ._write_word (LC709203F_CMD_POWERMODE , mode )
175
190
176
191
@property
@@ -181,7 +196,7 @@ def battery_profile(self) -> Literal[0, 1]:
181
196
@battery_profile .setter
182
197
def battery_profile (self , mode : Literal [0 , 1 ]) -> None :
183
198
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" )
185
200
self ._write_word (LC709203F_CMD_BATTPROF , mode )
186
201
187
202
@property
@@ -192,7 +207,7 @@ def pack_size(self) -> int:
192
207
@pack_size .setter
193
208
def pack_size (self , size : int ) -> None :
194
209
if not PackSize .is_valid (size ):
195
- raise AttributeError ("pack_size must be a PackSize" )
210
+ raise ValueError ("pack_size must be a PackSize" )
196
211
self ._write_word (LC709203F_CMD_APA , size )
197
212
198
213
@property
@@ -214,7 +229,7 @@ def thermistor_enable(self) -> bool:
214
229
def thermistor_enable (self , status : bool ) -> None :
215
230
"""Sets the temperature source to Tsense"""
216
231
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" )
218
233
self ._write_word (LC709203F_CMD_STATUSBIT , status )
219
234
220
235
# pylint: disable=no-self-use
@@ -243,7 +258,7 @@ def _read_word(self, command: int) -> int:
243
258
)
244
259
crc8 = self ._generate_crc (self ._buf [0 :5 ])
245
260
if crc8 != self ._buf [5 ]:
246
- raise RuntimeError ("CRC failure on reading word" )
261
+ raise OSError ("CRC failure on reading word" )
247
262
return (self ._buf [4 ] << 8 ) | self ._buf [3 ]
248
263
249
264
def _write_word (self , command : int , data : int ) -> None :
0 commit comments