45
45
_MS8607_READ_SERIAL_LAST_6BYTES_COMMAND = const (0xFCC9 ) #
46
46
_MS8607_WRITE_USER_REG_COMMAND = const (0xE6 ) #
47
47
_MS8607_READ_USER_REG_COMMAND = const (0xE7 ) #
48
+ _MS8607_PRESSURE_CALIB_ROM_ADDR = const (0xA0 ) # 16-bit registers through 0xAE
48
49
50
+
51
+ _MS8607_PTSENSOR_ADDR = const (0x76 ) #
49
52
_MS8607_HSENSOR_ADDR = const (0x40 ) #
50
53
_MS8607_COEFF_MUL = const (125 ) #
51
54
_MS8607_COEFF_ADD = const (- 6 ) #
@@ -69,23 +72,91 @@ class MS8607Humidity:
69
72
70
73
def __init__ (self , i2c_bus ):
71
74
72
- self .i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_HSENSOR_ADDR )
75
+ self .humidity_i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_HSENSOR_ADDR )
76
+ self .pressure_i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_PTSENSOR_ADDR )
73
77
74
- # self.reset()
75
- # self.initialize()
76
78
self ._buffer = bytearray (3 )
79
+ self .reset ()
80
+ self .initialize ()
81
+
82
+ def reset (self ):
83
+ """Reset the sensor to an initial unconfigured state"""
84
+
85
+ def initialize (self ):
86
+ """Configure the sensors with the default settings and state.
87
+ For use after calling `reset()`
88
+ """
89
+ constants = []
90
+
91
+ for i in range (7 ):
92
+ offset = 2 * i
93
+ self ._buffer [0 ] = _MS8607_PRESSURE_CALIB_ROM_ADDR + offset
94
+ with self .pressure_i2c_device as i2c :
95
+ i2c .write_then_readinto (
96
+ self ._buffer ,
97
+ self ._buffer ,
98
+ out_start = 0 ,
99
+ out_end = 1 ,
100
+ in_start = 0 ,
101
+ in_end = 2 ,
102
+ )
103
+
104
+ # print("got calibration constant %d(%s)"%(calibration_const, hex(calibration_const)))
105
+ constants .extend (unpack_from (">H" , self ._buffer [0 :2 ]))
106
+
107
+ crc_value = (constants [0 ] & 0xF000 ) >> 12
108
+ constants .append (0 )
109
+ if not self ._check_press_calibration_crc (constants , crc_value ):
110
+ raise RuntimeError ("CRC Error reading humidity calibration constants" )
111
+
112
+ self .press_sens = constants [1 ]
113
+ self .press_offset = constants [2 ]
114
+ self .press_sens_temp_coeff = constants [3 ]
115
+ self .press_offset_temp_coeff = constants [4 ]
116
+ self .ref_temp = constants [5 ]
117
+ 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
+
127
+ @property
128
+ def temperature (self ):
129
+ """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
77
148
78
149
@property
79
150
def relative_humidity (self ):
80
151
"""The current relative humidity in % rH"""
81
152
82
153
# self._buffer.clear()
83
154
self ._buffer [0 ] = _MS8607_READ_HUMIDITY_WO_HOLD_COMMAND
84
- with self .i2c_device as i2c :
155
+ with self .humidity_i2c_device as i2c :
85
156
i2c .write (self ._buffer , end = 1 )
86
157
sleep (0.1 ) # _i2cPort->requestFrom((uint8_t)MS8607_HSENSOR_ADDR, 3U)
87
158
88
- with self .i2c_device as i2c :
159
+ with self .humidity_i2c_device as i2c :
89
160
i2c .readinto (self ._buffer , end = 3 )
90
161
91
162
# _adc = (buffer[0] << 8) | buffer[1]
@@ -122,6 +193,35 @@ def _check_humidity_crc(value, crc):
122
193
return True
123
194
return False
124
195
196
+ @staticmethod
197
+ def _check_press_calibration_crc (calibration_int16s , crc ):
198
+ cnt = 0
199
+ n_rem = 0
200
+ n_rem = 0
201
+ crc_read = calibration_int16s [0 ]
202
+ calibration_int16s [7 ] = 0
203
+ calibration_int16s [0 ] = 0x0FFF & (calibration_int16s [0 ]) # Clear the CRC byte
204
+
205
+ for cnt in range (16 ):
206
+ # Get next byte
207
+
208
+ if cnt % 2 == 1 :
209
+ n_rem ^= calibration_int16s [cnt >> 1 ] & 0x00FF
210
+ else :
211
+ n_rem ^= calibration_int16s [cnt >> 1 ] >> 8
212
+
213
+ for _i in range (8 , 0 , - 1 ):
214
+ if n_rem & 0x8000 :
215
+ n_rem = (n_rem << 1 ) ^ 0x3000
216
+ else :
217
+ n_rem <<= 1
218
+ # we have to restrict to 16 bits
219
+ n_rem &= 0xFFFF
220
+
221
+ n_rem >>= 12
222
+ calibration_int16s [0 ] = crc_read
223
+ return n_rem == crc
224
+
125
225
126
226
# read_temperature_pressure_humidity(float *t, float *p,
127
227
0 commit comments