diff --git a/lib/wsen-pads/wsen_pads/device.py b/lib/wsen-pads/wsen_pads/device.py index 19b81306..f83ae405 100644 --- a/lib/wsen-pads/wsen_pads/device.py +++ b/lib/wsen-pads/wsen_pads/device.py @@ -28,6 +28,8 @@ def __init__(self, i2c, address=WSEN_PADS_I2C_DEFAULT_ADDR): """ self.i2c = i2c self.address = address + self._temp_gain = 1.0 + self._temp_offset = 0.0 # Wait for the sensor boot sequence after power-up. sleep_ms(BOOT_DELAY_MS) @@ -269,7 +271,8 @@ def temperature(self): """ Read and return temperature in degrees Celsius. """ - return self.temperature_raw() * TEMPERATURE_C_PER_DIGIT + factory = self.temperature_raw() * TEMPERATURE_C_PER_DIGIT + return self._temp_gain * factory + self._temp_offset def read(self): """ @@ -285,7 +288,8 @@ def read(self): p_raw = self._to_signed24((p_data[2] << 16) | (p_data[1] << 8) | p_data[0]) t_data = self._read_block(REG_DATA_T_L, 2) t_raw = self._to_signed16((t_data[1] << 8) | t_data[0]) - return p_raw * PRESSURE_HPA_PER_DIGIT, t_raw * TEMPERATURE_C_PER_DIGIT + t_c = self._temp_gain * (t_raw * TEMPERATURE_C_PER_DIGIT) + self._temp_offset + return p_raw * PRESSURE_HPA_PER_DIGIT, t_c # --------------------------------------------------------------------- # One-shot mode @@ -410,3 +414,34 @@ def disable_low_pass(self): current = self._read_reg(REG_CTRL_1) current &= ~(CTRL1_EN_LPFP | CTRL1_LPFP_CFG) self._write_reg(REG_CTRL_1, current) + + # --------------------------------------------------------------------- + # Temperature calibration + # --------------------------------------------------------------------- + + def set_temp_offset(self, offset_c): + """Set a temperature offset in °C (gain remains 1.0). + + Args: + offset_c: offset value in degrees Celsius. + """ + self._temp_gain = 1.0 + self._temp_offset = float(offset_c) + + def calibrate_temperature(self, ref_low, measured_low, ref_high, measured_high): + """Two-point calibration from reference measurements. + + Computes gain and offset so that the sensor reading is adjusted + to match reference values at two temperature points. + + Args: + ref_low: reference temperature at the low point (°C). + measured_low: sensor reading at the low point (°C). + ref_high: reference temperature at the high point (°C). + measured_high: sensor reading at the high point (°C). + """ + delta = float(measured_high - measured_low) + if delta == 0.0: + raise ValueError("measured_low and measured_high must differ") + self._temp_gain = float(ref_high - ref_low) / delta + self._temp_offset = float(ref_low) - self._temp_gain * float(measured_low) diff --git a/tests/scenarios/wsen_pads.yaml b/tests/scenarios/wsen_pads.yaml index cf70c695..b2c537c2 100644 --- a/tests/scenarios/wsen_pads.yaml +++ b/tests/scenarios/wsen_pads.yaml @@ -49,13 +49,38 @@ tests: - name: "Read pressure returns float" action: call method: pressure - expect_not_none: true + expect_range: [1013.0, 1014.0] mode: [mock] - name: "Read temperature returns float" action: call method: temperature - expect_not_none: true + expect_range: [24.9, 25.1] + mode: [mock] + + - name: "Temperature with offset" + action: script + script: | + dev.set_temp_offset(-2.0) + result = dev.temperature() + expect_range: [22.0, 24.0] + mode: [mock] + + - name: "Temperature with two-point calibration" + action: script + script: | + dev.calibrate_temperature(20.0, 25.0, 30.0, 35.0) + result = dev.temperature() + expect_range: [19.0, 21.0] + mode: [mock] + + - name: "Read applies temperature calibration" + action: script + script: | + dev.set_temp_offset(-2.0) + _, temp = dev.read() + result = temp + expect_range: [22.0, 24.0] mode: [mock] - name: "Pressure in plausible range"