Skip to content

Commit fe8cf3a

Browse files
committed
PHT working
1 parent b6b841a commit fe8cf3a

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

adafruit_ms8607.py

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
_MS8607_COEFF_ADD = const(-6) #
5555

5656

57-
_MS8607_PT_CMD_RESET_COMMAND = const(0x1E) # Command to reset pressure sensor
58-
_MS8607_PT_CMD_PRESS_START = const(0x40) # Command to start pressure ADC measurement
59-
_MS8607_PT_CMD_TEMP_START = const(0x50) # Command to start temperature ADC measurement
60-
_MS8607_PT_CMD_READ_ADC = const(0x00) # Temp and pressure ADC read command
57+
_MS8607_PT_CMD_RESET_COMMAND = const(0x1E) # Command to reset pressure sensor
58+
_MS8607_PT_CMD_PRESS_START = const(0x40) # Command to start pressure ADC measurement
59+
_MS8607_PT_CMD_TEMP_START = const(0x50) # Command to start temperature ADC measurement
60+
_MS8607_PT_CMD_READ_ADC = const(0x00) # Temp and pressure ADC read command
6161
# enum MS8607_humidity_resolution
6262
#:
6363
# MS8607_humidity_resolution_12b = 0,
@@ -83,7 +83,8 @@ def __init__(self, i2c_bus):
8383
self.reset()
8484
self.initialize()
8585
self._psensor_resolution_osr = 5
86-
86+
self._pressure = None
87+
self._temperature = None
8788

8889
def reset(self):
8990
"""Reset the sensor to an initial unconfigured state"""
@@ -107,27 +108,24 @@ def initialize(self):
107108
in_end=2,
108109
)
109110

110-
# print("got calibration constant %d(%s)"%(calibration_const, hex(calibration_const)))
111111
constants.extend(unpack_from(">H", self._buffer[0:2]))
112112

113113
crc_value = (constants[0] & 0xF000) >> 12
114114
constants.append(0)
115115
if not self._check_press_calibration_crc(constants, crc_value):
116116
raise RuntimeError("CRC Error reading humidity calibration constants")
117117

118-
self.press_sens = constants[1]
119-
self.press_offset = constants[2]
120-
self.press_sens_temp_coeff = constants[3]
121-
self.press_offset_temp_coeff = constants[4]
122-
self.ref_temp = constants[5]
123-
self.temp_temp_coeff = constants[6]
118+
self._calibration_constants = constants
124119

125120
@property
126-
def temperature(self):
127-
"""The current temperature in degrees Celcius"""
128-
129-
dT, TEMP, OFF, SENS, P, T2, OFF2, SENS2, raw_temp, raw_pressure = (0,)*10
130-
121+
def _pressure_temperature(self):
122+
press_sens = self._calibration_constants[1]
123+
press_offset = self._calibration_constants[2]
124+
press_sens_temp_coeff = self._calibration_constants[3]
125+
press_offset_temp_coeff = self._calibration_constants[4]
126+
ref_temp = self._calibration_constants[5]
127+
temp_temp_coeff = self._calibration_constants[6]
128+
#################
131129
# First read temperature
132130
cmd = self._psensor_resolution_osr * 2
133131
cmd |= _MS8607_PT_CMD_TEMP_START
@@ -139,13 +137,14 @@ def temperature(self):
139137

140138
self._buffer[0] = _MS8607_PT_CMD_READ_ADC
141139
with self.pressure_i2c_device as i2c:
142-
i2c.write_then_readinto(self._buffer, self._buffer, out_start=0, out_end=1, in_start=1, in_end=3)
140+
i2c.write_then_readinto(
141+
self._buffer, self._buffer, out_start=0, out_end=1, in_start=1, in_end=3
142+
)
143143

144144
# temp is only 24 bits but unpack wants 4 bytes so add a forth byte
145145
self._buffer[0] = 0
146146
raw_temperature = unpack_from(">I", self._buffer)[0]
147-
148-
147+
#################
149148

150149
# next read pressure
151150
cmd = self._psensor_resolution_osr * 2
@@ -158,55 +157,65 @@ def temperature(self):
158157

159158
self._buffer[0] = _MS8607_PT_CMD_READ_ADC
160159
with self.pressure_i2c_device as i2c:
161-
i2c.write_then_readinto(self._buffer, self._buffer, out_start=0, out_end=1, in_start=1, in_end=3)
160+
i2c.write_then_readinto(
161+
self._buffer, self._buffer, out_start=0, out_end=1, in_start=1, in_end=3
162+
)
162163
# temp is only 24 bits but unpack wants 4 bytes so add a forth byte
163164
self._buffer[0] = 0
165+
164166
raw_pressure = unpack_from(">I", self._buffer)[0]
165-
raw_pressure >>=8
167+
#################
166168

167-
dT = raw_temperature - (self.ref_temp << 8)
169+
delta_temp = raw_temperature - (ref_temp << 8)
168170

169-
# # Actual temperature = 2000 + dT * TEMPSENS
170-
TEMP = 2000 + (dT * self.temp_temp_coeff >> 23)
171+
# # Actual temperature = 2000 + delta_temp * TEMPSENS
172+
tmp_temp = 2000 + (delta_temp * temp_temp_coeff >> 23)
171173

172174
# # Second order temperature compensation
173-
if (TEMP < 2000):
174-
T2 = (3 * (dT * dT)) >> 33
175-
OFF2 = 61 * (TEMP - 2000) * (TEMP - 2000) / 16
176-
SENS2 = 29 * (TEMP - 2000) * (TEMP - 2000) / 16
177-
178-
if (TEMP < -1500):
179-
OFF2 += 17 * (TEMP + 1500) * (TEMP + 1500)
180-
SENS2 += 9 * (TEMP + 1500) * (TEMP + 1500)
175+
if tmp_temp < 2000:
176+
t_sq = (3 * (delta_temp * delta_temp)) >> 33
177+
offset_sq = 61 * (tmp_temp - 2000) * (tmp_temp - 2000) / 16
178+
sens2 = 29 * (tmp_temp - 2000) * (tmp_temp - 2000) / 16
179+
180+
if tmp_temp < -1500:
181+
offset_sq += 17 * (tmp_temp + 1500) * (tmp_temp + 1500)
182+
sens2 += 9 * (tmp_temp + 1500) * (tmp_temp + 1500)
181183
#
182184
else:
183-
T2 = (5 * (dT * dT)) >> 38
184-
OFF2 = 0
185-
SENS2 = 0
185+
t_sq = (5 * (delta_temp * delta_temp)) >> 38
186+
offset_sq = 0
187+
sens2 = 0
186188
#
187-
temperature = (TEMP - T2) / 100
188-
return temperature
189-
# # OFF = OFF_T1 + TCO * dT
190-
# OFF = ((self.press_offset) << 17) +
191-
# ((self.press_offset_temp_coeff * dT) >> 6)
192-
# OFF -= OFF2
189+
# offset = OFF_T1 + TCO * delta_temp
190+
# this can be refactored into a coefficient applied to delta_temp
191+
offset = ((press_offset) << 17) + ((press_offset_temp_coeff * delta_temp) >> 6)
192+
offset -= offset_sq
193193

194-
# # Sensitivity at actual temperature = SENS_T1 + TCS * dT
195-
# SENS = (self.press_sens << 16) +((self.press_sens_temp_coeff * dT) >> 7)
196-
# SENS -= SENS2
194+
# Sensitivity at actual temperature = SENS_T1 + TCS * delta_temp
195+
# this can be refactored into a coefficient applied to delta_temp
196+
sensitivity = (press_sens << 16) + ((press_sens_temp_coeff * delta_temp) >> 7)
197+
sensitivity -= sens2
197198

198-
# # Temperature compensated pressure = D1 * SENS - OFF
199-
# P = (((raw_pressure * SENS) >> 21) - OFF) >> 15
199+
# Temperature compensated pressure = D1 * sensitivity - offset
200+
temp_compensated_pressure = (
201+
((raw_pressure * sensitivity) >> 21) - offset
202+
) >> 15
200203

201-
202-
# pressure = (float)P / 100
204+
self._temperature = (tmp_temp - t_sq) / 100
205+
self._pressure = temp_compensated_pressure / 100
203206

204207
# return status
208+
return (self._temperature, self._pressure)
205209

210+
@property
211+
def temperature(self):
212+
"""The current temperature in degrees Celcius"""
213+
return self._pressure_temperature[0]
206214

207-
208-
209-
215+
@property
216+
def pressure(self):
217+
"""The current barometric pressure in hPa"""
218+
return self._pressure_temperature[1]
210219

211220
@property
212221
def relative_humidity(self):
@@ -263,8 +272,7 @@ def _check_press_calibration_crc(calibration_int16s, crc):
263272
crc_read = calibration_int16s[0]
264273
calibration_int16s[7] = 0
265274
calibration_int16s[0] = 0x0FFF & (calibration_int16s[0]) # Clear the CRC byte
266-
for cal_int16 in calibration_int16s:
267-
print("0x{0:2X}, ".format(cal_int16), end="")
275+
268276
for cnt in range(16):
269277
# Get next byte
270278

@@ -283,7 +291,6 @@ def _check_press_calibration_crc(calibration_int16s, crc):
283291

284292
n_rem >>= 12
285293
calibration_int16s[0] = crc_read
286-
print("calculated CRC:", n_rem, "retrieved crc:", crc)
287294
return n_rem == crc
288295

289296

0 commit comments

Comments
 (0)