54
54
_MS8607_COEFF_ADD = const (- 6 ) #
55
55
56
56
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
61
61
# enum MS8607_humidity_resolution
62
62
#:
63
63
# MS8607_humidity_resolution_12b = 0,
@@ -83,7 +83,8 @@ def __init__(self, i2c_bus):
83
83
self .reset ()
84
84
self .initialize ()
85
85
self ._psensor_resolution_osr = 5
86
-
86
+ self ._pressure = None
87
+ self ._temperature = None
87
88
88
89
def reset (self ):
89
90
"""Reset the sensor to an initial unconfigured state"""
@@ -107,27 +108,24 @@ def initialize(self):
107
108
in_end = 2 ,
108
109
)
109
110
110
- # print("got calibration constant %d(%s)"%(calibration_const, hex(calibration_const)))
111
111
constants .extend (unpack_from (">H" , self ._buffer [0 :2 ]))
112
112
113
113
crc_value = (constants [0 ] & 0xF000 ) >> 12
114
114
constants .append (0 )
115
115
if not self ._check_press_calibration_crc (constants , crc_value ):
116
116
raise RuntimeError ("CRC Error reading humidity calibration constants" )
117
117
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
124
119
125
120
@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
+ #################
131
129
# First read temperature
132
130
cmd = self ._psensor_resolution_osr * 2
133
131
cmd |= _MS8607_PT_CMD_TEMP_START
@@ -139,13 +137,14 @@ def temperature(self):
139
137
140
138
self ._buffer [0 ] = _MS8607_PT_CMD_READ_ADC
141
139
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
+ )
143
143
144
144
# temp is only 24 bits but unpack wants 4 bytes so add a forth byte
145
145
self ._buffer [0 ] = 0
146
146
raw_temperature = unpack_from (">I" , self ._buffer )[0 ]
147
-
148
-
147
+ #################
149
148
150
149
# next read pressure
151
150
cmd = self ._psensor_resolution_osr * 2
@@ -158,55 +157,65 @@ def temperature(self):
158
157
159
158
self ._buffer [0 ] = _MS8607_PT_CMD_READ_ADC
160
159
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
+ )
162
163
# temp is only 24 bits but unpack wants 4 bytes so add a forth byte
163
164
self ._buffer [0 ] = 0
165
+
164
166
raw_pressure = unpack_from (">I" , self ._buffer )[0 ]
165
- raw_pressure >>= 8
167
+ #################
166
168
167
- dT = raw_temperature - (self . ref_temp << 8 )
169
+ delta_temp = raw_temperature - (ref_temp << 8 )
168
170
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 )
171
173
172
174
# # 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 )
181
183
#
182
184
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
186
188
#
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
193
193
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
197
198
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
200
203
201
-
202
- # pressure = (float)P / 100
204
+ self . _temperature = ( tmp_temp - t_sq ) / 100
205
+ self . _pressure = temp_compensated_pressure / 100
203
206
204
207
# return status
208
+ return (self ._temperature , self ._pressure )
205
209
210
+ @property
211
+ def temperature (self ):
212
+ """The current temperature in degrees Celcius"""
213
+ return self ._pressure_temperature [0 ]
206
214
207
-
208
-
209
-
215
+ @property
216
+ def pressure (self ):
217
+ """The current barometric pressure in hPa"""
218
+ return self ._pressure_temperature [1 ]
210
219
211
220
@property
212
221
def relative_humidity (self ):
@@ -263,8 +272,7 @@ def _check_press_calibration_crc(calibration_int16s, crc):
263
272
crc_read = calibration_int16s [0 ]
264
273
calibration_int16s [7 ] = 0
265
274
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
+
268
276
for cnt in range (16 ):
269
277
# Get next byte
270
278
@@ -283,7 +291,6 @@ def _check_press_calibration_crc(calibration_int16s, crc):
283
291
284
292
n_rem >>= 12
285
293
calibration_int16s [0 ] = crc_read
286
- print ("calculated CRC:" , n_rem , "retrieved crc:" , crc )
287
294
return n_rem == crc
288
295
289
296
0 commit comments