Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion lib/wsen-hids/wsen_hids/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def __init__(
self._buffer_1 = bytearray(1)

self._calibration = {}
self._temp_gain = 1.0
self._temp_offset = 0.0

if check_device:
self.check_device()
Expand Down Expand Up @@ -257,7 +259,39 @@ def _convert_temperature(self, t_raw):
if delta_out == 0:
raise WSENHIDSError("Invalid temperature calibration data")

return ((t1_degC - t0_degC) * (t_raw - t0_out) / delta_out) + t0_degC
factory = ((t1_degC - t0_degC) * (t_raw - t0_out) / delta_out) + t0_degC
return self._temp_gain * factory + self._temp_offset

# -------------------------------------------------------------------------
# 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)

# -------------------------------------------------------------------------
# Public measurement API
Expand Down
7 changes: 7 additions & 0 deletions tests/runner/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ def run_action(action, driver_instance):
method = getattr(driver_instance, method_name)
return method(*args)

if action_type == "script":
script_vars = {"dev": driver_instance, "i2c": driver_instance.i2c}
exec(action["script"], script_vars, script_vars)
if "result" not in script_vars:
raise ValueError("Script must set a 'result' variable")
return script_vars["result"]

if action_type == "hardware_script":
# hardware_script is hardware-only; skip in mock mode
return None
Expand Down
16 changes: 16 additions & 0 deletions tests/scenarios/wsen_hids.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ tests:
expect_range: [24.0, 26.0]
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: "Humidity in plausible range"
action: call
method: humidity
Expand Down
15 changes: 15 additions & 0 deletions tests/test_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ def test_scenario(scenario, test, mode, port):
print(f" {label}: {value} {unit}")
prompt = test.get("prompt", "Manual check")
result = prompt_yes_no(prompt)
elif action == "script":
if is_board:
pytest.fail(
"Board scenarios do not support 'script' action; "
"use 'hardware_script' instead"
)
result = bridge.run_script(
scenario["driver"],
scenario["driver_class"],
scenario["i2c"],
test["script"],
module_name=scenario.get("module_name"),
hardware_init=scenario.get("hardware_init"),
i2c_address=scenario.get("i2c_address"),
)
elif action in ("call", "read_register", "interactive"):
if is_board:
pytest.fail(
Expand Down
Loading