Skip to content

Commit a96fd64

Browse files
committed
steami_config: Address PR #176 review comments.
1 parent 0bf66de commit a96fd64

4 files changed

Lines changed: 29 additions & 10 deletions

File tree

lib/steami_config/examples/calibrate_temperature.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import gc
9+
import sys
910
from machine import I2C
1011
from time import sleep_ms
1112

@@ -23,6 +24,7 @@
2324
print("Reference (WSEN-HIDS): {:.2f} C".format(ref_temp))
2425
config.set_temperature_calibration("wsen_hids", gain=1.0, offset=0.0)
2526
del WSEN_HIDS
27+
sys.modules.pop("wsen_hids.device", None)
2628
gc.collect()
2729

2830
# Calibrate each sensor one at a time to save RAM
@@ -44,6 +46,7 @@
4446
config.set_temperature_calibration(config_name, gain=1.0, offset=offset)
4547
print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(config_name, raw, offset))
4648
del sensor, cls, mod
49+
sys.modules.pop(module, None)
4750
gc.collect()
4851

4952
config.save()
@@ -64,4 +67,5 @@
6467
config2.apply_temperature_calibration(sensor)
6568
print(" {:10s}: {:6.2f} C".format(config_name, getattr(sensor, method)()))
6669
del sensor, cls, mod
70+
sys.modules.pop(module, None)
6771
gc.collect()

lib/steami_config/steami_config/device.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ def __init__(self, flash):
3333
# --------------------------------------------------
3434

3535
def load(self):
36-
"""Load configuration from the config zone."""
36+
"""Load configuration from the config zone.
37+
38+
Falls back to empty config if the zone contains invalid JSON.
39+
"""
3740
raw = self._flash.read_config()
3841
if raw:
39-
self._data = json.loads(raw)
42+
try:
43+
self._data = json.loads(raw)
44+
except (ValueError, TypeError):
45+
self._data = {}
4046
else:
4147
self._data = {}
4248

@@ -56,7 +62,10 @@ def board_revision(self):
5662

5763
@board_revision.setter
5864
def board_revision(self, value):
59-
self._data["rev"] = int(value)
65+
if value is None:
66+
self._data.pop("rev", None)
67+
else:
68+
self._data["rev"] = int(value)
6069

6170
@property
6271
def board_name(self):
@@ -65,7 +74,10 @@ def board_name(self):
6574

6675
@board_name.setter
6776
def board_name(self, value):
68-
self._data["name"] = str(value)
77+
if value is None:
78+
self._data.pop("name", None)
79+
else:
80+
self._data["name"] = str(value)
6981

7082
# --------------------------------------------------
7183
# Temperature calibration
@@ -116,6 +128,8 @@ def apply_temperature_calibration(self, sensor_instance):
116128
sensor_instance: a driver instance (e.g. ``HTS221``).
117129
"""
118130
class_name = type(sensor_instance).__name__.lower()
131+
if class_name not in _SENSOR_KEYS:
132+
return
119133
cal = self.get_temperature_calibration(class_name)
120134
if cal is None:
121135
return

tests/runner/mpremote_bridge.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ def run_script(
116116
117117
The script has access to ``i2c`` and ``dev`` variables and must
118118
set a ``result`` variable. The method returns the JSON-decoded
119-
value of ``result``.
120-
121-
The script must not print anything: any additional output on
122-
stdout will cause JSON parsing to fail.
119+
value of ``result`` (only the last line of stdout is parsed).
123120
124121
When ``hardware_init`` is provided it takes precedence over
125122
``i2c_address`` for device construction.
@@ -143,9 +140,9 @@ def run_script(
143140
if mount_dir is None:
144141
mount_dir = self._driver_dir(driver_name)
145142
else:
146-
# When mounting lib/, add sub-paths for each dependency
143+
# When mounting lib/, add sub-paths for all driver directories
147144
extra_paths = "import sys\n"
148-
for child in mount_dir.iterdir():
145+
for child in sorted(mount_dir.iterdir()):
149146
if child.is_dir() and not child.name.startswith("."):
150147
extra_paths += f"sys.path.insert(0, '/remote/{child.name}')\n"
151148
code = extra_paths + code

tests/test_scenarios.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def make_mock_instance(scenario):
7474
ns = {"i2c": fake_i2c}
7575
ns.update(vars(driver_module))
7676
exec(mock_init, ns)
77+
if "dev" not in ns:
78+
raise ValueError(
79+
f"mock_init for '{driver_name}' must define a 'dev' variable"
80+
)
7781
return ns["dev"], driver_name
7882

7983
# Build extra constructor kwargs from mock_pins

0 commit comments

Comments
 (0)