53
53
_MS8607_COEFF_MUL = const (125 ) #
54
54
_MS8607_COEFF_ADD = const (- 6 ) #
55
55
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
56
61
# enum MS8607_humidity_resolution
57
- # {
62
+ #:
58
63
# MS8607_humidity_resolution_12b = 0,
59
64
# MS8607_humidity_resolution_8b,
60
65
# MS8607_humidity_resolution_10b,
61
66
# MS8607_humidity_resolution_11b
62
- # } ]
67
+ # ]
63
68
64
69
65
70
class MS8607Humidity :
@@ -74,10 +79,11 @@ def __init__(self, i2c_bus):
74
79
75
80
self .humidity_i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_HSENSOR_ADDR )
76
81
self .pressure_i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_PTSENSOR_ADDR )
77
-
78
- self ._buffer = bytearray (3 )
82
+ self ._buffer = bytearray (4 )
79
83
self .reset ()
80
84
self .initialize ()
85
+ self ._psensor_resolution_osr = 5
86
+
81
87
82
88
def reset (self ):
83
89
"""Reset the sensor to an initial unconfigured state"""
@@ -115,36 +121,92 @@ def initialize(self):
115
121
self .press_offset_temp_coeff = constants [4 ]
116
122
self .ref_temp = constants [5 ]
117
123
self .temp_temp_coeff = constants [6 ]
118
- # psensor_resolution_osr = MS8607_PRESSURE_RESOLUTION_OSR_8192
119
- print ("self.press_sens" , self .press_sens )
120
- print ("self.press_offset" , self .press_offset )
121
-
122
- print ("self.press_sens_temp_coeff" , self .press_sens_temp_coeff )
123
- print ("self.press_offset_temp_coeff" , self .press_offset_temp_coeff )
124
- print ("self.ref_temp" , self .ref_temp )
125
- print ("self.temp_temp_coeff" , self .temp_temp_coeff )
126
124
127
125
@property
128
126
def temperature (self ):
129
127
"""The current temperature in degrees Celcius"""
130
- return 5
131
- # # self._buffer.clear()
132
- # self._buffer[0] = _MS8607_READ_HUMIDITY_WO_HOLD_COMMAND
133
- # with self.i2c_device as i2c:
134
- # i2c.write(self._buffer, end=1)
135
- # sleep(0.1) # _i2cPort->requestFrom((uint8_t)MS8607_HSENSOR_ADDR, 3U)
136
-
137
- # with self.i2c_device as i2c:
138
- # i2c.readinto(self._buffer, end=3)
139
-
140
- # # _adc = (buffer[0] << 8) | buffer[1]
141
- # # crc = buffer[2]
142
- # raw_humidity = unpack_from(">H", self._buffer)[0]
143
- # crc_value = unpack_from(">B", self._buffer, offset=2)[0]
144
- # humidity = (raw_humidity * (_MS8607_COEFF_MUL / (1 << 16))) + _MS8607_COEFF_ADD
145
- # if not self._check_humidity_crc(raw_humidity, crc_value):
146
- # raise RuntimeError("CRC Error reading humidity data")
147
- # return humidity
128
+
129
+ dT , TEMP , OFF , SENS , P , T2 , OFF2 , SENS2 , raw_temp , raw_pressure = (0 ,)* 10
130
+
131
+ # First read temperature
132
+ cmd = self ._psensor_resolution_osr * 2
133
+ cmd |= _MS8607_PT_CMD_TEMP_START
134
+ self ._buffer [0 ] = cmd
135
+ with self .pressure_i2c_device as i2c :
136
+ i2c .write (self ._buffer , end = 1 )
137
+
138
+ sleep (0.018 )
139
+
140
+ self ._buffer [0 ] = _MS8607_PT_CMD_READ_ADC
141
+ 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 )
143
+
144
+ # temp is only 24 bits but unpack wants 4 bytes so add a forth byte
145
+ self ._buffer [0 ] = 0
146
+ raw_temperature = unpack_from (">I" , self ._buffer )[0 ]
147
+
148
+
149
+
150
+ # next read pressure
151
+ cmd = self ._psensor_resolution_osr * 2
152
+ cmd |= _MS8607_PT_CMD_PRESS_START
153
+ self ._buffer [0 ] = cmd
154
+ with self .pressure_i2c_device as i2c :
155
+ i2c .write (self ._buffer , end = 1 )
156
+
157
+ sleep (0.18 )
158
+
159
+ self ._buffer [0 ] = _MS8607_PT_CMD_READ_ADC
160
+ 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 )
162
+ # temp is only 24 bits but unpack wants 4 bytes so add a forth byte
163
+ self ._buffer [0 ] = 0
164
+ raw_pressure = unpack_from (">I" , self ._buffer )[0 ]
165
+ raw_pressure >>= 8
166
+
167
+ dT = raw_temperature - (self .ref_temp << 8 )
168
+
169
+ # # Actual temperature = 2000 + dT * TEMPSENS
170
+ TEMP = 2000 + (dT * self .temp_temp_coeff >> 23 )
171
+
172
+ # # 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 )
181
+ #
182
+ else :
183
+ T2 = (5 * (dT * dT )) >> 38
184
+ OFF2 = 0
185
+ SENS2 = 0
186
+ #
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
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
197
+
198
+ # # Temperature compensated pressure = D1 * SENS - OFF
199
+ # P = (((raw_pressure * SENS) >> 21) - OFF) >> 15
200
+
201
+
202
+ # pressure = (float)P / 100
203
+
204
+ # return status
205
+
206
+
207
+
208
+
209
+
148
210
149
211
@property
150
212
def relative_humidity (self ):
@@ -201,7 +263,8 @@ def _check_press_calibration_crc(calibration_int16s, crc):
201
263
crc_read = calibration_int16s [0 ]
202
264
calibration_int16s [7 ] = 0
203
265
calibration_int16s [0 ] = 0x0FFF & (calibration_int16s [0 ]) # Clear the CRC byte
204
-
266
+ for cal_int16 in calibration_int16s :
267
+ print ("0x{0:2X}, " .format (cal_int16 ), end = "" )
205
268
for cnt in range (16 ):
206
269
# Get next byte
207
270
@@ -220,6 +283,7 @@ def _check_press_calibration_crc(calibration_int16s, crc):
220
283
221
284
n_rem >>= 12
222
285
calibration_int16s [0 ] = crc_read
286
+ print ("calculated CRC:" , n_rem , "retrieved crc:" , crc )
223
287
return n_rem == crc
224
288
225
289
0 commit comments