Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Axis Offset Compensation #44

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions adafruit_mlx90393.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
_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
_CMD_REG_CONF5 = const(0x04) # X-axis Offset Correction
_CMD_REG_CONF6 = const(0x05) # Y-axis Offset Correction
_CMD_REG_CONF7 = const(0x06) # Z-axis Offset Correction

# Gain settings
GAIN_5X = 0x0
Expand Down Expand Up @@ -212,6 +215,7 @@ def __init__( # pylint: disable=too-many-arguments
filt: int = FILTER_7,
oversampling: int = OSR_3,
temperature_compensation: bool = False,
offset: int = 0,
debug: bool = False,
) -> None:
self.i2c_device = I2CDevice(i2c_bus, address)
Expand All @@ -222,6 +226,7 @@ def __init__( # pylint: disable=too-many-arguments
self._osr = oversampling
self._gain_current = gain
self._temperature_compensation = temperature_compensation
self._off_x = self._off_y = self._off_z = offset

# Put the device in a known state to start
self.reset()
Expand All @@ -241,6 +246,11 @@ def __init__( # pylint: disable=too-many-arguments
self.gain = self._gain_current
self.temperature_compensation = self._temperature_compensation

# Set offsets to supplied level
self.offset_x = self._off_x
self.offset_y = self._off_y
self.offset_z = self._off_z

def _transceive(self, payload: ReadableBuffer, rxlen: int = 0) -> bytearray:
"""
Writes the specified 'payload' to the sensor
Expand Down Expand Up @@ -402,6 +412,48 @@ def temperature_compensation(self, temperature_compensation: bool) -> None:
self.write_reg(_CMD_REG_CONF2, reg)
self._temperature_compensation = temperature_compensation

@property
def offset_x(self) -> int:
"""The X axis offset."""
return self._off_x

@offset_x.setter
def offset_x(self, offset: int) -> None:
self._set_offset(0, offset)
self._off_x = offset

@property
def offset_y(self) -> int:
"""The Y axis offset."""
return self._off_y

@offset_y.setter
def offset_y(self, offset: int) -> None:
self._set_offset(1, offset)
self._off_y = offset

@property
def offset_z(self) -> int:
"""The Z axis offset."""
return self._off_z

@offset_z.setter
def offset_z(self, offset: int) -> None:
self._set_offset(2, offset)
self._off_z = offset

def _set_offset(self, axis: int, offset: int) -> None:
if offset < 0x0000 or offset > 0xFFFF:
raise ValueError("Incorrect offset setting.")
if axis == 0:
self.write_reg(_CMD_REG_CONF5, offset)
elif axis == 1:
self.write_reg(_CMD_REG_CONF6, offset)
elif axis == 2:
self.write_reg(_CMD_REG_CONF7, offset)
else:
raise ValueError("Incorrect axis setting.")

def display_status(self) -> None:
"""
Prints out the content of the last status byte in a human-readable
Expand Down