@@ -163,9 +163,6 @@ class HTS221: # pylint: disable=too-many-instance-attributes
163
163
_h0_t0_out = ROUnaryStruct (_H0_T0_OUT , "<h" )
164
164
_h1_t0_out = ROUnaryStruct (_H1_T1_OUT , "<h" )
165
165
166
-
167
-
168
-
169
166
def __init__ (self , i2c_bus , address = _HTS221_DEFAULT_ADDRESS ):
170
167
self .i2c_device = i2cdevice .I2CDevice (i2c_bus , address )
171
168
if not self ._chip_id in [_HTS221_CHIP_ID ]:
@@ -177,26 +174,26 @@ def __init__(self, i2c_bus, address=_HTS221_DEFAULT_ADDRESS):
177
174
self .data_rate = Rate .RATE_12_5_HZ # pylint:disable=no-member
178
175
179
176
t1_t0_msbs = self ._t1_t0_deg_c_x8_msbits
180
- self .calibrated_value_0 = self ._t0_deg_c_x8_lsbyte
181
- self .calibrated_value_0 |= (t1_t0_msbs & 0b0011 ) << 8
177
+ self .calib_temp_value_0 = self ._t0_deg_c_x8_lsbyte
178
+ self .calib_temp_value_0 |= (t1_t0_msbs & 0b0011 ) << 8
182
179
183
180
self .calibrated_value_1 = self ._t1_deg_c_x8_lsbyte
184
181
self .calibrated_value_1 |= (t1_t0_msbs & 0b1100 ) << 6
185
182
186
- self .calibrated_value_0 >>= 3
187
- self .calibrated_value_1 >>= 3
183
+ self .calib_temp_value_0 >>= 3 # divide by 8 to remove x8
184
+ self .calibrated_value_1 >>= 3 # divide by 8 to remove x8
188
185
189
- self .calibrated_measurement_0 = self ._t0_out
190
- self .calibrated_measurement_1 = self ._t1_out
191
- self .h0_rh = self ._h0_rh_x2
192
- self .h0_rh >>= 1
186
+ self .calib_temp_meas_0 = self ._t0_out
187
+ self .calib_temp_meas_1 = self ._t1_out
193
188
194
- self .h1_rh = self ._h1_rh_x2
195
- self .h1_rh >>= 1
189
+ self .calib_hum_value_0 = self ._h0_rh_x2
190
+ self .calib_hum_value_0 >>= 1 # divide by 2 to remove x2
196
191
197
- self .h0_out = self ._h0_t0_out
198
- self .h1_out = self . _h1_t0_out
192
+ self .calib_hum_value_1 = self ._h1_rh_x2
193
+ self .calib_hum_value_1 >>= 1 # divide by 2 to remove x2
199
194
195
+ self .calib_hum_meas_0 = self ._h0_t0_out
196
+ self .calib_hum_meas_1 = self ._h1_t0_out
200
197
201
198
def boot (self ):
202
199
"""Reset the sensor, restoring all configuration registers to their defaults"""
@@ -208,22 +205,30 @@ def boot(self):
208
205
@property
209
206
def humidity (self ):
210
207
"""The current humidity measurement in hPa"""
211
- adjusted_humidity = (self ._raw_humidity - self .h0_out ) * (self .h1_rh - self .h0_rh ) / (self .h1_out - self .h0_out ) + self .h0_rh
212
- return adjusted_humidity
208
+ calibrated_value_delta = self .calib_hum_value_1 - self .calib_hum_value_0
209
+ calibrated_measurement_delta = self .calib_hum_meas_1 - self .calib_hum_meas_0
210
+
211
+ calibration_value_offset = self .calib_hum_value_0
212
+ calibrated_measurement_offset = self .calib_hum_meas_0
213
+ zeroed_measured_humidity = self ._raw_humidity - calibrated_measurement_offset
214
+
215
+ correction_factor = calibrated_value_delta / calibrated_measurement_delta
216
+
217
+ adjusted_humidity = (
218
+ zeroed_measured_humidity * correction_factor + calibration_value_offset
219
+ )
213
220
221
+ return adjusted_humidity
214
222
215
223
@property
216
224
def temperature (self ):
217
225
"""The current temperature measurement in degrees C"""
218
226
219
- calibrated_value_delta = self .calibrated_value_1 - self .calibrated_value_0
220
- calibrated_measurement_delta = (
221
- self .calibrated_measurement_1 - self .calibrated_measurement_0
222
- )
223
-
224
- calibration_value_offset = self .calibrated_value_0
225
- calibrated_measurement_offset = self .calibrated_measurement_0
227
+ calibrated_value_delta = self .calibrated_value_1 - self .calib_temp_value_0
228
+ calibrated_measurement_delta = self .calib_temp_meas_1 - self .calib_temp_meas_0
226
229
230
+ calibration_value_offset = self .calib_temp_value_0
231
+ calibrated_measurement_offset = self .calib_temp_meas_0
227
232
zeroed_measured_temp = self ._raw_temperature - calibrated_measurement_offset
228
233
229
234
correction_factor = calibrated_value_delta / calibrated_measurement_delta
0 commit comments