Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
mcptmp.py
Go to the documentation of this file.
1 # Copyright 2016 Andreas Riegg - t-h-i-n-x.net
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # Changelog
16 #
17 # 1.0 2016-02-07 Initial release.
18 #
19 # Config parameters
20 #
21 # - slave 8 bit Value of the I2C slave address for the I2C
22 # versions of the chips. Defaults to 0x18.
23 #
24 # - resolution Integer Value of the resolution bits of the chip.
25 # Valid values are 9 ... 12, defaults to 12.
26 #
27 #
28 # Implementation remarks
29 #
30 # - Derived from tmpXXX driver.
31 #
32 
33 from webiopi.utils.types import toint, signInteger
34 from webiopi.devices.i2c import I2C
35 from webiopi.devices.sensor import Temperature
36 
38  def __init__(self, slave=0x18, resolution=12):
39  I2C.__init__(self, toint(slave))
40 
41  resolution = toint(resolution)
42  if not resolution in range(9,13):
43  raise ValueError("%dbits resolution out of range [%d..%d]bits" % (resolution, 9, 12))
44  self.resolution = resolution
45  self.writeRegister(0x08, (resolution - 9))
46 
47  def __str__(self):
48  return "MCP9808(slave=0x%02X, resolution=%d-bits)" % (self.slave, self.resolution)
49 
50  def __getKelvin__(self):
51  return self.Celsius2Kelvin()
52 
53  def __getCelsius__(self):
54  d = self.readRegisters(0x05, 2)
55  count = ((d[0] << 8) | d[1]) & 0x1FFF
56  return signInteger(count, 13)*0.0625
57 
58  def __getFahrenheit__(self):
59  return self.Celsius2Fahrenheit()
60