... | ... |
@@ -57,17 +57,25 @@ class tmp102: |
57 | 57 |
# register. |
58 | 58 |
def __init__(self, sAddr=DEFAULT_BUS_ADDRESS, |
59 | 59 |
sbus=DEFAULT_BUS_NUMBER, |
60 |
- config=DEFAULT_CONFIG): |
|
60 |
+ config=DEFAULT_CONFIG, |
|
61 |
+ debug=False): |
|
61 | 62 |
# Instantiate a smbus object |
62 | 63 |
self.sensorAddr = sAddr |
63 | 64 |
self.bus = smbus.SMBus(sbus) |
65 |
+ self.debugMode = debug |
|
66 |
+ |
|
64 | 67 |
# Initialize TMP102 sensor. |
65 | 68 |
initData = [(config >> 8), (config & 0x00FF)] |
66 | 69 |
self.bus.write_i2c_block_data(self.sensorAddr, CONFIG_REG, initData) |
70 |
+ |
|
71 |
+ if self.debugMode: |
|
72 |
+ # Read the TMP102 configuration register. |
|
73 |
+ data = self.getInfo() |
|
74 |
+ print("configuration register: %s %s\n" % data) |
|
67 | 75 |
## end def |
68 | 76 |
|
69 | 77 |
# Reads the configuration register (two bytes). |
70 |
- def status(self): |
|
78 |
+ def getInfo(self): |
|
71 | 79 |
# Read configuration data |
72 | 80 |
config = self.bus.read_i2c_block_data(self.sensorAddr, CONFIG_REG, 2) |
73 | 81 |
configB1 = format(config[0], "08b") |
... | ... |
@@ -99,6 +107,12 @@ class tmp102: |
99 | 107 |
# The temperature is returned in d11-d0, a two's complement, |
100 | 108 |
# 12 bit number. This means that d11 is the sign bit. |
101 | 109 |
data=self.bus.read_i2c_block_data(self.sensorAddr, TEMP_REG, 2) |
110 |
+ |
|
111 |
+ if self.debugMode: |
|
112 |
+ dataB1 = format(data[0], "08b") |
|
113 |
+ dataB2 = format(data[1], "08b") |
|
114 |
+ print("Temperature Reg: %s %s" % (dataB1, dataB2)) |
|
115 |
+ |
|
102 | 116 |
# Format into a 12 bit word. |
103 | 117 |
bData = ( data[0] << 8 | data[1] ) >> 4 |
104 | 118 |
# Convert from two's complement to integer. |
... | ... |
@@ -121,24 +135,18 @@ class tmp102: |
121 | 135 |
|
122 | 136 |
def testclass(): |
123 | 137 |
# Initialize the smbus and TMP102 sensor. |
124 |
- ts1 = tmp102(0x48, 1) |
|
125 |
- # Read the TMP102 configuration register. |
|
126 |
- data = ts1.status() |
|
127 |
- print "configuration register: %s %s\n" % data |
|
138 |
+ ts1 = tmp102(0x48, 1, debug=True) |
|
128 | 139 |
# Print out sensor values. |
129 | 140 |
bAl = False |
130 | 141 |
while True: |
131 |
- regdata = ts1.getTempReg() |
|
132 | 142 |
tempC = ts1.getTempC() |
133 | 143 |
tempF = ts1.getTempF() |
134 | 144 |
if bAl: |
135 | 145 |
bAl = False |
136 |
- print("\033[42;30mTemperature Reg: %s %s\033[m" % regdata) |
|
137 |
- print("\033[42;30m%6.2f%sC %6.2f%s \033[m" % \ |
|
146 |
+ print("\033[42;30m%6.2f%sC %6.2f%sF \033[m" % \ |
|
138 | 147 |
(tempC, DEGSYM, tempF, DEGSYM)) |
139 | 148 |
else: |
140 | 149 |
bAl = True |
141 |
- print("Temperature Reg: %s %s" % regdata) |
|
142 | 150 |
print("%6.2f%sC %6.2f%sF" % \ |
143 | 151 |
(tempC, DEGSYM, tempF, DEGSYM)) |
144 | 152 |
time.sleep(2) |