@@ -147,16 +147,21 @@ def __init__(
147
147
# pylint: disable=too-many-arguments
148
148
self ._last_pin_read = None
149
149
self .buf = bytearray (3 )
150
+ self .initialized = (
151
+ False # Prevents writing to ADC until all values are initialized
152
+ )
153
+ self .i2c_device = I2CDevice (i2c , address )
150
154
self .gain = gain
151
155
self .data_rate = self ._data_rate_default () if data_rate is None else data_rate
152
156
self .mode = mode
153
157
self .comparator_queue_length = comparator_queue_length
154
- self .i2c_device = I2CDevice (i2c , address )
155
158
self .comparator_low_threshold = comparator_low_threshold
156
159
self .comparator_high_threshold = comparator_high_threshold
157
160
self .comparator_mode = comparator_mode
158
161
self .comparator_polarity = comparator_polarity
159
162
self .comparator_latch = comparator_latch
163
+ self .initialized = True
164
+ self ._write_config ()
160
165
161
166
@property
162
167
def bits (self ) -> int :
@@ -174,6 +179,8 @@ def data_rate(self, rate: int) -> None:
174
179
if rate not in possible_rates :
175
180
raise ValueError ("Data rate must be one of: {}" .format (possible_rates ))
176
181
self ._data_rate = rate
182
+ if self .initialized :
183
+ self ._write_config ()
177
184
178
185
@property
179
186
def rates (self ) -> List [int ]:
@@ -196,6 +203,8 @@ def gain(self, gain: float) -> None:
196
203
if gain not in possible_gains :
197
204
raise ValueError ("Gain must be one of: {}" .format (possible_gains ))
198
205
self ._gain = gain
206
+ if self .initialized :
207
+ self ._write_config ()
199
208
200
209
@property
201
210
def gains (self ) -> List [float ]:
@@ -219,6 +228,8 @@ def comparator_queue_length(self, comparator_queue_length: int) -> None:
219
228
)
220
229
)
221
230
self ._comparator_queue_length = comparator_queue_length
231
+ if self .initialized :
232
+ self ._write_config ()
222
233
223
234
@property
224
235
def comparator_queue_lengths (self ) -> List [int ]:
@@ -275,6 +286,8 @@ def mode(self, mode: int) -> None:
275
286
if mode not in (Mode .CONTINUOUS , Mode .SINGLE ):
276
287
raise ValueError ("Unsupported mode." )
277
288
self ._mode = mode
289
+ if self .initialized :
290
+ self ._write_config ()
278
291
279
292
@property
280
293
def comparator_mode (self ) -> int :
@@ -286,6 +299,8 @@ def comparator_mode(self, comp_mode: int) -> None:
286
299
if comp_mode not in (Comp_Mode .TRADITIONAL , Comp_Mode .WINDOW ):
287
300
raise ValueError ("Unsupported mode." )
288
301
self ._comparator_mode = comp_mode
302
+ if self .initialized :
303
+ self ._write_config ()
289
304
290
305
@property
291
306
def comparator_polarity (self ) -> int :
@@ -297,6 +312,8 @@ def comparator_polarity(self, comp_pol: int) -> None:
297
312
if comp_pol not in (Comp_Polarity .ACTIVE_LOW , Comp_Polarity .ACTIVE_HIGH ):
298
313
raise ValueError ("Unsupported mode." )
299
314
self ._comparator_polarity = comp_pol
315
+ if self .initialized :
316
+ self ._write_config ()
300
317
301
318
@property
302
319
def comparator_latch (self ) -> int :
@@ -308,6 +325,8 @@ def comparator_latch(self, comp_latch: int) -> None:
308
325
if comp_latch not in (Comp_Latch .NONLATCHING , Comp_Latch .LATCHING ):
309
326
raise ValueError ("Unsupported mode." )
310
327
self ._comparator_latch = comp_latch
328
+ if self .initialized :
329
+ self ._write_config ()
311
330
312
331
def read (self , pin : Pin ) -> int :
313
332
"""I2C Interface for ADS1x15-based ADCs reads.
@@ -341,7 +360,7 @@ def _read(self, pin: Pin) -> int:
341
360
342
361
# Configure ADC every time before a conversion in SINGLE mode
343
362
# or changing channels in CONTINUOUS mode
344
- self .write_config (pin )
363
+ self ._write_config (pin )
345
364
346
365
# Wait for conversion to complete
347
366
# ADS1x1x devices settle within a single conversion cycle
@@ -390,15 +409,21 @@ def _read_register(self, reg: int, fast: bool = False) -> int:
390
409
i2c .write_then_readinto (bytearray ([reg ]), self .buf , in_end = 2 )
391
410
return self .buf [0 ] << 8 | self .buf [1 ]
392
411
393
- def write_config (self , pin_config : int ) -> None :
412
+ def _write_config (self , pin_config : Optional [ int ] = None ) -> None :
394
413
"""Write to configuration register of ADC
395
414
396
415
:param int pin_config: setting for MUX value in config register
397
416
"""
417
+ if pin_config is None :
418
+ pin_config = (
419
+ self ._read_register (_ADS1X15_POINTER_CONFIG ) & 0x7000
420
+ ) >> _ADS1X15_CONFIG_MUX_OFFSET
421
+
398
422
if self .mode == Mode .SINGLE :
399
423
config = _ADS1X15_CONFIG_OS_SINGLE
400
424
else :
401
425
config = 0
426
+
402
427
config |= (pin_config & 0x07 ) << _ADS1X15_CONFIG_MUX_OFFSET
403
428
config |= _ADS1X15_CONFIG_GAIN [self .gain ]
404
429
config |= self .mode
@@ -408,3 +433,35 @@ def write_config(self, pin_config: int) -> None:
408
433
config |= self .comparator_latch
409
434
config |= _ADS1X15_CONFIG_COMP_QUEUE [self .comparator_queue_length ]
410
435
self ._write_register (_ADS1X15_POINTER_CONFIG , config )
436
+
437
+ def _read_config (self ) -> None :
438
+ """Reads Config Register and sets all properties accordingly"""
439
+ config_value = self ._read_register (_ADS1X15_POINTER_CONFIG )
440
+
441
+ self .gain = next (
442
+ key
443
+ for key , value in _ADS1X15_CONFIG_GAIN .items ()
444
+ if value == (config_value & 0x0E00 )
445
+ )
446
+ self .data_rate = next (
447
+ key
448
+ for key , value in self .rate_config .items ()
449
+ if value == (config_value & 0x00E0 )
450
+ )
451
+ self .comparator_queue_length = next (
452
+ key
453
+ for key , value in _ADS1X15_CONFIG_COMP_QUEUE .items ()
454
+ if value == (config_value & 0x0003 )
455
+ )
456
+ self .mode = Mode .SINGLE if config_value & 0x0100 else Mode .CONTINUOUS
457
+ self .comparator_mode = (
458
+ Comp_Mode .WINDOW if config_value & 0x0010 else Comp_Mode .TRADITIONAL
459
+ )
460
+ self .comparator_polarity = (
461
+ Comp_Polarity .ACTIVE_HIGH
462
+ if config_value & 0x0008
463
+ else Comp_Polarity .ACTIVE_LOW
464
+ )
465
+ self .comparator_latch = (
466
+ Comp_Latch .LATCHING if config_value & 0x0004 else Comp_Latch .NONLATCHING
467
+ )
0 commit comments