Skip to content
This repository was archived by the owner on Dec 20, 2018. It is now read-only.

Commit bb3943b

Browse files
committed
Fix bug with endian of config, and add fahrenheit to example.
1 parent dabf482 commit bb3943b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Adafruit_TMP/TMP006.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ def begin(self, samplerate=CFG_16SAMPLE):
8484
self._logger.debug('Using samplerate value: {0:04X}'.format(samplerate))
8585
# Set configuration register to turn on chip, enable data ready output,
8686
# and start sampling at the specified rate.
87-
self._device.write16(TMP006_CONFIG, TMP006_CFG_MODEON | TMP006_CFG_DRDYEN | samplerate)
87+
config = TMP006_CFG_MODEON | TMP006_CFG_DRDYEN | samplerate
88+
# Flip byte order of config value because write16 uses little endian but we
89+
# need big endian here. This is an ugly hack for now, better to add support
90+
# in write16 for explicit endians.
91+
config = ((config & 0xFF) << 8) | (config >> 8)
92+
self._device.write16(TMP006_CONFIG, config)
8893
# Check manufacturer and device ID match expected values.
8994
mid = self._device.readU16BE(TMP006_MANID)
9095
did = self._device.readU16BE(TMP006_DEVID)

examples/simpletest.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
import Adafruit_TMP.TMP006 as TMP006
3030

3131

32+
# Define a function to convert celsius to fahrenheit.
33+
def c_to_f(c):
34+
return c * 9.0 / 5.0 + 32.0
35+
36+
3237
# Default constructor will use the default I2C address (0x40) and pick a default I2C bus.
3338
#
3439
# For the Raspberry Pi this means you should hook up to the only exposed I2C bus
@@ -54,6 +59,8 @@
5459
# Loop printing measurements every second.
5560
print 'Press Ctrl-C to quit.'
5661
while True:
57-
print 'Object temperature: {0:0.4F} *C'.format(sensor.readObjTempC())
58-
print ' Die temperature: {0:0.4F} *C'.format(sensor.readDieTempC())
62+
obj_temp = sensor.readObjTempC()
63+
die_temp = sensor.readDieTempC()
64+
print 'Object temperature: {0:0.3F}*C / {1:0.3F}*F'.format(obj_temp, c_to_f(obj_temp))
65+
print ' Die temperature: {0:0.3F}*C / {1:0.3F}*F'.format(die_temp, c_to_f(die_temp))
5966
time.sleep(1.0)

0 commit comments

Comments
 (0)