diff --git a/adafruit_mlx90393.py b/adafruit_mlx90393.py index 79d79be..e71785b 100755 --- a/adafruit_mlx90393.py +++ b/adafruit_mlx90393.py @@ -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 @@ -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) @@ -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() @@ -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 @@ -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