forked from garrettheath4/Adafruit_CircuitPython_TC74
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadafruit_tc74.py
103 lines (72 loc) · 3.34 KB
/
adafruit_tc74.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# SPDX-FileCopyrightText: 2019 Bryan Siepert for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_tc74`
================================================================================
CircuitPython library for the Microchip TC74 Digital Temperature Sensor
* Author(s): Bryan Siepert, Garrett Koller
Implementation Notes
--------------------
**Hardware:**
* Adafruit Breadboard Friendly I2C Temperature Sensor TC74: https://www.adafruit.com/product/4375
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
"""
from adafruit_register.i2c_struct import ROUnaryStruct
from adafruit_register.i2c_bit import RWBit, ROBit
import adafruit_bus_device.i2c_device as i2cdevice
try:
import typing # pylint: disable=unused-import
from busio import I2C
except ImportError:
pass
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TC74.git"
# pylint: disable=too-few-public-methods
TC74_DEFAULT_ADDRESS = 0x48
TC74_REGISTER_TEMP = 0 # Temperature register (read-only)
TC74_REGISTER_CONFIG = 1 # Configuration register
TC74_SHUTDOWN_BIT = 7 # Shutdown bit in Configuration register
TC74_DATA_READY_BIT = 6 # Data Ready bit in Configuration register
# pylint: enable=too-few-public-methods
class TC74:
"""
Driver for the Microchip TC74 Digital Temperature Sensor.
:param ~busio.I2C i2c_bus: The I2C bus the TC74 is connected to
:param int address: The I2C device address for the sensor. Default is :const:`0x48`
**Quickstart: Importing and using the TC74**
Here is an example of using the :class:`TC74` class.
First you will need to import the libraries to use the sensor
.. code-block:: python
import board
import adafruit_tc74
Once this is done you can define your `board.I2C` object and define your sensor object
.. code-block:: python
i2c = board.I2C() # uses board.SCL and board.SDA
tc = adafruit_tc74.TC74(i2c)
Now you have access to the temperature using :attr:`temperature`.
.. code-block:: python
temperature = tc.temperature
"""
def __init__(self, i2c_bus: I2C, address: int = TC74_DEFAULT_ADDRESS) -> None:
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address)
_temperature = ROUnaryStruct(TC74_REGISTER_TEMP, "b")
shutdown = RWBit(TC74_REGISTER_CONFIG, TC74_SHUTDOWN_BIT, lsb_first=True)
"""Set to True to turn off the temperature measurement circuitry in
the sensor. While shut down the configurations properties can still
be read or written but the temperature will not be measured."""
data_ready = ROBit(TC74_REGISTER_CONFIG, TC74_DATA_READY_BIT, lsb_first=True)
"""Read-only bit that indicates when the temperature register is
ready to be read from, especially after power-on or when switching
from the shutdown to the normal state."""
@property
def temperature(self) -> int:
"""
Returns the current temperature in degrees Celsius. Resolution
is 1 degrees Celsius.
"""
return self._temperature