Skip to content

Commit 7762818

Browse files
authored
Merge pull request #44 from brian-eng/offsets
Add Axis Offset Compensation
2 parents 773a846 + 0f2be0e commit 7762818

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

adafruit_mlx90393.py

+52
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
_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
68+
_CMD_REG_CONF5 = const(0x04) # X-axis Offset Correction
69+
_CMD_REG_CONF6 = const(0x05) # Y-axis Offset Correction
70+
_CMD_REG_CONF7 = const(0x06) # Z-axis Offset Correction
6871

6972
# Gain settings
7073
GAIN_5X = 0x0
@@ -212,6 +215,7 @@ def __init__( # pylint: disable=too-many-arguments
212215
filt: int = FILTER_7,
213216
oversampling: int = OSR_3,
214217
temperature_compensation: bool = False,
218+
offset: int = 0,
215219
debug: bool = False,
216220
) -> None:
217221
self.i2c_device = I2CDevice(i2c_bus, address)
@@ -222,6 +226,7 @@ def __init__( # pylint: disable=too-many-arguments
222226
self._osr = oversampling
223227
self._gain_current = gain
224228
self._temperature_compensation = temperature_compensation
229+
self._off_x = self._off_y = self._off_z = offset
225230

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

249+
# Set offsets to supplied level
250+
self.offset_x = self._off_x
251+
self.offset_y = self._off_y
252+
self.offset_z = self._off_z
253+
244254
def _transceive(self, payload: ReadableBuffer, rxlen: int = 0) -> bytearray:
245255
"""
246256
Writes the specified 'payload' to the sensor
@@ -402,6 +412,48 @@ def temperature_compensation(self, temperature_compensation: bool) -> None:
402412
self.write_reg(_CMD_REG_CONF2, reg)
403413
self._temperature_compensation = temperature_compensation
404414

415+
@property
416+
def offset_x(self) -> int:
417+
"""The X axis offset."""
418+
return self._off_x
419+
420+
@offset_x.setter
421+
def offset_x(self, offset: int) -> None:
422+
self._set_offset(0, offset)
423+
self._off_x = offset
424+
425+
@property
426+
def offset_y(self) -> int:
427+
"""The Y axis offset."""
428+
return self._off_y
429+
430+
@offset_y.setter
431+
def offset_y(self, offset: int) -> None:
432+
self._set_offset(1, offset)
433+
self._off_y = offset
434+
435+
@property
436+
def offset_z(self) -> int:
437+
"""The Z axis offset."""
438+
return self._off_z
439+
440+
@offset_z.setter
441+
def offset_z(self, offset: int) -> None:
442+
self._set_offset(2, offset)
443+
self._off_z = offset
444+
445+
def _set_offset(self, axis: int, offset: int) -> None:
446+
if offset < 0x0000 or offset > 0xFFFF:
447+
raise ValueError("Incorrect offset setting.")
448+
if axis == 0:
449+
self.write_reg(_CMD_REG_CONF5, offset)
450+
elif axis == 1:
451+
self.write_reg(_CMD_REG_CONF6, offset)
452+
elif axis == 2:
453+
self.write_reg(_CMD_REG_CONF7, offset)
454+
else:
455+
raise ValueError("Incorrect axis setting.")
456+
405457
def display_status(self) -> None:
406458
"""
407459
Prints out the content of the last status byte in a human-readable

0 commit comments

Comments
 (0)