bluerobotics-tmp119 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Blue Robotics Inc.
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.
@@ -0,0 +1,134 @@
1
+ Metadata-Version: 2.4
2
+ Name: bluerobotics-tmp119
3
+ Version: 0.0.1
4
+ Summary: A python module for the TMP119 digital temperature sensor
5
+ Home-page: https://www.bluerobotics.com
6
+ Author: Blue Robotics
7
+ Author-email: support@bluerobotics.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: author
15
+ Dynamic: author-email
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: home-page
20
+ Dynamic: license-file
21
+ Dynamic: requires-python
22
+ Dynamic: summary
23
+
24
+ # tmp119-python
25
+
26
+ A Python module to interface with the TMP119 temperature sensor. The TMP119 is
27
+ an ultra-high-accuracy, low-power digital temperature sensor from Texas
28
+ Instruments with an I2C-compatible interface. Tested on Raspberry Pi with
29
+ Raspberry Pi OS.
30
+
31
+ The python SMBus library must be installed.
32
+
33
+ sudo apt-get install python3-smbus
34
+
35
+ # Usage
36
+
37
+ import tmp119
38
+
39
+ ### TMP119(bus=1, address=0x48)
40
+
41
+ sensor = tmp119.TMP119() # Use default I2C bus 1, address 0x48
42
+ sensor = tmp119.TMP119(0) # Specify I2C bus 0
43
+ sensor = tmp119.TMP119(1, 0x49) # Specify bus and I2C address
44
+
45
+ The TMP119 supports up to four I2C addresses, selected by the ADD0 pin:
46
+
47
+ 0x48 (ADD0 to GND, default)
48
+ 0x49 (ADD0 to V+)
49
+ 0x4A (ADD0 to SDA)
50
+ 0x4B (ADD0 to SCL)
51
+
52
+ ### init()
53
+
54
+ Initialize the sensor. This needs to be called before using any other methods.
55
+ It verifies the device ID and configures the sensor for the fastest update rate
56
+ (no averaging, no added standby delay, ~15.5 ms cycle). Call `set_averaging()` /
57
+ `set_read_delay()` afterwards to trade speed for lower noise.
58
+
59
+ sensor.init()
60
+
61
+ Returns True if the sensor was successfully initialized, False otherwise.
62
+
63
+ ### read()
64
+
65
+ Read the sensor and update the temperature. The TMP119 runs in
66
+ continuous-conversion mode, so this always returns the latest result.
67
+
68
+ sensor.read()
69
+
70
+ Returns True if the read was successful, False otherwise.
71
+
72
+ ### temperature(conversion=UNITS_Centigrade)
73
+
74
+ Get the most recent temperature measurement.
75
+
76
+ sensor.temperature() # Centigrade (default)
77
+ sensor.temperature(tmp119.UNITS_Fahrenheit) # Fahrenheit
78
+
79
+ Valid arguments are:
80
+
81
+ tmp119.UNITS_Centigrade
82
+ tmp119.UNITS_Fahrenheit
83
+ tmp119.UNITS_Kelvin
84
+
85
+ Returns the most recent temperature in the requested units, or temperature in
86
+ degrees Centigrade if invalid units specified. Call `read()` to update.
87
+
88
+ ### set_averaging(avg)
89
+
90
+ Set the conversion averaging mode. More averaging reduces noise but takes
91
+ longer to produce each result. Returns True if the write succeeded.
92
+
93
+ sensor.set_averaging(tmp119.TMP119_AVERAGE_64X)
94
+
95
+ Valid arguments are (with the time each takes per result):
96
+
97
+ tmp119.TMP119_AVERAGE_1X # 15.5 ms
98
+ tmp119.TMP119_AVERAGE_8X # 125 ms
99
+ tmp119.TMP119_AVERAGE_32X # 500 ms
100
+ tmp119.TMP119_AVERAGE_64X # 1 s
101
+
102
+ ### set_read_delay(delay)
103
+
104
+ Set the *minimum* standby delay between conversions in continuous-conversion
105
+ mode. The actual time between readings is the greater of this standby delay and
106
+ the averaging time (see datasheet Table 8-6); for example, `TMP119_AVERAGE_64X`
107
+ always yields at least a ~1 s cycle because the averaging alone takes 1 s,
108
+ regardless of the delay setting. Returns True if the write succeeded.
109
+
110
+ sensor.set_read_delay(tmp119.TMP119_DELAY_1000_MS)
111
+
112
+ Valid arguments are:
113
+
114
+ tmp119.TMP119_DELAY_NONE
115
+ tmp119.TMP119_DELAY_125_MS
116
+ tmp119.TMP119_DELAY_250_MS
117
+ tmp119.TMP119_DELAY_500_MS
118
+ tmp119.TMP119_DELAY_1000_MS
119
+ tmp119.TMP119_DELAY_4000_MS
120
+ tmp119.TMP119_DELAY_8000_MS
121
+ tmp119.TMP119_DELAY_16000_MS
122
+
123
+ ### get_config() / set_config(config)
124
+
125
+ Read or write the raw 16-bit configuration register (address 0x01) for advanced
126
+ use. `set_config()` returns True if the write succeeded; read-only bits are
127
+ ignored by the device.
128
+
129
+ config = sensor.get_config()
130
+ sensor.set_config(config)
131
+
132
+ # Reference
133
+
134
+ You can find the [TMP119 datasheet here](https://www.ti.com/lit/ds/symlink/tmp119.pdf).
@@ -0,0 +1,111 @@
1
+ # tmp119-python
2
+
3
+ A Python module to interface with the TMP119 temperature sensor. The TMP119 is
4
+ an ultra-high-accuracy, low-power digital temperature sensor from Texas
5
+ Instruments with an I2C-compatible interface. Tested on Raspberry Pi with
6
+ Raspberry Pi OS.
7
+
8
+ The python SMBus library must be installed.
9
+
10
+ sudo apt-get install python3-smbus
11
+
12
+ # Usage
13
+
14
+ import tmp119
15
+
16
+ ### TMP119(bus=1, address=0x48)
17
+
18
+ sensor = tmp119.TMP119() # Use default I2C bus 1, address 0x48
19
+ sensor = tmp119.TMP119(0) # Specify I2C bus 0
20
+ sensor = tmp119.TMP119(1, 0x49) # Specify bus and I2C address
21
+
22
+ The TMP119 supports up to four I2C addresses, selected by the ADD0 pin:
23
+
24
+ 0x48 (ADD0 to GND, default)
25
+ 0x49 (ADD0 to V+)
26
+ 0x4A (ADD0 to SDA)
27
+ 0x4B (ADD0 to SCL)
28
+
29
+ ### init()
30
+
31
+ Initialize the sensor. This needs to be called before using any other methods.
32
+ It verifies the device ID and configures the sensor for the fastest update rate
33
+ (no averaging, no added standby delay, ~15.5 ms cycle). Call `set_averaging()` /
34
+ `set_read_delay()` afterwards to trade speed for lower noise.
35
+
36
+ sensor.init()
37
+
38
+ Returns True if the sensor was successfully initialized, False otherwise.
39
+
40
+ ### read()
41
+
42
+ Read the sensor and update the temperature. The TMP119 runs in
43
+ continuous-conversion mode, so this always returns the latest result.
44
+
45
+ sensor.read()
46
+
47
+ Returns True if the read was successful, False otherwise.
48
+
49
+ ### temperature(conversion=UNITS_Centigrade)
50
+
51
+ Get the most recent temperature measurement.
52
+
53
+ sensor.temperature() # Centigrade (default)
54
+ sensor.temperature(tmp119.UNITS_Fahrenheit) # Fahrenheit
55
+
56
+ Valid arguments are:
57
+
58
+ tmp119.UNITS_Centigrade
59
+ tmp119.UNITS_Fahrenheit
60
+ tmp119.UNITS_Kelvin
61
+
62
+ Returns the most recent temperature in the requested units, or temperature in
63
+ degrees Centigrade if invalid units specified. Call `read()` to update.
64
+
65
+ ### set_averaging(avg)
66
+
67
+ Set the conversion averaging mode. More averaging reduces noise but takes
68
+ longer to produce each result. Returns True if the write succeeded.
69
+
70
+ sensor.set_averaging(tmp119.TMP119_AVERAGE_64X)
71
+
72
+ Valid arguments are (with the time each takes per result):
73
+
74
+ tmp119.TMP119_AVERAGE_1X # 15.5 ms
75
+ tmp119.TMP119_AVERAGE_8X # 125 ms
76
+ tmp119.TMP119_AVERAGE_32X # 500 ms
77
+ tmp119.TMP119_AVERAGE_64X # 1 s
78
+
79
+ ### set_read_delay(delay)
80
+
81
+ Set the *minimum* standby delay between conversions in continuous-conversion
82
+ mode. The actual time between readings is the greater of this standby delay and
83
+ the averaging time (see datasheet Table 8-6); for example, `TMP119_AVERAGE_64X`
84
+ always yields at least a ~1 s cycle because the averaging alone takes 1 s,
85
+ regardless of the delay setting. Returns True if the write succeeded.
86
+
87
+ sensor.set_read_delay(tmp119.TMP119_DELAY_1000_MS)
88
+
89
+ Valid arguments are:
90
+
91
+ tmp119.TMP119_DELAY_NONE
92
+ tmp119.TMP119_DELAY_125_MS
93
+ tmp119.TMP119_DELAY_250_MS
94
+ tmp119.TMP119_DELAY_500_MS
95
+ tmp119.TMP119_DELAY_1000_MS
96
+ tmp119.TMP119_DELAY_4000_MS
97
+ tmp119.TMP119_DELAY_8000_MS
98
+ tmp119.TMP119_DELAY_16000_MS
99
+
100
+ ### get_config() / set_config(config)
101
+
102
+ Read or write the raw 16-bit configuration register (address 0x01) for advanced
103
+ use. `set_config()` returns True if the write succeeded; read-only bits are
104
+ ignored by the device.
105
+
106
+ config = sensor.get_config()
107
+ sensor.set_config(config)
108
+
109
+ # Reference
110
+
111
+ You can find the [TMP119 datasheet here](https://www.ti.com/lit/ds/symlink/tmp119.pdf).
@@ -0,0 +1,134 @@
1
+ Metadata-Version: 2.4
2
+ Name: bluerobotics-tmp119
3
+ Version: 0.0.1
4
+ Summary: A python module for the TMP119 digital temperature sensor
5
+ Home-page: https://www.bluerobotics.com
6
+ Author: Blue Robotics
7
+ Author-email: support@bluerobotics.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: author
15
+ Dynamic: author-email
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: home-page
20
+ Dynamic: license-file
21
+ Dynamic: requires-python
22
+ Dynamic: summary
23
+
24
+ # tmp119-python
25
+
26
+ A Python module to interface with the TMP119 temperature sensor. The TMP119 is
27
+ an ultra-high-accuracy, low-power digital temperature sensor from Texas
28
+ Instruments with an I2C-compatible interface. Tested on Raspberry Pi with
29
+ Raspberry Pi OS.
30
+
31
+ The python SMBus library must be installed.
32
+
33
+ sudo apt-get install python3-smbus
34
+
35
+ # Usage
36
+
37
+ import tmp119
38
+
39
+ ### TMP119(bus=1, address=0x48)
40
+
41
+ sensor = tmp119.TMP119() # Use default I2C bus 1, address 0x48
42
+ sensor = tmp119.TMP119(0) # Specify I2C bus 0
43
+ sensor = tmp119.TMP119(1, 0x49) # Specify bus and I2C address
44
+
45
+ The TMP119 supports up to four I2C addresses, selected by the ADD0 pin:
46
+
47
+ 0x48 (ADD0 to GND, default)
48
+ 0x49 (ADD0 to V+)
49
+ 0x4A (ADD0 to SDA)
50
+ 0x4B (ADD0 to SCL)
51
+
52
+ ### init()
53
+
54
+ Initialize the sensor. This needs to be called before using any other methods.
55
+ It verifies the device ID and configures the sensor for the fastest update rate
56
+ (no averaging, no added standby delay, ~15.5 ms cycle). Call `set_averaging()` /
57
+ `set_read_delay()` afterwards to trade speed for lower noise.
58
+
59
+ sensor.init()
60
+
61
+ Returns True if the sensor was successfully initialized, False otherwise.
62
+
63
+ ### read()
64
+
65
+ Read the sensor and update the temperature. The TMP119 runs in
66
+ continuous-conversion mode, so this always returns the latest result.
67
+
68
+ sensor.read()
69
+
70
+ Returns True if the read was successful, False otherwise.
71
+
72
+ ### temperature(conversion=UNITS_Centigrade)
73
+
74
+ Get the most recent temperature measurement.
75
+
76
+ sensor.temperature() # Centigrade (default)
77
+ sensor.temperature(tmp119.UNITS_Fahrenheit) # Fahrenheit
78
+
79
+ Valid arguments are:
80
+
81
+ tmp119.UNITS_Centigrade
82
+ tmp119.UNITS_Fahrenheit
83
+ tmp119.UNITS_Kelvin
84
+
85
+ Returns the most recent temperature in the requested units, or temperature in
86
+ degrees Centigrade if invalid units specified. Call `read()` to update.
87
+
88
+ ### set_averaging(avg)
89
+
90
+ Set the conversion averaging mode. More averaging reduces noise but takes
91
+ longer to produce each result. Returns True if the write succeeded.
92
+
93
+ sensor.set_averaging(tmp119.TMP119_AVERAGE_64X)
94
+
95
+ Valid arguments are (with the time each takes per result):
96
+
97
+ tmp119.TMP119_AVERAGE_1X # 15.5 ms
98
+ tmp119.TMP119_AVERAGE_8X # 125 ms
99
+ tmp119.TMP119_AVERAGE_32X # 500 ms
100
+ tmp119.TMP119_AVERAGE_64X # 1 s
101
+
102
+ ### set_read_delay(delay)
103
+
104
+ Set the *minimum* standby delay between conversions in continuous-conversion
105
+ mode. The actual time between readings is the greater of this standby delay and
106
+ the averaging time (see datasheet Table 8-6); for example, `TMP119_AVERAGE_64X`
107
+ always yields at least a ~1 s cycle because the averaging alone takes 1 s,
108
+ regardless of the delay setting. Returns True if the write succeeded.
109
+
110
+ sensor.set_read_delay(tmp119.TMP119_DELAY_1000_MS)
111
+
112
+ Valid arguments are:
113
+
114
+ tmp119.TMP119_DELAY_NONE
115
+ tmp119.TMP119_DELAY_125_MS
116
+ tmp119.TMP119_DELAY_250_MS
117
+ tmp119.TMP119_DELAY_500_MS
118
+ tmp119.TMP119_DELAY_1000_MS
119
+ tmp119.TMP119_DELAY_4000_MS
120
+ tmp119.TMP119_DELAY_8000_MS
121
+ tmp119.TMP119_DELAY_16000_MS
122
+
123
+ ### get_config() / set_config(config)
124
+
125
+ Read or write the raw 16-bit configuration register (address 0x01) for advanced
126
+ use. `set_config()` returns True if the write succeeded; read-only bits are
127
+ ignored by the device.
128
+
129
+ config = sensor.get_config()
130
+ sensor.set_config(config)
131
+
132
+ # Reference
133
+
134
+ You can find the [TMP119 datasheet here](https://www.ti.com/lit/ds/symlink/tmp119.pdf).
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ setup.py
4
+ bluerobotics_tmp119.egg-info/PKG-INFO
5
+ bluerobotics_tmp119.egg-info/SOURCES.txt
6
+ bluerobotics_tmp119.egg-info/dependency_links.txt
7
+ bluerobotics_tmp119.egg-info/top_level.txt
8
+ tests/test_tmp119.py
9
+ tmp119/__init__.py
10
+ tmp119/tmp119.py
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env python
2
+
3
+ from setuptools import setup, find_packages
4
+
5
+ with open("README.md", "r") as f:
6
+ long_description = f.read()
7
+
8
+ setup(name='bluerobotics-tmp119',
9
+ version='0.0.1',
10
+ description='A python module for the TMP119 digital temperature sensor',
11
+ long_description=long_description,
12
+ long_description_content_type='text/markdown',
13
+ author='Blue Robotics',
14
+ author_email='support@bluerobotics.com',
15
+ url='https://www.bluerobotics.com',
16
+ packages=find_packages(),
17
+ python_requires='>=3.6',
18
+ classifiers=[
19
+ "Programming Language :: Python :: 3",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Operating System :: OS Independent",
22
+ ]
23
+ )
@@ -0,0 +1,80 @@
1
+ import pytest
2
+
3
+ import tmp119
4
+
5
+ # Config register bit masks (mirrors the private constants in the driver)
6
+ AVG_MASK = 0x0060
7
+ CONV_MASK = 0x0380
8
+
9
+
10
+ def test_device_id(sensor):
11
+ assert sensor.get_device_id() == 0x2117
12
+
13
+
14
+ def test_init_success_clears_avg_and_conv(sensor):
15
+ assert sensor.init() is True
16
+ assert sensor.get_config() & (AVG_MASK | CONV_MASK) == 0
17
+
18
+
19
+ def test_init_preserves_unrelated_config_bits(sensor):
20
+ # Set a bit outside the AVG/CONV masks; init() must leave it untouched.
21
+ sensor._bus.regs[0x01] = [0x82, 0x20] # 0x8220
22
+ assert sensor.init() is True
23
+ assert sensor.get_config() == 0x8000
24
+
25
+
26
+ def test_init_fails_on_wrong_device_id(sensor):
27
+ sensor._bus.regs[0x0F] = [0x00, 0x00]
28
+ assert sensor.init() is False
29
+
30
+
31
+ def test_init_returns_false_without_bus(sensor):
32
+ sensor._bus = None
33
+ assert sensor.init() is False
34
+
35
+
36
+ def test_read_returns_false_without_bus(sensor):
37
+ sensor._bus = None
38
+ assert sensor.read() is False
39
+
40
+
41
+ def test_read_positive_temperature(sensor):
42
+ sensor._bus.regs[0x00] = [0x0C, 0x80] # 3200 * 0.0078125 = 25.0 C
43
+ assert sensor.read() is True
44
+ assert sensor.temperature() == pytest.approx(25.0)
45
+
46
+
47
+ def test_read_negative_temperature(sensor):
48
+ sensor._bus.regs[0x00] = [0xFF, 0x00] # -256 * 0.0078125 = -2.0 C
49
+ sensor.read()
50
+ assert sensor.temperature() == pytest.approx(-2.0)
51
+
52
+
53
+ def test_temperature_unit_conversions(sensor):
54
+ sensor._bus.regs[0x00] = [0x0C, 0x80] # 25.0 C
55
+ sensor.read()
56
+ assert sensor.temperature(tmp119.UNITS_Centigrade) == pytest.approx(25.0)
57
+ assert sensor.temperature(tmp119.UNITS_Fahrenheit) == pytest.approx(77.0)
58
+ assert sensor.temperature(tmp119.UNITS_Kelvin) == pytest.approx(298.15)
59
+
60
+
61
+ def test_set_averaging(sensor):
62
+ assert sensor.set_averaging(tmp119.TMP119_AVERAGE_64X) is True
63
+ assert sensor.get_config() & AVG_MASK == (3 << 5)
64
+
65
+
66
+ def test_set_read_delay(sensor):
67
+ assert sensor.set_read_delay(tmp119.TMP119_DELAY_16000_MS) is True
68
+ assert sensor.get_config() & CONV_MASK == (7 << 7)
69
+
70
+
71
+ def test_set_averaging_preserves_read_delay(sensor):
72
+ sensor.set_read_delay(tmp119.TMP119_DELAY_8000_MS)
73
+ sensor.set_averaging(tmp119.TMP119_AVERAGE_32X)
74
+ assert sensor.get_config() & CONV_MASK == (6 << 7)
75
+ assert sensor.get_config() & AVG_MASK == (2 << 5)
76
+
77
+
78
+ def test_set_config_round_trips_16_bits(sensor):
79
+ sensor.set_config(0xABCD)
80
+ assert sensor.get_config() == 0xABCD
@@ -0,0 +1 @@
1
+ from .tmp119 import *
@@ -0,0 +1,169 @@
1
+ """Driver for the Texas Instruments TMP119 high-accuracy temperature sensor."""
2
+
3
+ try:
4
+ import smbus
5
+ except ImportError:
6
+ print('Try sudo apt-get install python3-smbus')
7
+
8
+ # Valid units
9
+ UNITS_Centigrade = 1
10
+ UNITS_Fahrenheit = 2
11
+ UNITS_Kelvin = 3
12
+
13
+ # Conversion averaging mode (AVG[1:0], config register bits 6:5).
14
+ # More averaging reduces noise but takes longer to produce each result. The
15
+ # time to compute one averaged result is the per-mode minimum conversion time
16
+ # shown below (datasheet Table 8-6). Power-on default: TMP119_AVERAGE_8X.
17
+ TMP119_AVERAGE_1X = 0 # No averaging (min conversion time 15.5 ms)
18
+ TMP119_AVERAGE_8X = 1 # 8 conversions (min conversion time 125 ms, default)
19
+ TMP119_AVERAGE_32X = 2 # 32 conversions (min conversion time 500 ms)
20
+ TMP119_AVERAGE_64X = 3 # 64 conversions (min conversion time 1 s)
21
+
22
+ # Minimum standby delay inserted between conversions in continuous-conversion
23
+ # mode (CONV[2:0], config register bits 9:7).
24
+ #
25
+ # This is not the actual time between readings on its own. The actual
26
+ # conversion cycle time is the greater of this standby delay and the time
27
+ # needed by the selected averaging mode (see the averaging constants above).
28
+ # For example, TMP119_DELAY_NONE with TMP119_AVERAGE_64X still produces a ~1 s
29
+ # cycle because the 64x average alone takes 1 s. See datasheet Table 8-6.
30
+ #
31
+ # Power-on default: TMP119_DELAY_1000_MS.
32
+ TMP119_DELAY_NONE = 0 # no added standby delay
33
+ TMP119_DELAY_125_MS = 1
34
+ TMP119_DELAY_250_MS = 2
35
+ TMP119_DELAY_500_MS = 3
36
+ TMP119_DELAY_1000_MS = 4 # power-on default
37
+ TMP119_DELAY_4000_MS = 5
38
+ TMP119_DELAY_8000_MS = 6
39
+ TMP119_DELAY_16000_MS = 7
40
+
41
+
42
+ class TMP119:
43
+
44
+ # Registers
45
+ _TEMP_REG = 0x00
46
+ _CONFIG_REG = 0x01
47
+ _DEVICE_ID_REG = 0x0F
48
+ _DEVICE_ID = 0x2117
49
+
50
+ # CONV[2:0] (standby delay), config register bits 9:7
51
+ _CONV_SHIFT = 7
52
+ _CONV_MASK = 0x0380
53
+ # AVG[1:0] (averaging), config register bits 6:5
54
+ _AVG_SHIFT = 5
55
+ _AVG_MASK = 0x0060
56
+
57
+ # Each LSB of the temperature register represents this many degrees C
58
+ _LSB_C = 0.0078125
59
+
60
+ def __init__(self, bus=1, address=0x48):
61
+ """Create a sensor on the given I2C bus and address.
62
+
63
+ The TMP119 supports up to four I2C addresses (0x48 - 0x4B), selected by
64
+ the ADD0 pin. The default address (ADD0 to GND) is 0x48.
65
+ """
66
+ self._address = address
67
+
68
+ # Degrees C
69
+ self._temperature = 0
70
+
71
+ try:
72
+ self._bus = smbus.SMBus(bus)
73
+ except Exception:
74
+ print("Bus %d is not available." % bus)
75
+ print("Available busses are listed as /dev/i2c*")
76
+ self._bus = None
77
+
78
+ def init(self):
79
+ """Verify the device and configure it for the fastest update rate.
80
+
81
+ Returns True on success, False if the bus is unavailable or the device
82
+ ID does not match. Call set_averaging()/set_read_delay() afterwards to
83
+ trade speed for lower noise.
84
+ """
85
+ if self._bus is None:
86
+ print("No bus!")
87
+ return False
88
+
89
+ if self.get_device_id() != self._DEVICE_ID:
90
+ return False
91
+
92
+ # Configure for the fastest update rate: no averaging and no added
93
+ # standby delay, giving a ~15.5 ms conversion cycle (datasheet Table
94
+ # 8-6). Clearing the AVG and CONV bits sets AVG = 1X and delay = none.
95
+ config = self.get_config()
96
+ config &= ~(self._AVG_MASK | self._CONV_MASK)
97
+ return self.set_config(config)
98
+
99
+ def read(self):
100
+ """Read the latest conversion and update the stored temperature.
101
+
102
+ Returns True on success, False if the bus is unavailable.
103
+ """
104
+ if self._bus is None:
105
+ print("No bus!")
106
+ return False
107
+
108
+ # The TMP119 powers up in continuous-conversion mode, so the
109
+ # temperature register always holds the most recent conversion.
110
+ raw = self._read_register(self._TEMP_REG)
111
+
112
+ # Data is in 2's complement format
113
+ if raw > 32767:
114
+ raw -= 65536
115
+
116
+ self._temperature = raw * self._LSB_C
117
+ return True
118
+
119
+ def temperature(self, conversion=UNITS_Centigrade):
120
+ """Return the most recent temperature in the requested units.
121
+
122
+ Defaults to degrees Centigrade. Call read() to update the value.
123
+ """
124
+ if conversion == UNITS_Fahrenheit:
125
+ return (9 / 5) * self._temperature + 32
126
+ elif conversion == UNITS_Kelvin:
127
+ return self._temperature + 273.15
128
+ return self._temperature
129
+
130
+ def set_averaging(self, avg):
131
+ """Set the conversion averaging mode, preserving other config bits."""
132
+ config = self.get_config()
133
+ config = (config & ~self._AVG_MASK) | \
134
+ ((avg << self._AVG_SHIFT) & self._AVG_MASK)
135
+ return self.set_config(config)
136
+
137
+ def set_read_delay(self, delay):
138
+ """Set the standby delay between conversions, preserving other bits."""
139
+ config = self.get_config()
140
+ config = (config & ~self._CONV_MASK) | \
141
+ ((delay << self._CONV_SHIFT) & self._CONV_MASK)
142
+ return self.set_config(config)
143
+
144
+ def get_config(self):
145
+ """Read the raw 16-bit configuration register (address 0x01)."""
146
+ return self._read_register(self._CONFIG_REG)
147
+
148
+ def set_config(self, config):
149
+ """Write the raw 16-bit configuration register (address 0x01).
150
+
151
+ Read-only bits are ignored by the device.
152
+ """
153
+ return self._write_register(self._CONFIG_REG, config)
154
+
155
+ def get_device_id(self):
156
+ """Read the raw 16-bit device ID register (address 0x0F)."""
157
+ return self._read_register(self._DEVICE_ID_REG)
158
+
159
+ # The TMP119 transfers 16-bit registers MSB first. smbus word transfers are
160
+ # little-endian, so we use block transfers and order the bytes ourselves.
161
+ def _read_register(self, register):
162
+ data = self._bus.read_i2c_block_data(self._address, register, 2)
163
+ return (data[0] << 8) | data[1]
164
+
165
+ def _write_register(self, register, value):
166
+ value &= 0xFFFF
167
+ self._bus.write_i2c_block_data(
168
+ self._address, register, [(value >> 8) & 0xFF, value & 0xFF])
169
+ return True