@@ -28,6 +28,8 @@ def __init__(self, i2c, address=WSEN_PADS_I2C_DEFAULT_ADDR):
2828 """
2929 self .i2c = i2c
3030 self .address = address
31+ self ._temp_gain = 1.0
32+ self ._temp_offset = 0.0
3133
3234 # Wait for the sensor boot sequence after power-up.
3335 sleep_ms (BOOT_DELAY_MS )
@@ -269,7 +271,8 @@ def temperature(self):
269271 """
270272 Read and return temperature in degrees Celsius.
271273 """
272- return self .temperature_raw () * TEMPERATURE_C_PER_DIGIT
274+ factory = self .temperature_raw () * TEMPERATURE_C_PER_DIGIT
275+ return self ._temp_gain * factory + self ._temp_offset
273276
274277 def read (self ):
275278 """
@@ -285,7 +288,8 @@ def read(self):
285288 p_raw = self ._to_signed24 ((p_data [2 ] << 16 ) | (p_data [1 ] << 8 ) | p_data [0 ])
286289 t_data = self ._read_block (REG_DATA_T_L , 2 )
287290 t_raw = self ._to_signed16 ((t_data [1 ] << 8 ) | t_data [0 ])
288- return p_raw * PRESSURE_HPA_PER_DIGIT , t_raw * TEMPERATURE_C_PER_DIGIT
291+ t_c = self ._temp_gain * (t_raw * TEMPERATURE_C_PER_DIGIT ) + self ._temp_offset
292+ return p_raw * PRESSURE_HPA_PER_DIGIT , t_c
289293
290294 # ---------------------------------------------------------------------
291295 # One-shot mode
@@ -410,3 +414,34 @@ def disable_low_pass(self):
410414 current = self ._read_reg (REG_CTRL_1 )
411415 current &= ~ (CTRL1_EN_LPFP | CTRL1_LPFP_CFG )
412416 self ._write_reg (REG_CTRL_1 , current )
417+
418+ # ---------------------------------------------------------------------
419+ # Temperature calibration
420+ # ---------------------------------------------------------------------
421+
422+ def set_temp_offset (self , offset_c ):
423+ """Set a temperature offset in °C (gain remains 1.0).
424+
425+ Args:
426+ offset_c: offset value in degrees Celsius.
427+ """
428+ self ._temp_gain = 1.0
429+ self ._temp_offset = float (offset_c )
430+
431+ def calibrate_temperature (self , ref_low , measured_low , ref_high , measured_high ):
432+ """Two-point calibration from reference measurements.
433+
434+ Computes gain and offset so that the sensor reading is adjusted
435+ to match reference values at two temperature points.
436+
437+ Args:
438+ ref_low: reference temperature at the low point (°C).
439+ measured_low: sensor reading at the low point (°C).
440+ ref_high: reference temperature at the high point (°C).
441+ measured_high: sensor reading at the high point (°C).
442+ """
443+ delta = float (measured_high - measured_low )
444+ if delta == 0.0 :
445+ raise ValueError ("measured_low and measured_high must differ" )
446+ self ._temp_gain = float (ref_high - ref_low ) / delta
447+ self ._temp_offset = float (ref_low ) - self ._temp_gain * float (measured_low )
0 commit comments