@@ -147,16 +147,21 @@ def __init__(
147147 # pylint: disable=too-many-arguments
148148 self ._last_pin_read = None
149149 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 )
150154 self .gain = gain
151155 self .data_rate = self ._data_rate_default () if data_rate is None else data_rate
152156 self .mode = mode
153157 self .comparator_queue_length = comparator_queue_length
154- self .i2c_device = I2CDevice (i2c , address )
155158 self .comparator_low_threshold = comparator_low_threshold
156159 self .comparator_high_threshold = comparator_high_threshold
157160 self .comparator_mode = comparator_mode
158161 self .comparator_polarity = comparator_polarity
159162 self .comparator_latch = comparator_latch
163+ self .initialized = True
164+ self ._write_config ()
160165
161166 @property
162167 def bits (self ) -> int :
@@ -174,6 +179,8 @@ def data_rate(self, rate: int) -> None:
174179 if rate not in possible_rates :
175180 raise ValueError ("Data rate must be one of: {}" .format (possible_rates ))
176181 self ._data_rate = rate
182+ if self .initialized :
183+ self ._write_config ()
177184
178185 @property
179186 def rates (self ) -> List [int ]:
@@ -196,6 +203,8 @@ def gain(self, gain: float) -> None:
196203 if gain not in possible_gains :
197204 raise ValueError ("Gain must be one of: {}" .format (possible_gains ))
198205 self ._gain = gain
206+ if self .initialized :
207+ self ._write_config ()
199208
200209 @property
201210 def gains (self ) -> List [float ]:
@@ -219,6 +228,8 @@ def comparator_queue_length(self, comparator_queue_length: int) -> None:
219228 )
220229 )
221230 self ._comparator_queue_length = comparator_queue_length
231+ if self .initialized :
232+ self ._write_config ()
222233
223234 @property
224235 def comparator_queue_lengths (self ) -> List [int ]:
@@ -275,6 +286,8 @@ def mode(self, mode: int) -> None:
275286 if mode not in (Mode .CONTINUOUS , Mode .SINGLE ):
276287 raise ValueError ("Unsupported mode." )
277288 self ._mode = mode
289+ if self .initialized :
290+ self ._write_config ()
278291
279292 @property
280293 def comparator_mode (self ) -> int :
@@ -286,6 +299,8 @@ def comparator_mode(self, comp_mode: int) -> None:
286299 if comp_mode not in (Comp_Mode .TRADITIONAL , Comp_Mode .WINDOW ):
287300 raise ValueError ("Unsupported mode." )
288301 self ._comparator_mode = comp_mode
302+ if self .initialized :
303+ self ._write_config ()
289304
290305 @property
291306 def comparator_polarity (self ) -> int :
@@ -297,6 +312,8 @@ def comparator_polarity(self, comp_pol: int) -> None:
297312 if comp_pol not in (Comp_Polarity .ACTIVE_LOW , Comp_Polarity .ACTIVE_HIGH ):
298313 raise ValueError ("Unsupported mode." )
299314 self ._comparator_polarity = comp_pol
315+ if self .initialized :
316+ self ._write_config ()
300317
301318 @property
302319 def comparator_latch (self ) -> int :
@@ -308,6 +325,8 @@ def comparator_latch(self, comp_latch: int) -> None:
308325 if comp_latch not in (Comp_Latch .NONLATCHING , Comp_Latch .LATCHING ):
309326 raise ValueError ("Unsupported mode." )
310327 self ._comparator_latch = comp_latch
328+ if self .initialized :
329+ self ._write_config ()
311330
312331 def read (self , pin : Pin ) -> int :
313332 """I2C Interface for ADS1x15-based ADCs reads.
@@ -341,7 +360,7 @@ def _read(self, pin: Pin) -> int:
341360
342361 # Configure ADC every time before a conversion in SINGLE mode
343362 # or changing channels in CONTINUOUS mode
344- self .write_config (pin )
363+ self ._write_config (pin )
345364
346365 # Wait for conversion to complete
347366 # ADS1x1x devices settle within a single conversion cycle
@@ -390,15 +409,21 @@ def _read_register(self, reg: int, fast: bool = False) -> int:
390409 i2c .write_then_readinto (bytearray ([reg ]), self .buf , in_end = 2 )
391410 return self .buf [0 ] << 8 | self .buf [1 ]
392411
393- def write_config (self , pin_config : int ) -> None :
412+ def _write_config (self , pin_config : Optional [ int ] = None ) -> None :
394413 """Write to configuration register of ADC
395414
396415 :param int pin_config: setting for MUX value in config register
397416 """
417+ if pin_config is None :
418+ pin_config = (
419+ self ._read_register (_ADS1X15_POINTER_CONFIG ) & 0x7000
420+ ) >> _ADS1X15_CONFIG_MUX_OFFSET
421+
398422 if self .mode == Mode .SINGLE :
399423 config = _ADS1X15_CONFIG_OS_SINGLE
400424 else :
401425 config = 0
426+
402427 config |= (pin_config & 0x07 ) << _ADS1X15_CONFIG_MUX_OFFSET
403428 config |= _ADS1X15_CONFIG_GAIN [self .gain ]
404429 config |= self .mode
@@ -408,3 +433,35 @@ def write_config(self, pin_config: int) -> None:
408433 config |= self .comparator_latch
409434 config |= _ADS1X15_CONFIG_COMP_QUEUE [self .comparator_queue_length ]
410435 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