From 8c2846e83f7fffc5798fddc687dc35e3b868613b Mon Sep 17 00:00:00 2001 From: Chandra Wijaya Sentosa Date: Tue, 22 Feb 2022 06:34:22 +0000 Subject: [PATCH] Add docstring for ADS1x15 class and methods --- ADS1x15/ADS1x15.py | 115 +++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/ADS1x15/ADS1x15.py b/ADS1x15/ADS1x15.py index eec13e7..7114faa 100644 --- a/ADS1x15/ADS1x15.py +++ b/ADS1x15/ADS1x15.py @@ -7,8 +7,8 @@ # ADS1x15 default i2c address I2C_address = 0x48 -# General ADS1x15 family ADC class class ADS1x15: + "General ADS1x15 family ADC class" # ADS1x15 register address CONVERSION_REG = 0x00 @@ -86,25 +86,25 @@ class ADS1x15: # Default conversion lengths _adcBits = 16 - # Constructor with SMBus ID and I2C address input def __init__(self, busId: int, address: int = I2C_address) : + "Constructor with SMBus ID and I2C address input" self.i2c = SMBus(busId) self._address = address # Store initial config resgister to config property self._config = self.readRegister(self.CONFIG_REG) - # Write 16-bit integer to an address pointer register def writeRegister(self, address: int, value) : + "Write 16-bit integer to an address pointer register" registerValue = [(value >> 8) & 0xFF, value & 0xFF] self.i2c.write_i2c_block_data(self._address, address, registerValue) - # Read 16-bit integer value from an address pointer register def readRegister(self, address: int) : + "Read 16-bit integer value from an address pointer register" registerValue = self.i2c.read_i2c_block_data(self._address, address, 2) return (registerValue[0] << 8) + registerValue[1] - # Set input multiplexer configuration def setInput(self, input: int) : + "Set input multiplexer configuration" # Filter input argument if input < 0 or input > 7 : inputRegister = 0x0000 else : inputRegister = input << 12 @@ -112,12 +112,12 @@ def setInput(self, input: int) : self._config = (self._config & 0x8FFF) | inputRegister self.writeRegister(self.CONFIG_REG, self._config) - # Get input multiplexer configuration def getInput(self) : + "Get input multiplexer configuration" return (self._config & 0x7000) >> 12 - # Set programmable gain amplifier configuration def setGain(self, gain: int) : + "Set programmable gain amplifier configuration" # Filter gain argument if gain == self.PGA_4_096V : gainRegister = 0x0200 elif gain == self.PGA_2_048V : gainRegister = 0x0400 @@ -129,8 +129,8 @@ def setGain(self, gain: int) : self._config = (self._config & 0xF1FF) | gainRegister self.writeRegister(self.CONFIG_REG, self._config) - # Get programmable gain amplifier configuration def getGain(self) : + "Get programmable gain amplifier configuration" gainRegister = self._config & 0x0E00 if gainRegister == 0x0200 : return self.PGA_4_096V elif gainRegister == 0x0400 : return self.PGA_2_048V @@ -139,8 +139,8 @@ def getGain(self) : elif gainRegister == 0x0A00 : return self.PGA_0_256V else : return 0x0000 - # Set device operating mode configuration def setMode(self, mode: int) : + "Set device operating mode configuration" # Filter mode argument if mode == 0 : modeRegister = 0x0000 else : modeRegister = 0x0100 @@ -148,12 +148,12 @@ def setMode(self, mode: int) : self._config = (self._config & 0xFEFF) | modeRegister self.writeRegister(self.CONFIG_REG, self._config) - # Get device operating mode configuration def getMode(self) : + "Get device operating mode configuration" return (self._config & 0x0100) >> 8 - # Set data rate configuration def setDataRate(self, dataRate: int) : + "Set data rate configuration" # Filter dataRate argument if dataRate < 0 or dataRate > 7 : dataRateRegister = 0x0080 else : dataRateRegister = dataRate << 5 @@ -161,12 +161,12 @@ def setDataRate(self, dataRate: int) : self._config = (self._config & 0xFF1F) | dataRateRegister self.writeRegister(self.CONFIG_REG, self._config) - # Get data rate configuration def getDataRate(self) : + "Get data rate configuration" return (self._config & 0x00E0) >> 5 - # Set comparator mode configuration def setComparatorMode(self, comparatorMode: int) : + "Set comparator mode configuration" # Filter comparatorMode argument if comparatorMode == 1 : comparatorModeRegister = 0x0010 else : comparatorModeRegister = 0x0000 @@ -174,12 +174,12 @@ def setComparatorMode(self, comparatorMode: int) : self._config = (self._config & 0xFFEF) | comparatorModeRegister self.writeRegister(self.CONFIG_REG, self._config) - # Get comparator mode configuration def getComparatorMode(self) : + "Get comparator mode configuration" return (self._config & 0x0010) >> 4 - # Set comparator polarity configuration def setComparatorPolarity(self, comparatorPolarity: int) : + "Set comparator polarity configuration" # Filter comparatorPolarity argument if comparatorPolarity == 1 : comparatorPolarityRegister = 0x0008 else : comparatorPolarityRegister = 0x0000 @@ -187,12 +187,12 @@ def setComparatorPolarity(self, comparatorPolarity: int) : self._config = (self._config & 0xFFF7) | comparatorPolarityRegister self.writeRegister(self.CONFIG_REG, self._config) - # Get comparator polarity configuration def getComparatorPolarity(self) : + "Get comparator polarity configuration" return (self._config & 0x0008) >> 3 - # Set comparator polarity configuration def setComparatorLatch(self, comparatorLatch: int) : + "Set comparator polarity configuration" # Filter comparatorLatch argument if comparatorLatch == 1 : comparatorLatchRegister = 0x0004 else : comparatorLatchRegister = 0x0000 @@ -200,12 +200,12 @@ def setComparatorLatch(self, comparatorLatch: int) : self._config = (self._config & 0xFFFB) | comparatorLatchRegister self.writeRegister(self.CONFIG_REG, self._config) - # Get comparator polarity configuration def getComparatorLatch(self) : + "Get comparator polarity configuration" return (self._config & 0x0004) >> 2 - # Set comparator queue configuration def setComparatorQueue(self, comparatorQueue: int) : + "Set comparator queue configuration" # Filter comparatorQueue argument if comparatorQueue < 0 or comparatorQueue > 3 : comparatorQueueRegister = 0x0002 else : comparatorQueueRegister = comparatorQueue @@ -213,47 +213,48 @@ def setComparatorQueue(self, comparatorQueue: int) : self._config = (self._config & 0xFFFC) | comparatorQueueRegister self.writeRegister(self.CONFIG_REG, self._config) - # Get comparator queue configuration def getComparatorQueue(self) : + "Get comparator queue configuration" return (self._config & 0x0003) - # Set low threshold for voltage comparator def setComparatorThresholdLow(self, threshold: float) : + "Set low threshold for voltage comparator" self.writeRegister(self.LO_THRESH_REG, round(threshold)) - # Get voltage comparator low threshold def getComparatorThresholdLow(self) : + "Get voltage comparator low threshold" threshold = self.readRegister(self.LO_THRESH_REG) if threshold >= 32768 : threshold = threshold - 65536 return threshold - # Set high threshold for voltage comparator def setComparatorThresholdHigh(self, threshold: float) : + "Set high threshold for voltage comparator" self.writeRegister(self.HI_THRESH_REG, round(threshold)) - # Get voltage comparator high threshold def getComparatorThresholdHigh(self) : + "Get voltage comparator high threshold" threshold = self.readRegister(self.HI_THRESH_REG) if threshold >= 32768 : threshold = threshold - 65536 return threshold - # Check if device currently not performing conversion def isReady(self) : + "Check if device currently not performing conversion" value = self.readRegister(self.CONFIG_REG) return bool(value & 0x8000) def isBusy(self) : + "Check if device currently performing conversion" return not self.isReady() - # Private method for starting a single-shot conversion def _requestADC(self, input) : + "Private method for starting a single-shot conversion" self.setInput(input) # Set single-shot conversion start (bit 15) if self._config & 0x0100 : self.writeRegister(self.CONFIG_REG, self._config | 0x8000) - # Get ADC value with current configuration def _getADC(self) -> int : + "Get ADC value with current configuration" t = time.time() isContinuos = not (self._config & 0x0100) # Wait conversion process finish or reach conversion time for continuous mode @@ -262,31 +263,31 @@ def _getADC(self) -> int : break return self.getValue() - # Get ADC value def getValue(self) -> int : + "Get ADC value" value = self.readRegister(self.CONVERSION_REG) # Shift bit based on ADC bits and change 2'complement negative value to negative integer value = value >> (16 - self._adcBits) if value >= (2 ** (self._adcBits - 1)) : value = value - (2 ** (self._adcBits)) return value - # Request single-shot conversion of a pin to ground def requestADC(self, pin: int) : + "Request single-shot conversion of a pin to ground" if (pin >= self._maxPorts or pin < 0) : return self._requestADC(pin + 4) - # Get ADC value of a pin def readADC(self, pin: int) : + "Get ADC value of a pin" if (pin >= self._maxPorts or pin < 0) : return 0 self.requestADC(pin) return self._getADC() - # Request single-shot conversion between pin 0 and pin 1 def requestADC_Differential_0_1(self) : + "Request single-shot conversion between pin 0 and pin 1" self._requestADC(0) - # Get ADC value between pin 0 and pin 1 def readADC_Differential_0_1(self) : + "Get ADC value between pin 0 and pin 1" self.requestADC_Differential_0_1() return self._getADC() @@ -299,16 +300,16 @@ def getMaxVoltage(self) -> float : elif self._config & 0x0E00 == 0x0800 : return 0.512 else : return 0.256 - # Transform an ADC value to nominal voltage def toVoltage(self, value: int = 1) -> float : + "Transform an ADC value to nominal voltage" volts = self.getMaxVoltage() * value return volts / ((2 ** (self._adcBits - 1)) - 1) -# ADS1013 class derifed from general ADS1x15 class class ADS1013(ADS1x15) : + "ADS1013 class derifed from general ADS1x15 class" - # Constructor with SMBus ID and I2C address input and configuration for ADS1013 def __init__(self, busId: int, address: int = I2C_address) : + "Constructor with SMBus ID and I2C address input and configuration for ADS1013" self.i2c = SMBus(busId) self._address = address self._conversionDelay = 2 @@ -317,11 +318,11 @@ def __init__(self, busId: int, address: int = I2C_address) : # Store initial config resgister to config property self._config = self.readRegister(self.CONFIG_REG) -# ADS1014 class derifed from general ADS1x15 class class ADS1014(ADS1x15) : + "ADS1014 class derifed from general ADS1x15 class" - # Constructor with SMBus ID and I2C address input and configuration for ADS1014 def __init__(self, busId: int, address: int = I2C_address) : + "Constructor with SMBus ID and I2C address input and configuration for ADS1014" self.i2c = SMBus(busId) self._address = address self._conversionDelay = 2 @@ -330,11 +331,11 @@ def __init__(self, busId: int, address: int = I2C_address) : # Store initial config resgister to config property self._config = self.readRegister(self.CONFIG_REG) -# ADS1015 class derifed from general ADS1x15 class class ADS1015(ADS1x15) : + "ADS1015 class derifed from general ADS1x15 class" - # Constructor with SMBus ID and I2C address input and configuration for ADS1015 def __init__(self, busId: int, address: int = I2C_address) : + "Constructor with SMBus ID and I2C address input and configuration for ADS1015" self.i2c = SMBus(busId) self._address = address self._conversionDelay = 2 @@ -343,38 +344,38 @@ def __init__(self, busId: int, address: int = I2C_address) : # Store initial config resgister to config property self._config = self.readRegister(self.CONFIG_REG) - # Request single-shot conversion between pin 0 and pin 3 def requestADC_Differential_0_3(self) : + "Request single-shot conversion between pin 0 and pin 3" self._requestADC(1) - # Get ADC value between pin 0 and pin 3 def readADC_Differential_0_3(self) : + "Get ADC value between pin 0 and pin 3" self.requestADC_Differential_0_3() return self._getADC() - # Request single-shot conversion between pin 1 and pin 3 def requestADC_Differential_1_3(self) : + "Request single-shot conversion between pin 1 and pin 3" self._requestADC(2) - # Get ADC value between pin 1 and pin 3 def readADC_Differential_1_3(self) : + "Get ADC value between pin 1 and pin 3" self.requestADC_Differential_1_3() return self._getADC() - # Request single-shot conversion between pin 2 and pin 3 def requestADC_Differential_2_3(self) : + "Request single-shot conversion between pin 2 and pin 3" self._requestADC(3) - # Get ADC value between pin 2 and pin 3 def readADC_Differential_2_3(self) : + "Get ADC value between pin 2 and pin 3" self.requestADC_Differential_2_3() return self._getADC() -# ADS1113 class derifed from general ADS1x15 class class ADS1113(ADS1x15) : + "ADS1113 class derifed from general ADS1x15 class" - # Constructor with SMBus ID and I2C address input and configuration for ADS1113 def __init__(self, busId: int, address: int = I2C_address) : + "Constructor with SMBus ID and I2C address input and configuration for ADS1113" self.i2c = SMBus(busId) self._address = address self._conversionDelay = 8 @@ -383,11 +384,11 @@ def __init__(self, busId: int, address: int = I2C_address) : # Store initial config resgister to config property self._config = self.readRegister(self.CONFIG_REG) -# ADS1114 class derifed from general ADS1x15 class class ADS1114(ADS1x15) : + "ADS1114 class derifed from general ADS1x15 class" - # Constructor with SMBus ID and I2C address input and configuration for ADS1114 def __init__(self, busId: int, address: int = I2C_address) : + "Constructor with SMBus ID and I2C address input and configuration for ADS1114" self.i2c = SMBus(busId) self._address = address self._conversionDelay = 8 @@ -396,11 +397,11 @@ def __init__(self, busId: int, address: int = I2C_address) : # Store initial config resgister to config property self._config = self.readRegister(self.CONFIG_REG) -# ADS1115 class derifed from general ADS1x15 class class ADS1115(ADS1x15) : + "ADS1115 class derifed from general ADS1x15 class" - # Constructor with SMBus ID and I2C address input and configuration for ADS1115 def __init__(self, busId: int, address: int = I2C_address) : + "Constructor with SMBus ID and I2C address input and configuration for ADS1115" self.i2c = SMBus(busId) self._address = address self._conversionDelay = 8 @@ -409,29 +410,29 @@ def __init__(self, busId: int, address: int = I2C_address) : # Store initial config resgister to config property self._config = self.readRegister(self.CONFIG_REG) - # Request single-shot conversion between pin 0 and pin 3 def requestADC_Differential_0_3(self) : + "Request single-shot conversion between pin 0 and pin 3" self._requestADC(1) - # Get ADC value between pin 0 and pin 3 def readADC_Differential_0_3(self) : + "Get ADC value between pin 0 and pin 3" self.requestADC_Differential_0_3() return self._getADC() - # Request single-shot conversion between pin 1 and pin 3 def requestADC_Differential_1_3(self) : + "Request single-shot conversion between pin 1 and pin 3" self._requestADC(2) - # Get ADC value between pin 1 and pin 3 def readADC_Differential_1_3(self) : + "Get ADC value between pin 1 and pin 3" self.requestADC_Differential_1_3() return self._getADC() - # Request single-shot conversion between pin 2 and pin 3 def requestADC_Differential_2_3(self) : + "Request single-shot conversion between pin 2 and pin 3" self._requestADC(3) - # Get ADC value between pin 2 and pin 3 def readADC_Differential_2_3(self) : + "Get ADC value between pin 2 and pin 3" self.requestADC_Differential_2_3() return self._getADC()