|
| 1 | +import json |
| 2 | + |
| 3 | + |
| 4 | +# Map sensor class names to short JSON keys to save space. |
| 5 | +_SENSOR_KEYS = { |
| 6 | + "hts221": "hts", |
| 7 | + "lis2mdl": "mag", |
| 8 | + "ism330dl": "ism", |
| 9 | + "wsen_hids": "hid", |
| 10 | + "wsen_pads": "pad", |
| 11 | +} |
| 12 | + |
| 13 | +# Reverse map: short key -> sensor name. |
| 14 | +_KEY_SENSORS = {v: k for k, v in _SENSOR_KEYS.items()} |
| 15 | + |
| 16 | + |
| 17 | +class SteamiConfig(object): |
| 18 | + """Persistent configuration stored in the DAPLink F103 config zone. |
| 19 | +
|
| 20 | + Data is serialised as compact JSON and written via |
| 21 | + ``DaplinkFlash.write_config()`` / ``read_config()``. |
| 22 | +
|
| 23 | + Args: |
| 24 | + flash: a ``DaplinkFlash`` instance. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, flash): |
| 28 | + self._flash = flash |
| 29 | + self._data = {} |
| 30 | + |
| 31 | + # -------------------------------------------------- |
| 32 | + # Persistence |
| 33 | + # -------------------------------------------------- |
| 34 | + |
| 35 | + def load(self): |
| 36 | + """Load configuration from the config zone.""" |
| 37 | + raw = self._flash.read_config() |
| 38 | + if raw: |
| 39 | + self._data = json.loads(raw) |
| 40 | + else: |
| 41 | + self._data = {} |
| 42 | + |
| 43 | + def save(self): |
| 44 | + """Save configuration to the config zone.""" |
| 45 | + self._flash.clear_config() |
| 46 | + self._flash.write_config(json.dumps(self._data, separators=(",", ":"))) |
| 47 | + |
| 48 | + # -------------------------------------------------- |
| 49 | + # Board info |
| 50 | + # -------------------------------------------------- |
| 51 | + |
| 52 | + @property |
| 53 | + def board_revision(self): |
| 54 | + """Board hardware revision number, or None.""" |
| 55 | + return self._data.get("rev") |
| 56 | + |
| 57 | + @board_revision.setter |
| 58 | + def board_revision(self, value): |
| 59 | + self._data["rev"] = int(value) |
| 60 | + |
| 61 | + @property |
| 62 | + def board_name(self): |
| 63 | + """Board name string, or None.""" |
| 64 | + return self._data.get("name") |
| 65 | + |
| 66 | + @board_name.setter |
| 67 | + def board_name(self, value): |
| 68 | + self._data["name"] = str(value) |
| 69 | + |
| 70 | + # -------------------------------------------------- |
| 71 | + # Temperature calibration |
| 72 | + # -------------------------------------------------- |
| 73 | + |
| 74 | + def set_temperature_calibration(self, sensor, gain=1.0, offset=0.0): |
| 75 | + """Store temperature calibration for a sensor. |
| 76 | +
|
| 77 | + Args: |
| 78 | + sensor: sensor name (e.g. ``"hts221"``, ``"wsen_pads"``). |
| 79 | + gain: multiplicative gain factor. |
| 80 | + offset: additive offset in degrees Celsius. |
| 81 | + """ |
| 82 | + key = _SENSOR_KEYS.get(sensor) |
| 83 | + if key is None: |
| 84 | + raise ValueError("unknown sensor: {}".format(sensor)) |
| 85 | + tc = self._data.get("tc") |
| 86 | + if tc is None: |
| 87 | + tc = {} |
| 88 | + self._data["tc"] = tc |
| 89 | + tc[key] = {"g": gain, "o": offset} |
| 90 | + |
| 91 | + def get_temperature_calibration(self, sensor): |
| 92 | + """Return temperature calibration for a sensor. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + dict with ``"gain"`` and ``"offset"`` keys, or None. |
| 96 | + """ |
| 97 | + key = _SENSOR_KEYS.get(sensor) |
| 98 | + if key is None: |
| 99 | + raise ValueError("unknown sensor: {}".format(sensor)) |
| 100 | + tc = self._data.get("tc") |
| 101 | + if tc is None: |
| 102 | + return None |
| 103 | + entry = tc.get(key) |
| 104 | + if entry is None: |
| 105 | + return None |
| 106 | + return {"gain": entry["g"], "offset": entry["o"]} |
| 107 | + |
| 108 | + def apply_temperature_calibration(self, sensor_instance): |
| 109 | + """Apply stored calibration to a sensor instance. |
| 110 | +
|
| 111 | + The sensor class name is used to look up calibration data. |
| 112 | + The instance must have ``_temp_gain`` and ``_temp_offset`` |
| 113 | + attributes (all STeaMi temperature sensors do). |
| 114 | +
|
| 115 | + Args: |
| 116 | + sensor_instance: a driver instance (e.g. ``HTS221``). |
| 117 | + """ |
| 118 | + class_name = type(sensor_instance).__name__.lower() |
| 119 | + cal = self.get_temperature_calibration(class_name) |
| 120 | + if cal is None: |
| 121 | + return |
| 122 | + sensor_instance._temp_gain = cal["gain"] |
| 123 | + sensor_instance._temp_offset = cal["offset"] |
0 commit comments