From 6e5a57677281a5559d83407dbc2e393a1ed0c85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20NEDJAR?= Date: Thu, 26 Mar 2026 08:44:36 +0100 Subject: [PATCH 1/3] fix(steami_config): Simplify calibrate_temperature for frozen drivers. --- .../examples/calibrate_temperature.py | 69 ++++++++----------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/lib/steami_config/examples/calibrate_temperature.py b/lib/steami_config/examples/calibrate_temperature.py index 89ef1471..da50dcf7 100644 --- a/lib/steami_config/examples/calibrate_temperature.py +++ b/lib/steami_config/examples/calibrate_temperature.py @@ -1,18 +1,24 @@ """Calibrate all temperature sensors against WSEN-HIDS reference. -This example reads each sensor one at a time to stay within RAM -limits on the STM32WB55. Calibration offsets are stored in the -persistent config zone and survive power cycles. +Reads the WSEN-HIDS sensor as reference, then computes an offset for +each other sensor. Calibration offsets are stored in the persistent +config zone and survive power cycles. + +Note: this example assumes the drivers are frozen into the firmware. +Use ``make firmware && make deploy`` to build a firmware with the +latest drivers before running this script. """ -import gc -import sys from time import sleep_ms -from daplink_flash.device import DaplinkFlash +from daplink_flash import DaplinkFlash +from hts221 import HTS221 +from ism330dl import ISM330DL +from lis2mdl import LIS2MDL from machine import I2C -from steami_config.device import SteamiConfig -from wsen_hids.device import WSEN_HIDS +from steami_config import SteamiConfig +from wsen_hids import WSEN_HIDS +from wsen_pads import WSEN_PADS i2c = I2C(1) flash = DaplinkFlash(i2c) @@ -20,52 +26,35 @@ config.load() # Read reference temperature from WSEN-HIDS (most accurate at ambient) -ref_temp = WSEN_HIDS(i2c).temperature() +ref = WSEN_HIDS(i2c) +ref_temp = ref.temperature() print("Reference (WSEN-HIDS): {:.2f} C".format(ref_temp)) config.set_temperature_calibration("wsen_hids", gain=1.0, offset=0.0) -del WSEN_HIDS -sys.modules.pop("wsen_hids.device", None) -gc.collect() -# Calibrate each sensor one at a time to save RAM -SENSORS = [ - ("hts221", "hts221.device", "HTS221", "temperature"), - ("wsen_pads", "wsen_pads.device", "WSEN_PADS", "temperature"), - ("lis2mdl", "lis2mdl.device", "LIS2MDL", "temperature"), - ("ism330dl", "ism330dl.device", "ISM330DL", "temperature"), +# Calibrate each sensor +sensors = [ + ("hts221", HTS221(i2c)), + ("wsen_pads", WSEN_PADS(i2c)), + ("lis2mdl", LIS2MDL(i2c)), + ("ism330dl", ISM330DL(i2c)), ] -for config_name, module, class_name, method in SENSORS: - mod = __import__(module, None, None, [class_name]) - cls = getattr(mod, class_name) - sensor = cls(i2c) - if config_name == "ism330dl": +for name, sensor in sensors: + if name == "ism330dl": sleep_ms(200) - raw = getattr(sensor, method)() + raw = sensor.temperature() offset = ref_temp - raw - config.set_temperature_calibration(config_name, gain=1.0, offset=offset) - print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(config_name, raw, offset)) - del sensor, cls, mod - sys.modules.pop(module, None) - gc.collect() + config.set_temperature_calibration(name, gain=1.0, offset=offset) + print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(name, raw, offset)) config.save() print("\nCalibration saved.") # Verify by reloading -gc.collect() config2 = SteamiConfig(flash) config2.load() print("\nVerification (after reload):") -for config_name, module, class_name, method in SENSORS: - mod = __import__(module, None, None, [class_name]) - cls = getattr(mod, class_name) - sensor = cls(i2c) - if config_name == "ism330dl": - sleep_ms(200) +for name, sensor in sensors: config2.apply_temperature_calibration(sensor) - print(" {:10s}: {:6.2f} C".format(config_name, getattr(sensor, method)())) - del sensor, cls, mod - sys.modules.pop(module, None) - gc.collect() + print(" {:10s}: {:6.2f} C".format(name, sensor.temperature())) From 1836fb482688ce1931bdd902f6cf6a3b91bccada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20NEDJAR?= Date: Thu, 26 Mar 2026 09:20:19 +0100 Subject: [PATCH 2/3] fix(steami_config): Use fresh sensor instances for verification. --- lib/steami_config/examples/calibrate_temperature.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/steami_config/examples/calibrate_temperature.py b/lib/steami_config/examples/calibrate_temperature.py index da50dcf7..e462e687 100644 --- a/lib/steami_config/examples/calibrate_temperature.py +++ b/lib/steami_config/examples/calibrate_temperature.py @@ -50,11 +50,20 @@ config.save() print("\nCalibration saved.") -# Verify by reloading +# Verify by reloading with fresh sensor instances (simulates a reboot) config2 = SteamiConfig(flash) config2.load() +verify_sensors = [ + ("hts221", HTS221(i2c)), + ("wsen_pads", WSEN_PADS(i2c)), + ("lis2mdl", LIS2MDL(i2c)), + ("ism330dl", ISM330DL(i2c)), +] + print("\nVerification (after reload):") -for name, sensor in sensors: +for name, sensor in verify_sensors: + if name == "ism330dl": + sleep_ms(200) config2.apply_temperature_calibration(sensor) print(" {:10s}: {:6.2f} C".format(name, sensor.temperature())) From aa7493fd7528e52fa6e0d477cb1345f28f4447cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20NEDJAR?= Date: Thu, 26 Mar 2026 09:24:24 +0100 Subject: [PATCH 3/3] fix(steami_config): Add warm-up delay for LIS2MDL temperature read. --- lib/steami_config/examples/calibrate_temperature.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/steami_config/examples/calibrate_temperature.py b/lib/steami_config/examples/calibrate_temperature.py index e462e687..1e2bd8a7 100644 --- a/lib/steami_config/examples/calibrate_temperature.py +++ b/lib/steami_config/examples/calibrate_temperature.py @@ -40,7 +40,7 @@ ] for name, sensor in sensors: - if name == "ism330dl": + if name in ("ism330dl", "lis2mdl"): sleep_ms(200) raw = sensor.temperature() offset = ref_temp - raw @@ -63,7 +63,7 @@ print("\nVerification (after reload):") for name, sensor in verify_sensors: - if name == "ism330dl": + if name in ("ism330dl", "lis2mdl"): sleep_ms(200) config2.apply_temperature_calibration(sensor) print(" {:10s}: {:6.2f} C".format(name, sensor.temperature()))