37
37
from micropython import const
38
38
import adafruit_bus_device .i2c_device as i2c_device
39
39
40
- # # HSENSOR device commands
41
- _MS8607_RESET_COMMAND = const (0xFE ) #
42
- _MS8607_READ_HUMIDITY_W_HOLD_COMMAND = const (0xE5 ) #
43
- _MS8607_READ_HUMIDITY_WO_HOLD_COMMAND = const (0xF5 ) #
44
- _MS8607_READ_SERIAL_FIRST_8BYTES_COMMAND = const (0xFA0F ) #
45
- _MS8607_READ_SERIAL_LAST_6BYTES_COMMAND = const (0xFCC9 ) #
46
-
47
- _MS8607_HUM_CMD_READ_USR = const (0xE7 )
48
- _MS8607_HUM_CMD_WRITE_USR = const (0xE6 )
49
-
50
- _MS8607_HUM_CMD_ENABLE_ONCHIP_HEATER_MASK = const (0x4 )
51
- _MS8607_PT_CALIB_ROM_ADDR = const (0xA0 ) # 16-bit registers through 0xAE
40
+ _MS8607_HSENSOR_ADDR = const (0x40 ) #
41
+ _MS8607_PTSENSOR_ADDR = const (0x76 ) #
52
42
53
- _MS8607_HUM_USR_RESOLUTION_MASK = const (0x81 )
54
43
55
- _MS8607_PTSENSOR_ADDR = const (0x76 ) #
56
- _MS8607_HSENSOR_ADDR = const (0x40 ) #
44
+ _MS8607_HUM_USR_REG_RESOLUTION_MASK = const (0x81 )
45
+ _MS8607_HUM_USR_REG_HEATER_EN_MASK = const (0x4 )
46
+ _MS8607_HUM_COEFF_MUL = const (125 ) #
47
+ _MS8607_HUM_COEFF_ADD = const (- 6 ) #
57
48
58
- _MS8607_COEFF_MUL = const (125 ) #
59
- _MS8607_COEFF_ADD = const (- 6 ) #
49
+ _MS8607_HUM_CMD_READ_HOLD = const (0xE5 ) #
50
+ _MS8607_HUM_CMD_READ_NO_HOLD = const (0xF5 ) #
51
+ _MS8607_HUM_CMD_READ_USR = const (0xE7 )
52
+ _MS8607_HUM_CMD_WRITE_USR = const (0xE6 )
53
+ _MS8607_HUM_CMD_RESET = const (0xFE ) #
60
54
61
55
62
- _MS8607_PT_CMD_RESET_COMMAND = const (0x1E ) # Command to reset pressure sensor
56
+ _MS8607_PT_CALIB_ROM_ADDR = const (0xA0 ) # 16-bit registers through 0xAE
57
+ _MS8607_PT_CMD_RESET = const (0x1E ) # Command to reset pressure sensor
63
58
_MS8607_PT_CMD_PRESS_START = const (0x40 ) # Command to start pressure ADC measurement
64
59
_MS8607_PT_CMD_TEMP_START = const (0x50 ) # Command to start temperature ADC measurement
65
60
_MS8607_PT_CMD_READ_ADC = const (0x00 ) # Temp and pressure ADC read command
@@ -133,24 +128,36 @@ def __init__(self, i2c_bus):
133
128
self .humidity_i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_HSENSOR_ADDR )
134
129
self .pressure_i2c_device = i2c_device .I2CDevice (i2c_bus , _MS8607_PTSENSOR_ADDR )
135
130
self ._buffer = bytearray (4 )
136
- self .reset ()
137
- self .initialize ()
138
- self .pressure_resolution = (
139
- PressureResolution .OSR_8192 # pylint:disable=no-member
140
- )
141
- self .humidity_resolution = (
142
- HumidityResolution .OSR_4096 # pylint:disable=no-member
143
- )
131
+ self ._calibration_constants = []
144
132
self ._pressure = None
145
133
self ._temperature = None
134
+ self .reset ()
135
+ self .initialize ()
146
136
147
137
def reset (self ):
148
138
"""Reset the sensor to an initial unconfigured state"""
139
+ self ._buffer [0 ] = _MS8607_HUM_CMD_RESET
140
+ with self .humidity_i2c_device as i2c :
141
+ i2c .write (self ._buffer , end = 1 )
142
+
143
+ sleep (0.015 )
144
+ self ._buffer [0 ] = _MS8607_PT_CMD_RESET
145
+ with self .pressure_i2c_device as i2c :
146
+ i2c .write (self ._buffer , end = 1 )
149
147
150
148
def initialize (self ):
151
149
"""Configure the sensors with the default settings and state.
152
150
For use after calling `reset()`
153
151
"""
152
+ self ._set_calibration_consts ()
153
+ self .pressure_resolution = (
154
+ PressureResolution .OSR_8192 # pylint:disable=no-member
155
+ )
156
+ self .humidity_resolution = (
157
+ HumidityResolution .OSR_4096 # pylint:disable=no-member
158
+ )
159
+
160
+ def _set_calibration_consts (self ):
154
161
constants = []
155
162
156
163
for i in range (7 ):
@@ -176,7 +183,8 @@ def initialize(self):
176
183
self ._calibration_constants = constants
177
184
178
185
@property
179
- def _pressure_temperature (self ):
186
+ def pressure_temperature (self ):
187
+ """Pressure and Temperature, measured at the same time"""
180
188
raw_temperature , raw_pressure = self ._read_temp_pressure ()
181
189
182
190
self ._scale_temp_pressure (raw_temperature , raw_pressure )
@@ -297,19 +305,19 @@ def _dt(self, raw_temperature):
297
305
@property
298
306
def temperature (self ):
299
307
"""The current temperature in degrees Celcius"""
300
- return self ._pressure_temperature [0 ]
308
+ return self .pressure_temperature [0 ]
301
309
302
310
@property
303
311
def pressure (self ):
304
312
"""The current barometric pressure in hPa"""
305
- return self ._pressure_temperature [1 ]
313
+ return self .pressure_temperature [1 ]
306
314
307
315
@property
308
316
def relative_humidity (self ):
309
317
"""The current relative humidity in % rH"""
310
318
311
319
# self._buffer.clear()
312
- self ._buffer [0 ] = _MS8607_READ_HUMIDITY_WO_HOLD_COMMAND
320
+ self ._buffer [0 ] = _MS8607_HUM_CMD_READ_NO_HOLD
313
321
with self .humidity_i2c_device as i2c :
314
322
i2c .write (self ._buffer , end = 1 )
315
323
sleep (0.016 ) # _i2cPort->requestFrom((uint8_t)MS8607_HSENSOR_ADDR, 3U)
@@ -319,7 +327,9 @@ def relative_humidity(self):
319
327
320
328
raw_humidity = unpack_from (">H" , self ._buffer )[0 ]
321
329
crc_value = unpack_from (">B" , self ._buffer , offset = 2 )[0 ]
322
- humidity = (raw_humidity * (_MS8607_COEFF_MUL / (1 << 16 ))) + _MS8607_COEFF_ADD
330
+ humidity = (
331
+ raw_humidity * (_MS8607_HUM_COEFF_MUL / (1 << 16 ))
332
+ ) + _MS8607_HUM_COEFF_ADD
323
333
if not self ._check_humidity_crc (raw_humidity , crc_value ):
324
334
raise RuntimeError ("CRC Error reading humidity data" )
325
335
return humidity
@@ -338,9 +348,9 @@ def humidity_resolution(self, resolution):
338
348
reg_value = self ._read_hum_user_register ()
339
349
340
350
# Clear the resolution bits
341
- reg_value &= ~ _MS8607_HUM_USR_RESOLUTION_MASK
351
+ reg_value &= ~ _MS8607_HUM_USR_REG_RESOLUTION_MASK
342
352
# and then set them to the new value
343
- reg_value |= resolution & _MS8607_HUM_USR_RESOLUTION_MASK
353
+ reg_value |= resolution & _MS8607_HUM_USR_REG_RESOLUTION_MASK
344
354
345
355
self ._set_hum_user_register (reg_value )
346
356
0 commit comments