Skip to content

Commit

Permalink
Merge pull request #39 from purepani/add_temperature_compensation
Browse files Browse the repository at this point in the history
Adds temperature compensation
  • Loading branch information
FoamyGuy committed Sep 8, 2024
2 parents 3e8235e + 88a83c4 commit eab0d73
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion adafruit_mlx90393.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
_CMD_TEMP = const(0x01) # Temperature bit for commands

_CMD_REG_CONF1 = const(0x00) # Gain
_CMD_REG_CONF2 = const(0x01) # Burst, comm mode
_CMD_REG_CONF2 = const(0x01) # Burst, comm mode, temperature compensation
_CMD_REG_CONF3 = const(0x02) # Oversampling, Filter, Resolution
_CMD_REG_CONF4 = const(0x03) # Sensitivity drift

Expand Down Expand Up @@ -211,6 +211,7 @@ def __init__( # pylint: disable=too-many-arguments
resolution: int = RESOLUTION_16,
filt: int = FILTER_7,
oversampling: int = OSR_3,
temperature_compensation: bool = False,
debug: bool = False,
) -> None:
self.i2c_device = I2CDevice(i2c_bus, address)
Expand All @@ -220,6 +221,7 @@ def __init__( # pylint: disable=too-many-arguments
self._filter = filt
self._osr = oversampling
self._gain_current = gain
self._temperature_compensation = temperature_compensation

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

# Set gain to the supplied level
self.gain = self._gain_current
self.temperature_compensation = self._temperature_compensation

def _transceive(self, payload: ReadableBuffer, rxlen: int = 0) -> bytearray:
"""
Expand Down Expand Up @@ -385,6 +388,20 @@ def oversampling(self, level: int) -> None:
self.write_reg(_CMD_REG_CONF3, reg)
self._osr = level

@property
def temperature_compensation(self) -> bool:
"""The temperature compensation setting"""
return self._temperature_compensation

@temperature_compensation.setter
def temperature_compensation(self, temperature_compensation: bool) -> None:
reg = self.read_reg(_CMD_REG_CONF2)
t_cmp_bit = 10
reg &= ~(1 << t_cmp_bit)
reg |= temperature_compensation << t_cmp_bit
self.write_reg(_CMD_REG_CONF2, reg)
self._temperature_compensation = temperature_compensation

def display_status(self) -> None:
"""
Prints out the content of the last status byte in a human-readable
Expand Down Expand Up @@ -462,6 +479,22 @@ def read_data(self) -> Tuple[int, int, int]:
"""
Reads a single X/Y/Z sample from the magnetometer.
"""

resolutions = {self.resolution_x, self.resolution_y, self.resolution_z}
valid_tcomp_resolutions = {RESOLUTION_16, RESOLUTION_17}
if self._temperature_compensation and not resolutions.issubset(
valid_tcomp_resolutions
):
resolutions_output = f"""Current Resolutions:
\tresolution_x: {self.resolution_x}
\tresolution_y: {self.resolution_y}
\tresolution_z: {self.resolution_z}"""

raise ValueError(
"All resolutions must be RESOLUTION_16 or RESOLUTION_17"
f" if temperature compensation is enabled.\n {resolutions_output}"
)

# Set conversion delay based on filter and oversampling
delay = _TCONV_LOOKUP[self._filter][self._osr] / 1000 # per datasheet
delay *= 1.1 # plus a little
Expand Down Expand Up @@ -493,6 +526,9 @@ def _unpack_axis_data(self, resolution: int, data: ReadableBuffer) -> int:
elif resolution == RESOLUTION_18:
(value,) = struct.unpack(">H", data)
value -= 0x8000
elif self.temperature_compensation:
(value,) = struct.unpack(">H", data)
value -= 0x8000
else:
value = struct.unpack(">h", data)[0]
return value
Expand Down

0 comments on commit eab0d73

Please sign in to comment.