This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadt7410.py
368 lines (295 loc) · 12.8 KB
/
adt7410.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
"""
`adt7410`
================================================================================
CircuitPython Driver for the Analog Devices ADT7410 Temperature Sensor
* Author(s): Jose D. Montoya
"""
import time
from collections import namedtuple
from micropython import const
from adafruit_bus_device import i2c_device
from adafruit_register.i2c_struct import UnaryStruct
from adafruit_register.i2c_bits import RWBits
try:
from busio import I2C
except ImportError:
pass
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/jposada202020/CircuitPython_ADT7410.git"
_REG_WHOAMI = const(0xB)
_TEMP = const(0x00)
_STATUS = const(0x02)
_CONFIGURATION = const(0x03)
_TEMP_HIGH = const(0x04)
_TEMP_LOW = const(0x06)
_TEMP_CRITICAL = const(0x08)
_TEMP_HYSTERESIS = const(0x0A)
CONTINUOUS = const(0b00)
ONE_SHOT = const(0b01)
SPS = const(0b10)
SHUTDOWN = const(0b11)
operation_mode_values = (CONTINUOUS, ONE_SHOT, SPS, SHUTDOWN)
LOW_RESOLUTION = const(0b0)
HIGH_RESOLUTION = const(0b1)
resolution_mode_values = (LOW_RESOLUTION, HIGH_RESOLUTION)
COMP_DISABLED = const(0b0)
COMP_ENABLED = const(0b1)
comparator_mode_values = (COMP_DISABLED, COMP_ENABLED)
AlertStatus = namedtuple("AlertStatus", ["high_alert", "low_alert", "critical_alert"])
class ADT7410:
"""Driver for the ADT7410 Sensor connected over I2C.
:param ~busio.I2C i2c_bus: The I2C bus the ADT7410 is connected to.
:param int address: The I2C device address. Defaults to :const:`0x69`
:raises RuntimeError: if the sensor is not found
**Quickstart: Importing and using the device**
Here is an example of using the :class:`ADT7410` class.
First you will need to import the libraries to use the sensor
.. code-block:: python
import board
import adt7410
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
adt = adt7410.ADT7410(i2c)
Now you have access to the attributes
.. code-block:: python
temp = adt.temperature
"""
_device_id = UnaryStruct(_REG_WHOAMI, "B")
_temperature = UnaryStruct(_TEMP, ">h")
_temperature_high = UnaryStruct(_TEMP_HIGH, ">h")
_temperature_low = UnaryStruct(_TEMP_LOW, ">h")
_temperature_critical = UnaryStruct(_TEMP_CRITICAL, ">h")
_temperature_hysteresis = UnaryStruct(_TEMP_HYSTERESIS, "B")
_status = UnaryStruct(_STATUS, "B")
# Configuration register
_resolution_mode = RWBits(1, _CONFIGURATION, 7)
_operation_mode = RWBits(2, _CONFIGURATION, 5)
_comparator_mode = RWBits(1, _CONFIGURATION, 4)
# Status Register
_critical_alert = RWBits(1, _STATUS, 6)
_high_alert = RWBits(1, _STATUS, 5)
_low_alert = RWBits(1, _STATUS, 4)
def __init__(self, i2c_bus: I2C, address: int = 0x48) -> None:
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
if self._device_id != 0xCB:
raise RuntimeError("Failed to find ADT7410")
@property
def operation_mode(self) -> str:
"""
Sensor operation_mode
Continuous Mode
---------------
In continuous conversion mode, the read operation provides the most recent
converted result.
One Shot Mode
--------------
When one-shot mode is enabled, the ADT7410 immediately
completes a conversion and then goes into shutdown mode. The
one-shot mode is useful when one of the circuit design priorities is
to reduce power consumption.
SPS Mode
----------
In this mode, the part performs one measurement per second.
A conversion takes only 60 ms, and it remains in the idle state
for the remaining 940 ms period
Shutdown Mode
---------------
The ADT7410 can be placed in shutdown mode, the entire IC is
shut down and no further conversions are initiated until the
ADT7410 is taken out of shutdown mode. The conversion result from the
last conversion prior to shutdown can still be read from the
ADT7410 even when it is in shutdown mode. When the part is
taken out of shutdown mode, the internal clock is started and a
conversion is initiated
+--------------------------------+------------------+
| Mode | Value |
+================================+==================+
| :py:const:`adt7410.CONTINUOUS` | :py:const:`0b00` |
+--------------------------------+------------------+
| :py:const:`adt7410.ONE_SHOT` | :py:const:`0b01` |
+--------------------------------+------------------+
| :py:const:`adt7410.SPS` | :py:const:`0b10` |
+--------------------------------+------------------+
| :py:const:`adt7410.SHUTDOWN` | :py:const:`0b11` |
+--------------------------------+------------------+
"""
values = (
"CONTINUOUS",
"ONE_SHOT",
"SPS",
"SHUTDOWN",
)
return values[self._operation_mode]
@operation_mode.setter
def operation_mode(self, value: int) -> None:
if value not in operation_mode_values:
raise ValueError("Value must be a valid operation_mode setting")
self._operation_mode = value
time.sleep(0.24)
@property
def temperature(self) -> float:
"""
Temperature in Celsius
In normal mode, the ADT7410 runs an automatic conversion
sequence. During this automatic conversion sequence, a conversion
takes 240 ms to complete and the ADT7410 is continuously
converting. This means that as soon as one temperature conversion
is completed, another temperature conversion begins.
On power-up, the first conversion is a fast conversion, taking
typically 6 ms. Fast conversion temperature accuracy is typically
within ±5°C.
The measured temperature value is compared with a critical
temperature limit, a high temperature limit, and a low temperature
limit. If the measured value
exceeds these limits, the INT pin is activated; and if it exceeds the
:attr:`critical_temp` limit, the CT pin is activated.
"""
return self._temperature / 128
@property
def resolution_mode(self) -> str:
"""
Sensor resolution_mode
+-------------------------------------+-----------------+
| Mode | Value |
+=====================================+=================+
| :py:const:`adt7410.LOW_RESOLUTION` | :py:const:`0b0` |
+-------------------------------------+-----------------+
| :py:const:`adt7410.HIGH_RESOLUTION` | :py:const:`0b1` |
+-------------------------------------+-----------------+
"""
values = (
"LOW_RESOLUTION",
"HIGH_RESOLUTION",
)
return values[self._resolution_mode]
@resolution_mode.setter
def resolution_mode(self, value: int) -> None:
if value not in resolution_mode_values:
raise ValueError("Value must be a valid resolution_mode setting")
self._resolution_mode = value
@property
def alert_status(self):
"""The current triggered status of the high and low temperature alerts as a AlertStatus
named tuple with attributes for the triggered status of each alert.
.. code-block :: python
import time
import board
import adt7410
i2c = board.I2C() # uses board.SCL and board.SDA
adt = adt7410.ADT7410(i2c)
tmp.low_temperature = 20
tmp.high_temperature = 23
tmp.critical_temperature = 30
print("High limit: {}".format(tmp.high_temperature))
print("Low limit: {}".format(tmp.low_temperature))
print("Critical limit: {}".format(tmp.critical_temperature))
adt.comparator_mode = adt7410.COMP_ENABLED
while True:
print("Temperature: {:.2f}C".format(adt.temperature))
alert_status = tmp.alert_status
if alert_status.high_alert:
print("Temperature above high set limit!")
if alert_status.low_alert:
print("Temperature below low set limit!")
if alert_status.critical_alert:
print("Temperature above critical set limit!")
time.sleep(1)
"""
return AlertStatus(
high_alert=self._high_alert,
low_alert=self._low_alert,
critical_alert=self._critical_alert,
)
@property
def comparator_mode(self) -> str:
"""
Sensor comparator_mode
+-----------------------------------+-----------------+
| Mode | Value |
+===================================+=================+
| :py:const:`adt7410.COMP_DISABLED` | :py:const:`0b0` |
+-----------------------------------+-----------------+
| :py:const:`adt7410.COMP_ENABLED` | :py:const:`0b1` |
+-----------------------------------+-----------------+
"""
values = (
"COMP_DISABLED",
"COMP_ENABLED",
)
return values[self._comparator_mode]
@comparator_mode.setter
def comparator_mode(self, value: int) -> None:
if value not in comparator_mode_values:
raise ValueError("Value must be a valid comparator_mode setting")
self._comparator_mode = value
@property
def high_temperature(self) -> float:
"""
High temperature limit value in Celsius
When the temperature goes above the :attr:`high_temperature`,
and if :attr:`comparator_mode` is selected. The :attr:`alert_status`
high_alert clears to 0 when the status register is read and/or when
the temperature measured goes back below the limit set in the setpoint
:attr:`high_temperature` + :attr:`hysteresis_temperature`
The INT pin is activated if an over temperature event occur
The default setting is 64°C
"""
return self._temperature_high // 128
@high_temperature.setter
def high_temperature(self, value: int) -> None:
if value not in range(-55, 151, 1):
raise ValueError("Temperature should be between -55C and 150C")
self._temperature_high = value * 128
@property
def low_temperature(self) -> float:
"""
Low temperature limit value in Celsius.
When the temperature goes below the :attr:`low_temperature`,
and if :attr:`comparator_mode` is selected. The :attr:`alert_status`
low_alert clears to 0 when the status register is read and/or when
the temperature measured goes back above the limit set in the setpoint
:attr:`low_temperature` + :attr:`hysteresis_temperature`
The INT pin is activated if an under temperature event occur
The default setting is 10°C
"""
return self._temperature_low // 128
@low_temperature.setter
def low_temperature(self, value: int) -> None:
if value not in range(-55, 151, 1):
raise ValueError("Temperature should be between -55°C and 150°C")
self._temperature_low = value * 128
@property
def critical_temperature(self) -> float:
"""
Critical temperature limit value in Celsius
When the temperature goes above the :attr:`critical_temperature`,
and if :attr:`comparator_mode` is selected. The :attr:`alert_status`
critical_alert clears to 0 when the status register is read and/or when
the temperature measured goes back below the limit set in
:attr:`critical_temperature` + :attr:`hysteresis_temperature`
The INT pin is activated if a critical over temperature event occur
The default setting is 147°C
"""
return self._temperature_critical // 128
@critical_temperature.setter
def critical_temperature(self, value: int) -> None:
if value not in range(-55, 151, 1):
raise ValueError("Temperature should be between -55°C and 150°C")
self._temperature_critical = value * 128
@property
def hysteresis_temperature(self) -> float:
"""
Hysteresis temperature limit value in Celsius for the
:attr:`critical_temperature`, :attr:`high_temperature` and
:attr:`low_temperature` limits
"""
return self._temperature_hysteresis
@hysteresis_temperature.setter
def hysteresis_temperature(self, value: int) -> None:
if value not in range(0, 16, 1):
raise ValueError("Temperature should be between 0°C and 15°C")
self._temperature_hysteresis = value