|
| 1 | +# Copyright (c) 2014 Adafruit Industries |
| 2 | +# Author: Tony DiCola |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in |
| 12 | +# all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +# THE SOFTWARE. |
| 21 | +import logging |
| 22 | +import math |
| 23 | + |
| 24 | +import Adafruit_GPIO.I2C as I2C |
| 25 | + |
| 26 | +# Default I2C address for device. |
| 27 | +MCP9808_I2CADDR_DEFAULT = 0x18 |
| 28 | + |
| 29 | +# Register addresses. |
| 30 | +MCP9808_REG_CONFIG = 0x01 |
| 31 | +MCP9808_REG_UPPER_TEMP = 0x02 |
| 32 | +MCP9808_REG_LOWER_TEMP = 0x03 |
| 33 | +MCP9808_REG_CRIT_TEMP = 0x04 |
| 34 | +MCP9808_REG_AMBIENT_TEMP = 0x05 |
| 35 | +MCP9808_REG_MANUF_ID = 0x06 |
| 36 | +MCP9808_REG_DEVICE_ID = 0x07 |
| 37 | + |
| 38 | +# Configuration register values. |
| 39 | +MCP9808_REG_CONFIG_SHUTDOWN = 0x0100 |
| 40 | +MCP9808_REG_CONFIG_CRITLOCKED = 0x0080 |
| 41 | +MCP9808_REG_CONFIG_WINLOCKED = 0x0040 |
| 42 | +MCP9808_REG_CONFIG_INTCLR = 0x0020 |
| 43 | +MCP9808_REG_CONFIG_ALERTSTAT = 0x0010 |
| 44 | +MCP9808_REG_CONFIG_ALERTCTRL = 0x0008 |
| 45 | +MCP9808_REG_CONFIG_ALERTSEL = 0x0002 |
| 46 | +MCP9808_REG_CONFIG_ALERTPOL = 0x0002 |
| 47 | +MCP9808_REG_CONFIG_ALERTMODE = 0x0001 |
| 48 | + |
| 49 | + |
| 50 | +class MCP9808(object): |
| 51 | + """Class to represent an Adafruit MCP9808 precision temperature measurement |
| 52 | + board. |
| 53 | + """ |
| 54 | + |
| 55 | + def __init__(self, address=MCP9808_I2CADDR_DEFAULT, busnum=I2C.get_default_bus()): |
| 56 | + """Initialize MCP9808 device on the specified I2C address and bus number. |
| 57 | + Address defaults to 0x18 and bus number defaults to the appropriate bus |
| 58 | + for the hardware. |
| 59 | + """ |
| 60 | + self._logger = logging.getLogger('Adafruit_MCP9808.MCP9808') |
| 61 | + self._device = I2C.Device(address, busnum) |
| 62 | + |
| 63 | + def begin(self): |
| 64 | + """Start taking temperature measurements. Returns True if the device is |
| 65 | + intialized, False otherwise. |
| 66 | + """ |
| 67 | + # Check manufacturer and device ID match expected values. |
| 68 | + mid = self._device.readU16BE(MCP9808_REG_MANUF_ID) |
| 69 | + did = self._device.readU16BE(MCP9808_REG_DEVICE_ID) |
| 70 | + self._logger.debug('Read manufacturer ID: {0:04X}'.format(mid)) |
| 71 | + self._logger.debug('Read device ID: {0:04X}'.format(did)) |
| 72 | + return mid == 0x0054 and did == 0x0400 |
| 73 | + |
| 74 | + def readTempC(self): |
| 75 | + """Read sensor and return its value in degrees celsius.""" |
| 76 | + # Read temperature register value. |
| 77 | + t = self._device.readU16BE(MCP9808_REG_AMBIENT_TEMP) |
| 78 | + self._logger.debug('Raw ambient temp register value: 0x{0:04X}'.format(t & 0xFFFF)) |
| 79 | + # Scale and convert to signed value. |
| 80 | + temp = (t & 0x0FFF) / 16.0 |
| 81 | + if t & 0x1000: |
| 82 | + temp -= 256.0 |
| 83 | + return temp |
0 commit comments