Skip to content

Commit

Permalink
Added ability to specify alternate units when getting and setting tem…
Browse files Browse the repository at this point in the history
…peratures
  • Loading branch information
n8many committed Jun 1, 2017
1 parent 656631e commit 81f03dd
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions tmp102.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def tempToBytes(self, temp):
data[1] = ((res & (2**(4 + ext)-1)) << (4 - ext)) | ext
return data

def extractConfig(self, num, location, length):
def extractConfig(self, num, location=0, length=0):

data = self.bus.read_i2c_block_data(self.address, CONFIG_REG, 2)
if (num == 3):
Expand All @@ -73,12 +73,13 @@ def injectConfig(self, setting, num, location, length):
data[num] |= setting
self.bus.write_i2c_block_data(self.address, CONFIG_REG, data)

def readTemperature(self):
def readTemperature(self, units=None):
data = self.bus.read_i2c_block_data(self.address, TEMPERATURE_REG, 2)
tempC = self.bytesToTemp(data)
units = units or self.units

try:
tempOut = tempConvert[self.units](tempC)
tempOut = tempConvert[units](tempC)
except:
raise ValueError('Invalid Units "' + self.units + '"')
return tempOut
Expand Down Expand Up @@ -134,10 +135,11 @@ def setAlertMode(self, mode):
# 1 : Thermostat Mode (Active if over T_High, reset on read)
self.injectConfig(mode, 0, 1, 1)

def setBoundTemp(self, high, temperature):
def setBoundTemp(self, upper, temperature, units=None):
units = units or self.units
ext = self.extractConfig(1, 4, 1)
try:
temperature = tempConvertInv[self.units](temperature)
temperature = tempConvertInv[units](temperature)
except:
raise ValueError('Invalid Units "' + self.units + '"')
if (ext is 1 and temperature > 150):
Expand All @@ -146,23 +148,24 @@ def setBoundTemp(self, high, temperature):
temperature = -55
data = self.tempToBytes(temperature)

if (high):
if (upper):
reg = T_HIGH_REG
else:
reg = T_LOW_REG

self.bus.write_i2c_block_data(self.address, reg, data)

def getBoundTemp(self, high):
if (high):
def getBoundTemp(self, upper, units=None):
units = units or self.units
if (upper):
reg = T_HIGH_REG
else:
reg = T_LOW_REG
data = self.bus.read_i2c_block_data(self.address, reg, 2)
tempC = self.bytesToTemp(data)

try:
tempOut = tempConvert[self.units](tempC)
tempOut = tempConvert[units](tempC)
except:
raise ValueError('Invalid Units "' + self.units + '"')
return tempOut

0 comments on commit 81f03dd

Please sign in to comment.