Skip to content

Commit 81f9588

Browse files
authored
fix(steami_config): Simplify calibrate_temperature for frozen drivers. (#249)
* fix(steami_config): Simplify calibrate_temperature for frozen drivers. * fix(steami_config): Use fresh sensor instances for verification. * fix(steami_config): Add warm-up delay for LIS2MDL temperature read.
1 parent 79c609a commit 81f9588

1 file changed

Lines changed: 38 additions & 40 deletions

File tree

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,69 @@
11
"""Calibrate all temperature sensors against WSEN-HIDS reference.
22
3-
This example reads each sensor one at a time to stay within RAM
4-
limits on the STM32WB55. Calibration offsets are stored in the
5-
persistent config zone and survive power cycles.
3+
Reads the WSEN-HIDS sensor as reference, then computes an offset for
4+
each other sensor. Calibration offsets are stored in the persistent
5+
config zone and survive power cycles.
6+
7+
Note: this example assumes the drivers are frozen into the firmware.
8+
Use ``make firmware && make deploy`` to build a firmware with the
9+
latest drivers before running this script.
610
"""
711

8-
import gc
9-
import sys
1012
from time import sleep_ms
1113

12-
from daplink_flash.device import DaplinkFlash
14+
from daplink_flash import DaplinkFlash
15+
from hts221 import HTS221
16+
from ism330dl import ISM330DL
17+
from lis2mdl import LIS2MDL
1318
from machine import I2C
14-
from steami_config.device import SteamiConfig
15-
from wsen_hids.device import WSEN_HIDS
19+
from steami_config import SteamiConfig
20+
from wsen_hids import WSEN_HIDS
21+
from wsen_pads import WSEN_PADS
1622

1723
i2c = I2C(1)
1824
flash = DaplinkFlash(i2c)
1925
config = SteamiConfig(flash)
2026
config.load()
2127

2228
# Read reference temperature from WSEN-HIDS (most accurate at ambient)
23-
ref_temp = WSEN_HIDS(i2c).temperature()
29+
ref = WSEN_HIDS(i2c)
30+
ref_temp = ref.temperature()
2431
print("Reference (WSEN-HIDS): {:.2f} C".format(ref_temp))
2532
config.set_temperature_calibration("wsen_hids", gain=1.0, offset=0.0)
26-
del WSEN_HIDS
27-
sys.modules.pop("wsen_hids.device", None)
28-
gc.collect()
2933

30-
# Calibrate each sensor one at a time to save RAM
31-
SENSORS = [
32-
("hts221", "hts221.device", "HTS221", "temperature"),
33-
("wsen_pads", "wsen_pads.device", "WSEN_PADS", "temperature"),
34-
("lis2mdl", "lis2mdl.device", "LIS2MDL", "temperature"),
35-
("ism330dl", "ism330dl.device", "ISM330DL", "temperature"),
34+
# Calibrate each sensor
35+
sensors = [
36+
("hts221", HTS221(i2c)),
37+
("wsen_pads", WSEN_PADS(i2c)),
38+
("lis2mdl", LIS2MDL(i2c)),
39+
("ism330dl", ISM330DL(i2c)),
3640
]
3741

38-
for config_name, module, class_name, method in SENSORS:
39-
mod = __import__(module, None, None, [class_name])
40-
cls = getattr(mod, class_name)
41-
sensor = cls(i2c)
42-
if config_name == "ism330dl":
42+
for name, sensor in sensors:
43+
if name in ("ism330dl", "lis2mdl"):
4344
sleep_ms(200)
44-
raw = getattr(sensor, method)()
45+
raw = sensor.temperature()
4546
offset = ref_temp - raw
46-
config.set_temperature_calibration(config_name, gain=1.0, offset=offset)
47-
print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(config_name, raw, offset))
48-
del sensor, cls, mod
49-
sys.modules.pop(module, None)
50-
gc.collect()
47+
config.set_temperature_calibration(name, gain=1.0, offset=offset)
48+
print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(name, raw, offset))
5149

5250
config.save()
5351
print("\nCalibration saved.")
5452

55-
# Verify by reloading
56-
gc.collect()
53+
# Verify by reloading with fresh sensor instances (simulates a reboot)
5754
config2 = SteamiConfig(flash)
5855
config2.load()
5956

57+
verify_sensors = [
58+
("hts221", HTS221(i2c)),
59+
("wsen_pads", WSEN_PADS(i2c)),
60+
("lis2mdl", LIS2MDL(i2c)),
61+
("ism330dl", ISM330DL(i2c)),
62+
]
63+
6064
print("\nVerification (after reload):")
61-
for config_name, module, class_name, method in SENSORS:
62-
mod = __import__(module, None, None, [class_name])
63-
cls = getattr(mod, class_name)
64-
sensor = cls(i2c)
65-
if config_name == "ism330dl":
65+
for name, sensor in verify_sensors:
66+
if name in ("ism330dl", "lis2mdl"):
6667
sleep_ms(200)
6768
config2.apply_temperature_calibration(sensor)
68-
print(" {:10s}: {:6.2f} C".format(config_name, getattr(sensor, method)()))
69-
del sensor, cls, mod
70-
sys.modules.pop(module, None)
71-
gc.collect()
69+
print(" {:10s}: {:6.2f} C".format(name, sensor.temperature()))

0 commit comments

Comments
 (0)