Skip to content

Commit 4802b24

Browse files
committed
merge begin into init -- create properties - clean up names
1 parent b520fb4 commit 4802b24

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

adafruit_tmp007.py

+17-14
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,17 @@ class TMP007:
8484

8585

8686

87-
def __init__(self, i2c, address=_TMP007_I2CADDR):
87+
def __init__(self, i2c, address=_TMP007_I2CADDR, samplerate=CFG_16SAMPLE):
8888
"""Initialize TMP007 device on the specified I2C address and bus number.
8989
Address defaults to 0x40 and bus number defaults to the appropriate bus
9090
for the hardware.
91-
"""
92-
self._device = I2CDevice(i2c, address)
93-
94-
def begin(self, samplerate=CFG_16SAMPLE):
95-
"""Start taking temperature measurements. Samplerate can be one of
91+
Start taking temperature measurements. Samplerate can be one of
9692
TMP007_CFG_1SAMPLE, TMP007_CFG_2SAMPLE, TMP007_CFG_4SAMPLE,
9793
TMP007_CFG_8SAMPLE, or TMP007_CFG_16SAMPLE. The default is 16 samples
9894
for the highest resolution. Returns True if the device is intialized,
9995
False otherwise.
10096
"""
97+
self._device = I2CDevice(i2c, address)
10198
self._write_u16(_TMP007_CONFIG, _TMP007_CFG_RESET)
10299
time.sleep(.5)
103100
if samplerate not in (CFG_1SAMPLE, CFG_2SAMPLE, CFG_4SAMPLE, CFG_8SAMPLE,
@@ -109,10 +106,11 @@ def begin(self, samplerate=CFG_16SAMPLE):
109106
config = _TMP007_CFG_MODEON | _TMP007_CFG_DRDYEN | samplerate
110107
self._write_u16(_TMP007_CONFIG, config)
111108
# Check device ID match expected value.
112-
return self.read_register(_TMP007_DEVID) == 0x0078
113-
114-
109+
dev_id = self.read_register(_TMP007_DEVID)
110+
if dev_id != 0x78:
111+
raise ValueError('Init failed - Did not find TMP007')
115112

113+
@property
116114
def sleep(self):
117115
"""Put TMP007 into low power sleep mode. No measurement data will be
118116
updated while in sleep mode.
@@ -121,13 +119,15 @@ def sleep(self):
121119
control &= ~(_TMP007_CFG_MODEON)
122120
self._write_u16(_TMP007_CONFIG, control)
123121

122+
@property
124123
def wake(self):
125124
"""Wake up TMP007 from low power sleep mode."""
126125
control = self._read_u16(_TMP007_CONFIG)
127126
control |= _TMP007_CFG_MODEON
128127
self._write_u16(_TMP007_CONFIG, control)
129128

130-
def read_raw_voltage(self):
129+
@property
130+
def raw_voltage(self):
131131
"""Read raw voltage from TMP007 sensor. Meant to be used in the
132132
calculation of temperature values.
133133
"""
@@ -136,19 +136,22 @@ def read_raw_voltage(self):
136136
raw = (raw & 0x7fff) - 32768
137137
return raw
138138

139-
def read_raw_die_temperature(self):
139+
@property
140+
def raw_sensor_temperature(self):
140141
"""Read raw die temperature from TMP007 sensor. Meant to be used in the
141142
calculation of temperature values.
142143
"""
143144
raw = self._read_u16(_TMP007_TAMB)
144145
return raw >> 2
145146

146-
def read_die_temp_c(self):
147+
@property
148+
def die_temperature(self):
147149
"""Read sensor die temperature and return its value in degrees celsius."""
148-
t_die = self.read_raw_die_temperature()
150+
t_die = self.raw_sensor_temperature
149151
return t_die * 0.03125
150152

151-
def read_obj_temp_c(self):
153+
@property
154+
def temperature(self):
152155
"""Read object temperature from TMP007 sensor.
153156
"""
154157
raw = self._read_u16(_TMP007_TOBJ)

examples/tmp007_simpletest.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ def c_to_f(c):
2424
# Initialize communication with the sensor, using the default 16 samples per conversion.
2525
# This is the best accuracy but a little slower at reacting to changes.
2626
# The first sample will be meaningless
27-
if sensor.begin():
28-
while True:
29-
die_temp = sensor.read_die_temp_c()
30-
print(' Die temperature: {0:0.3F}*C / {1:0.3F}*F'.format(die_temp, c_to_f(die_temp)))
31-
obj_temp = sensor.read_obj_temp_c()
32-
print('Object temperature: {0:0.3F}*C / {1:0.3F}*F'.format(obj_temp, c_to_f(obj_temp)))
33-
time.sleep(5.0)
34-
else:
35-
print("Sensor Init failed")
27+
while True:
28+
die_temp = sensor.die_temperature
29+
print(' Die temperature: {0:0.3F}*C / {1:0.3F}*F'.format(die_temp, c_to_f(die_temp)))
30+
obj_temp = sensor.temperature
31+
print('Object temperature: {0:0.3F}*C / {1:0.3F}*F'.format(obj_temp, c_to_f(obj_temp)))
32+
time.sleep(5.0)

0 commit comments

Comments
 (0)