/ adafruit_adt7410.py
adafruit_adt7410.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2019 ladyada for Adafruit Industries
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  """
 23  `adafruit_adt7410`
 24  ====================================================
 25  
 26  CircuitPython driver for reading temperature from the Analog Devices ADT7410
 27  precision temperature sensor
 28  
 29  * Author(s): ladyada
 30  
 31  Implementation Notes
 32  --------------------
 33  
 34  **Hardware:**
 35  
 36  **Software and Dependencies:**
 37  
 38  * Adafruit CircuitPython firmware for the supported boards:
 39    https://github.com/adafruit/circuitpython/releases
 40  
 41  * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
 42  
 43  * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
 44  """
 45  
 46  
 47  import time
 48  import struct
 49  from adafruit_bus_device.i2c_device import I2CDevice
 50  from adafruit_register.i2c_bit import RWBit, ROBit
 51  from micropython import const
 52  
 53  __version__ = "0.0.0-auto.0"
 54  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADT7410.git"
 55  
 56  
 57  _ADT7410_TEMPMSB = const(0x0)
 58  _ADT7410_TEMPLSB = const(0x1)
 59  _ADT7410_STATUS = const(0x2)
 60  _ADT7410_CONFIG = const(0x3)
 61  _ADT7410_ID = const(0xB)
 62  _ADT7410_SWRST = const(0x2F)
 63  
 64  
 65  class ADT7410:
 66      """Interface to the Analog Devices ADT7410 temperature sensor."""
 67  
 68      # many modes can be set with register objects for simplicity
 69      ready = ROBit(_ADT7410_STATUS, 7)
 70      ctpin_polarity = RWBit(_ADT7410_CONFIG, 2)
 71      intpin_polarity = RWBit(_ADT7410_CONFIG, 3)
 72      comparator_mode = RWBit(_ADT7410_CONFIG, 4)
 73      high_resolution = RWBit(_ADT7410_CONFIG, 7)
 74  
 75      def __init__(self, i2c_bus, address=0x48):
 76          self.i2c_device = I2CDevice(i2c_bus, address)
 77          self._buf = bytearray(3)
 78          # Verify the manufacturer and device ids to ensure we are talking to
 79          # what we expect.
 80          _id = (self._read_register(_ADT7410_ID)[0]) & 0xF8
 81          if _id != 0xC8:
 82              raise ValueError(
 83                  "Unable to find ADT7410 at i2c address " + str(hex(address))
 84              )
 85          self.reset()
 86  
 87      @property
 88      def temperature(self):
 89          """The temperature in celsius"""
 90          temp = self._read_register(_ADT7410_TEMPMSB, 2)
 91          return struct.unpack(">h", temp)[0] / 128
 92  
 93      @property
 94      def status(self):
 95          """The ADT7410 status registers current value"""
 96          return self._read_register(_ADT7410_STATUS)[0]
 97  
 98      @property
 99      def configuration(self):
100          """The ADT7410 configuration register"""
101          return self._read_register(_ADT7410_CONFIG)[0]
102  
103      @configuration.setter
104      def configuration(self, val):
105          return self._write_register(_ADT7410_CONFIG, val)
106  
107      def reset(self):
108          """Perform a software reset"""
109          self._write_register(_ADT7410_SWRST)
110          time.sleep(0.5)
111  
112      def _read_register(self, addr, num=1):
113          self._buf[0] = addr
114          with self.i2c_device as i2c:
115              i2c.write_then_readinto(
116                  self._buf, self._buf, out_end=1, in_start=1, in_end=num + 1
117              )
118          return self._buf[1 : num + 1]
119  
120      def _write_register(self, addr, data=None):
121          self._buf[0] = addr
122          end = 1
123          if data:
124              self._buf[1] = data
125              end = 2
126          with self.i2c_device as i2c:
127              i2c.write(self._buf, end=end)