-
Notifications
You must be signed in to change notification settings - Fork 4
Extended library for TMP116, TMP117, TMP119 #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
846cb09
ec5e94a
d3c580f
fcbadb1
c92229b
c8cb178
90075d1
342e2c5
e5bdacd
9b36367
a411866
9026e48
92fa34b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,7 +17,11 @@ Introduction | |||||
:target: https://github.com/psf/black | ||||||
:alt: Code Style: Black | ||||||
|
||||||
CircuitPython library for the TI TMP117 Temperature sensor | ||||||
CircuitPython library for the TI TMP116, TMP117, TMP119 Temperature sensor | ||||||
|
||||||
It is forked from Adafruit_CircuitPython_TMP117, updated and extended to use it with TMP116 and TMP119 | ||||||
|
||||||
All those sensor have common register map | ||||||
|
||||||
WARNING: Library may not run on some boards with less RAM such as boards using the SAMD21 | ||||||
|
||||||
|
@@ -41,13 +45,13 @@ PyPI <https://pypi.org/project/adafruit-circuitpython-tmp117/>`_. To install for | |||||
|
||||||
.. code-block:: shell | ||||||
|
||||||
pip3 install adafruit-circuitpython-tmp117 | ||||||
pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you mean to do this.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello thanks for review, and your feedback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding readme . i will correct it if you going to merge it. |
||||||
|
||||||
To install system-wide (this may be required in some cases): | ||||||
|
||||||
.. code-block:: shell | ||||||
|
||||||
sudo pip3 install adafruit-circuitpython-tmp117 | ||||||
sudo pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||||||
|
||||||
To install in a virtual environment in your current project: | ||||||
|
||||||
|
@@ -56,7 +60,7 @@ To install in a virtual environment in your current project: | |||||
mkdir project-name && cd project-name | ||||||
python3 -m venv .venv | ||||||
source .venv/bin/activate | ||||||
pip3 install adafruit-circuitpython-tmp117 | ||||||
pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||||||
|
||||||
Usage Example | ||||||
============= | ||||||
|
@@ -65,13 +69,15 @@ Usage Example | |||||
|
||||||
import time | ||||||
import board | ||||||
import adafruit_tmp117 | ||||||
import adafruit_tmp11X | ||||||
from adafruit_tmp11X import TMP117, TMP116, TMP119, AverageCount, MeasurementMode, MeasurementDelay | ||||||
|
||||||
i2c = board.I2C() # uses board.SCL and board.SDA | ||||||
tmp117 = adafruit_tmp117.TMP117(i2c) | ||||||
|
||||||
t1 = TMP116(i2c_bus=i2c, address=0x49) | ||||||
# t2 = TMP117(i2c_bus=i2c, address=0x49) | ||||||
# t2 = TMP119(i2c_bus=i2c, address=0x49) | ||||||
Comment on lines
+76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the I2C addresses all the same? If so don't bother to specify the address here, since the default is fine and makes usage easier. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears the default I2C address is the same. It's just that it can be varied via pin settings. |
||||||
while True: | ||||||
print("Temperature: %.2f degrees C"%tmp117.temperature) | ||||||
print("Temperature:", t1.temperature) | ||||||
time.sleep(1) | ||||||
|
||||||
Documentation | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,11 +1,11 @@ | ||||||||||||||||||
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries | ||||||||||||||||||
Check failure on line 1 in adafruit_tmp11X.py
|
||||||||||||||||||
# | ||||||||||||||||||
# SPDX-License-Identifier: MIT | ||||||||||||||||||
""" | ||||||||||||||||||
`adafruit_tmp117` | ||||||||||||||||||
`adafruit_tmp11X` | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use lower-case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and |
||||||||||||||||||
================================================================================ | ||||||||||||||||||
|
||||||||||||||||||
CircuitPython library for the TI TMP117 Temperature sensor | ||||||||||||||||||
CircuitPython library for the TI TMP116,TMP117,TMP119 Temperature sensor | ||||||||||||||||||
|
||||||||||||||||||
* Author(s): Bryan Siepert, Ian Grant | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -53,7 +53,7 @@ | |||||||||||||||||
pass | ||||||||||||||||||
|
||||||||||||||||||
__version__ = "0.0.0+auto.0" | ||||||||||||||||||
__repo__ = "https:#github.com/adafruit/Adafruit_CircuitPython_TMP117.git" | ||||||||||||||||||
__repo__ = "https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git" | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
_I2C_ADDR = 0x48 # default I2C Address | ||||||||||||||||||
|
@@ -67,7 +67,10 @@ | |||||||||||||||||
_TEMP_OFFSET = const(0x07) | ||||||||||||||||||
_EEPROM3 = const(0x08) | ||||||||||||||||||
_DEVICE_ID = const(0x0F) | ||||||||||||||||||
_DEVICE_ID_VALUE = 0x0117 | ||||||||||||||||||
_TMP117_DEVICE_ID_VALUE = const(0x0117) | ||||||||||||||||||
_TMP116_DEVICE_ID_VALUE = const(0x1116) | ||||||||||||||||||
_TMP119_DEVICE_ID_VALUE = const(0x0117) | ||||||||||||||||||
|
||||||||||||||||||
_TMP117_RESOLUTION = ( | ||||||||||||||||||
0.0078125 # Resolution of the device, found on (page 1 of datasheet) | ||||||||||||||||||
) | ||||||||||||||||||
|
@@ -113,10 +116,12 @@ | |||||||||||||||||
"""Validate that a given value is a member""" | ||||||||||||||||||
return value in cls.string | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class AverageCount(CV): | ||||||||||||||||||
"""Options for `averaged_measurements`""" | ||||||||||||||||||
|
||||||||||||||||||
AVERAGE_1X = None | ||||||||||||||||||
AVERAGE_8X = None | ||||||||||||||||||
AVERAGE_32X = None | ||||||||||||||||||
AVERAGE_64X = None | ||||||||||||||||||
Comment on lines
+121
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These make the library take more RAM, which is a problem on small boards. Don't include these even if it is more convenient for IDE's.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
AverageCount.add_values( | ||||||||||||||||||
( | ||||||||||||||||||
|
@@ -128,9 +133,17 @@ | |||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class MeasurementDelay(CV): | ||||||||||||||||||
"""Options for `measurement_delay`""" | ||||||||||||||||||
|
||||||||||||||||||
DELAY_0_0015_S = None | ||||||||||||||||||
DELAY_0_125_S = None | ||||||||||||||||||
DELAY_0_250_S = None | ||||||||||||||||||
DELAY_0_500_S = None | ||||||||||||||||||
DELAY_1_S = None | ||||||||||||||||||
DELAY_4_S = None | ||||||||||||||||||
DELAY_8_S = None | ||||||||||||||||||
DELAY_16_S = None | ||||||||||||||||||
Comment on lines
+139
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
MeasurementDelay.add_values( | ||||||||||||||||||
( | ||||||||||||||||||
|
@@ -148,16 +161,20 @@ | |||||||||||||||||
|
||||||||||||||||||
class AlertMode(CV): | ||||||||||||||||||
"""Options for `alert_mode`. See `alert_mode` for more information.""" | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
WINDOW = None | ||||||||||||||||||
HYSTERESIS = None | ||||||||||||||||||
Comment on lines
+164
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
AlertMode.add_values( | ||||||||||||||||||
(("WINDOW", 0, "Window", None), ("HYSTERESIS", 1, "Hysteresis", None)) | ||||||||||||||||||
( | ||||||||||||||||||
("WINDOW", 0, "Window", None), | ||||||||||||||||||
("HYSTERESIS", 1, "Hysteresis", None)) | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class MeasurementMode(CV): | ||||||||||||||||||
"""Options for `measurement_mode`. See `measurement_mode` for more information.""" | ||||||||||||||||||
|
||||||||||||||||||
CONTINUOUS = None | ||||||||||||||||||
ONE_SHOT = None | ||||||||||||||||||
SHUTDOWN = None | ||||||||||||||||||
Comment on lines
+175
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
MeasurementMode.add_values( | ||||||||||||||||||
( | ||||||||||||||||||
|
@@ -170,7 +187,6 @@ | |||||||||||||||||
|
||||||||||||||||||
class TMP117: | ||||||||||||||||||
"""Library for the TI TMP117 high-accuracy temperature sensor""" | ||||||||||||||||||
|
||||||||||||||||||
_part_id = ROUnaryStruct(_DEVICE_ID, ">H") | ||||||||||||||||||
_raw_temperature = ROUnaryStruct(_TEMP_RESULT, ">h") | ||||||||||||||||||
_raw_high_limit = UnaryStruct(_T_HIGH_LIMIT, ">h") | ||||||||||||||||||
|
@@ -192,18 +208,22 @@ | |||||||||||||||||
|
||||||||||||||||||
def __init__(self, i2c_bus: I2C, address: int = _I2C_ADDR): | ||||||||||||||||||
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address) | ||||||||||||||||||
if self._part_id != _DEVICE_ID_VALUE: | ||||||||||||||||||
raise AttributeError("Cannot find a TMP117") | ||||||||||||||||||
self._check_dev_id() | ||||||||||||||||||
# currently set when `alert_status` is read, but not exposed | ||||||||||||||||||
self.reset() | ||||||||||||||||||
self.initialize() | ||||||||||||||||||
|
||||||||||||||||||
# thsi methods is unique for each device | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
def _check_dev_id(self): | ||||||||||||||||||
id = _TMP117_DEVICE_ID_VALUE | ||||||||||||||||||
if self._part_id != id: | ||||||||||||||||||
raise AttributeError("Cannot find a TMP117") | ||||||||||||||||||
def reset(self): | ||||||||||||||||||
"""Reset the sensor to its unconfigured power-on state""" | ||||||||||||||||||
self._soft_reset = True | ||||||||||||||||||
|
||||||||||||||||||
def initialize(self): | ||||||||||||||||||
"""Configure the sensor with sensible defaults. `initialize` is primarily provided to be | ||||||||||||||||||
called after `reset`, however it can also be used to easily set the sensor to a known | ||||||||||||||||||
configuration""" | ||||||||||||||||||
# Datasheet specifies that reset will finish in 2ms however by default the first | ||||||||||||||||||
|
@@ -218,6 +238,10 @@ | |||||||||||||||||
|
||||||||||||||||||
return self._read_temperature() | ||||||||||||||||||
|
||||||||||||||||||
@property | ||||||||||||||||||
def temperature_updated(self): | ||||||||||||||||||
return self._wait_for_measurement() | ||||||||||||||||||
|
||||||||||||||||||
@property | ||||||||||||||||||
def temperature_offset(self): | ||||||||||||||||||
"""User defined temperature offset to be added to measurements from `temperature` | ||||||||||||||||||
|
@@ -520,9 +544,11 @@ | |||||||||||||||||
def _set_mode_and_wait_for_measurement(self, mode: int) -> float: | ||||||||||||||||||
self._mode = mode | ||||||||||||||||||
# poll for data ready | ||||||||||||||||||
return self._wait_for_measurement() | ||||||||||||||||||
|
||||||||||||||||||
def _wait_for_measurement(self)-> float: | ||||||||||||||||||
while not self._read_status()[2]: | ||||||||||||||||||
time.sleep(0.001) | ||||||||||||||||||
|
||||||||||||||||||
return self._read_temperature() | ||||||||||||||||||
|
||||||||||||||||||
# eeprom write enable to set defaults for limits and config | ||||||||||||||||||
|
@@ -539,4 +565,17 @@ | |||||||||||||||||
return (high_alert, low_alert, data_ready) | ||||||||||||||||||
|
||||||||||||||||||
def _read_temperature(self) -> float: | ||||||||||||||||||
return self._raw_temperature * _TMP117_RESOLUTION | ||||||||||||||||||
return round(self._raw_temperature * _TMP117_RESOLUTION, 3) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't round here, because the extra digits are still interesting when summing or averaging. Leave it up to the caller to round as needed.
Suggested change
Also this specifically mentions the TMP117 resolution: does the value change? Should it be |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class TMP119(TMP117): | ||||||||||||||||||
def _check_dev_id(self): | ||||||||||||||||||
id = _TMP119_DEVICE_ID_VALUE | ||||||||||||||||||
if self._part_id != id: | ||||||||||||||||||
raise AttributeError("Cannot find a TMP119") | ||||||||||||||||||
|
||||||||||||||||||
class TMP116(TMP117): | ||||||||||||||||||
def _check_dev_id(self): | ||||||||||||||||||
id = _TMP116_DEVICE_ID_VALUE | ||||||||||||||||||
if self._part_id != id: | ||||||||||||||||||
raise AttributeError("Cannot find a TMP116") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# import usb | ||
# import usb.util | ||
# dev = usb.core.find(idVendor=0x0403, idProduct=0x6014) | ||
# print(dev) | ||
import os | ||
os.environ['BLINKA_FT232H'] = "1" | ||
import board | ||
from adafruit_tmp11X import TMP117, TMP116, TMP119, AverageCount, MeasurementMode, MeasurementDelay | ||
import time | ||
import datetime | ||
|
||
deg_sybm = u'\N{DEGREE SIGN}' | ||
|
||
i2c = board.I2C() # uses board.SCL and board.SDA | ||
t1 = TMP116(i2c_bus=i2c, address=0x49) | ||
# t2 = TMP116(i2c_bus=i2c, address=0x47) | ||
# t3 = TMP116(i2c_bus=i2c, address=0x48) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, they do have different I2C addresses. |
||
|
||
|
||
# add to this array multiple sensors. | ||
# address limitation for single device allow to add up to 4 devises | ||
# However you may add different I2C busses to array | ||
sensors = [t1,] | ||
print(AverageCount.AVERAGE_32X) | ||
|
||
for sensor in sensors: | ||
sensor.measurement_mode = MeasurementMode.CONTINUOUS | ||
sensor.averaged_measurements = AverageCount.AVERAGE_32X | ||
sensor.measurement_delay = MeasurementDelay.DELAY_0_0015_S | ||
|
||
|
||
def get_temperature(): | ||
temp = [] | ||
for sensor in sensors: | ||
temp.append(sensor.temperature_updated) | ||
return temp | ||
|
||
|
||
while True: | ||
txt = f"{datetime.datetime.now()},Temperature:[{deg_sybm}C]: {get_temperature()} " | ||
print(txt) | ||
# time.sleep(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
import os | ||
os.environ['BLINKA_FT232H'] = "1" | ||
import board | ||
from adafruit_tmp117 import TMP117, AverageCount | ||
|
||
from adafruit_tmp11X import TMP116, AverageCount | ||
|
||
i2c = board.I2C() # uses board.SCL and board.SDA | ||
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller | ||
tmp117 = TMP117(i2c) | ||
t1 = TMP116(i2c_bus=i2c, address=0x49) | ||
|
||
# uncomment different options below to see how it affects the reported temperature | ||
# and measurement time | ||
|
||
# tmp117.averaged_measurements = AverageCount.AVERAGE_1X | ||
# tmp117.averaged_measurements = AverageCount.AVERAGE_8X | ||
# tmp117.averaged_measurements = AverageCount.AVERAGE_32X | ||
# tmp117.averaged_measurements = AverageCount.AVERAGE_64X | ||
# t1.averaged_measurements = AverageCount.AVERAGE_1X | ||
# t1.averaged_measurements = AverageCount.AVERAGE_8X | ||
t1.averaged_measurements = AverageCount.AVERAGE_32X | ||
# t1.averaged_measurements = AverageCount.AVERAGE_64X | ||
|
||
print( | ||
"Number of averaged samples per measurement:", | ||
AverageCount.string[tmp117.averaged_measurements], | ||
AverageCount.string[t1.averaged_measurements], | ||
) | ||
print( | ||
"Reads should take approximately", | ||
AverageCount.string[tmp117.averaged_measurements] * 0.0155, | ||
AverageCount.string[t1.averaged_measurements] * 0.0155, | ||
"seconds", | ||
) | ||
|
||
while True: | ||
print("Single measurement: %.2f degrees C" % tmp117.take_single_measurement()) | ||
print("Single measurement: %.2f degrees C" % t1.take_single_measurement()) | ||
# time.sleep(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.