Skip to content

Commit eab0d73

Browse files
authored
Merge pull request #39 from purepani/add_temperature_compensation
Adds temperature compensation
2 parents 3e8235e + 88a83c4 commit eab0d73

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

adafruit_mlx90393.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
_CMD_TEMP = const(0x01) # Temperature bit for commands
6363

6464
_CMD_REG_CONF1 = const(0x00) # Gain
65-
_CMD_REG_CONF2 = const(0x01) # Burst, comm mode
65+
_CMD_REG_CONF2 = const(0x01) # Burst, comm mode, temperature compensation
6666
_CMD_REG_CONF3 = const(0x02) # Oversampling, Filter, Resolution
6767
_CMD_REG_CONF4 = const(0x03) # Sensitivity drift
6868

@@ -211,6 +211,7 @@ def __init__( # pylint: disable=too-many-arguments
211211
resolution: int = RESOLUTION_16,
212212
filt: int = FILTER_7,
213213
oversampling: int = OSR_3,
214+
temperature_compensation: bool = False,
214215
debug: bool = False,
215216
) -> None:
216217
self.i2c_device = I2CDevice(i2c_bus, address)
@@ -220,6 +221,7 @@ def __init__( # pylint: disable=too-many-arguments
220221
self._filter = filt
221222
self._osr = oversampling
222223
self._gain_current = gain
224+
self._temperature_compensation = temperature_compensation
223225

224226
# Put the device in a known state to start
225227
self.reset()
@@ -237,6 +239,7 @@ def __init__( # pylint: disable=too-many-arguments
237239

238240
# Set gain to the supplied level
239241
self.gain = self._gain_current
242+
self.temperature_compensation = self._temperature_compensation
240243

241244
def _transceive(self, payload: ReadableBuffer, rxlen: int = 0) -> bytearray:
242245
"""
@@ -385,6 +388,20 @@ def oversampling(self, level: int) -> None:
385388
self.write_reg(_CMD_REG_CONF3, reg)
386389
self._osr = level
387390

391+
@property
392+
def temperature_compensation(self) -> bool:
393+
"""The temperature compensation setting"""
394+
return self._temperature_compensation
395+
396+
@temperature_compensation.setter
397+
def temperature_compensation(self, temperature_compensation: bool) -> None:
398+
reg = self.read_reg(_CMD_REG_CONF2)
399+
t_cmp_bit = 10
400+
reg &= ~(1 << t_cmp_bit)
401+
reg |= temperature_compensation << t_cmp_bit
402+
self.write_reg(_CMD_REG_CONF2, reg)
403+
self._temperature_compensation = temperature_compensation
404+
388405
def display_status(self) -> None:
389406
"""
390407
Prints out the content of the last status byte in a human-readable
@@ -462,6 +479,22 @@ def read_data(self) -> Tuple[int, int, int]:
462479
"""
463480
Reads a single X/Y/Z sample from the magnetometer.
464481
"""
482+
483+
resolutions = {self.resolution_x, self.resolution_y, self.resolution_z}
484+
valid_tcomp_resolutions = {RESOLUTION_16, RESOLUTION_17}
485+
if self._temperature_compensation and not resolutions.issubset(
486+
valid_tcomp_resolutions
487+
):
488+
resolutions_output = f"""Current Resolutions:
489+
\tresolution_x: {self.resolution_x}
490+
\tresolution_y: {self.resolution_y}
491+
\tresolution_z: {self.resolution_z}"""
492+
493+
raise ValueError(
494+
"All resolutions must be RESOLUTION_16 or RESOLUTION_17"
495+
f" if temperature compensation is enabled.\n {resolutions_output}"
496+
)
497+
465498
# Set conversion delay based on filter and oversampling
466499
delay = _TCONV_LOOKUP[self._filter][self._osr] / 1000 # per datasheet
467500
delay *= 1.1 # plus a little
@@ -493,6 +526,9 @@ def _unpack_axis_data(self, resolution: int, data: ReadableBuffer) -> int:
493526
elif resolution == RESOLUTION_18:
494527
(value,) = struct.unpack(">H", data)
495528
value -= 0x8000
529+
elif self.temperature_compensation:
530+
(value,) = struct.unpack(">H", data)
531+
value -= 0x8000
496532
else:
497533
value = struct.unpack(">h", data)[0]
498534
return value

0 commit comments

Comments
 (0)