@@ -309,13 +309,12 @@ def comparator_latch(self, comp_latch: int) -> None:
309
309
raise ValueError ("Unsupported mode." )
310
310
self ._comparator_latch = comp_latch
311
311
312
- def read (self , pin : Pin , is_differential : bool = False ) -> int :
312
+ def read (self , pin : Pin ) -> int :
313
313
"""I2C Interface for ADS1x15-based ADCs reads.
314
314
315
315
:param ~microcontroller.Pin pin: individual or differential pin.
316
316
:param bool is_differential: single-ended or differential read.
317
317
"""
318
- pin = pin if is_differential else pin + 0x04
319
318
return self ._read (pin )
320
319
321
320
def _data_rate_default (self ) -> int :
@@ -342,19 +341,7 @@ def _read(self, pin: Pin) -> int:
342
341
343
342
# Configure ADC every time before a conversion in SINGLE mode
344
343
# or changing channels in CONTINUOUS mode
345
- if self .mode == Mode .SINGLE :
346
- config = _ADS1X15_CONFIG_OS_SINGLE
347
- else :
348
- config = 0
349
- config |= (pin & 0x07 ) << _ADS1X15_CONFIG_MUX_OFFSET
350
- config |= _ADS1X15_CONFIG_GAIN [self .gain ]
351
- config |= self .mode
352
- config |= self .rate_config [self .data_rate ]
353
- config |= self .comparator_mode
354
- config |= self .comparator_polarity
355
- config |= self .comparator_latch
356
- config |= _ADS1X15_CONFIG_COMP_QUEUE [self .comparator_queue_length ]
357
- self ._write_register (_ADS1X15_POINTER_CONFIG , config )
344
+ self .write_config (pin )
358
345
359
346
# Wait for conversion to complete
360
347
# ADS1x1x devices settle within a single conversion cycle
@@ -403,34 +390,21 @@ def _read_register(self, reg: int, fast: bool = False) -> int:
403
390
i2c .write_then_readinto (bytearray ([reg ]), self .buf , in_end = 2 )
404
391
return self .buf [0 ] << 8 | self .buf [1 ]
405
392
406
- def read_config (self ) -> None :
407
- """Reads Config Register and sets all properties accordingly"""
408
- config_value = self ._read_register (_ADS1X15_POINTER_CONFIG )
409
-
410
- self .gain = next (
411
- key
412
- for key , value in _ADS1X15_CONFIG_GAIN .items ()
413
- if value == (config_value & 0x0E00 )
414
- )
415
- self .data_rate = next (
416
- key
417
- for key , value in self .rate_config .items ()
418
- if value == (config_value & 0x00E0 )
419
- )
420
- self .comparator_queue_length = next (
421
- key
422
- for key , value in _ADS1X15_CONFIG_COMP_QUEUE .items ()
423
- if value == (config_value & 0x0003 )
424
- )
425
- self .mode = Mode .SINGLE if config_value & 0x0100 else Mode .CONTINUOUS
426
- self .comparator_mode = (
427
- Comp_Mode .WINDOW if config_value & 0x0010 else Comp_Mode .TRADITIONAL
428
- )
429
- self .comparator_polarity = (
430
- Comp_Polarity .ACTIVE_HIGH
431
- if config_value & 0x0008
432
- else Comp_Polarity .ACTIVE_LOW
433
- )
434
- self .comparator_latch = (
435
- Comp_Latch .LATCHING if config_value & 0x0004 else Comp_Latch .NONLATCHING
436
- )
393
+ def write_config (self , pin_config : int ) -> None :
394
+ """Write to configuration register of ADC
395
+
396
+ :param int pin_config: setting for MUX value in config register
397
+ """
398
+ if self .mode == Mode .SINGLE :
399
+ config = _ADS1X15_CONFIG_OS_SINGLE
400
+ else :
401
+ config = 0
402
+ config |= (pin_config & 0x07 ) << _ADS1X15_CONFIG_MUX_OFFSET
403
+ config |= _ADS1X15_CONFIG_GAIN [self .gain ]
404
+ config |= self .mode
405
+ config |= self .rate_config [self .data_rate ]
406
+ config |= self .comparator_mode
407
+ config |= self .comparator_polarity
408
+ config |= self .comparator_latch
409
+ config |= _ADS1X15_CONFIG_COMP_QUEUE [self .comparator_queue_length ]
410
+ self ._write_register (_ADS1X15_POINTER_CONFIG , config )
0 commit comments