62
62
_CMD_TEMP = const (0x01 ) # Temperature bit for commands
63
63
64
64
_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
66
66
_CMD_REG_CONF3 = const (0x02 ) # Oversampling, Filter, Resolution
67
67
_CMD_REG_CONF4 = const (0x03 ) # Sensitivity drift
68
68
@@ -211,6 +211,7 @@ def __init__( # pylint: disable=too-many-arguments
211
211
resolution : int = RESOLUTION_16 ,
212
212
filt : int = FILTER_7 ,
213
213
oversampling : int = OSR_3 ,
214
+ temperature_compensation : bool = False ,
214
215
debug : bool = False ,
215
216
) -> None :
216
217
self .i2c_device = I2CDevice (i2c_bus , address )
@@ -220,6 +221,7 @@ def __init__( # pylint: disable=too-many-arguments
220
221
self ._filter = filt
221
222
self ._osr = oversampling
222
223
self ._gain_current = gain
224
+ self ._temperature_compensation = temperature_compensation
223
225
224
226
# Put the device in a known state to start
225
227
self .reset ()
@@ -237,6 +239,7 @@ def __init__( # pylint: disable=too-many-arguments
237
239
238
240
# Set gain to the supplied level
239
241
self .gain = self ._gain_current
242
+ self .temperature_compensation = self ._temperature_compensation
240
243
241
244
def _transceive (self , payload : ReadableBuffer , rxlen : int = 0 ) -> bytearray :
242
245
"""
@@ -385,6 +388,20 @@ def oversampling(self, level: int) -> None:
385
388
self .write_reg (_CMD_REG_CONF3 , reg )
386
389
self ._osr = level
387
390
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
+
388
405
def display_status (self ) -> None :
389
406
"""
390
407
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]:
462
479
"""
463
480
Reads a single X/Y/Z sample from the magnetometer.
464
481
"""
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
+ \t resolution_x: { self .resolution_x }
490
+ \t resolution_y: { self .resolution_y }
491
+ \t resolution_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
+
465
498
# Set conversion delay based on filter and oversampling
466
499
delay = _TCONV_LOOKUP [self ._filter ][self ._osr ] / 1000 # per datasheet
467
500
delay *= 1.1 # plus a little
@@ -493,6 +526,9 @@ def _unpack_axis_data(self, resolution: int, data: ReadableBuffer) -> int:
493
526
elif resolution == RESOLUTION_18 :
494
527
(value ,) = struct .unpack (">H" , data )
495
528
value -= 0x8000
529
+ elif self .temperature_compensation :
530
+ (value ,) = struct .unpack (">H" , data )
531
+ value -= 0x8000
496
532
else :
497
533
value = struct .unpack (">h" , data )[0 ]
498
534
return value
0 commit comments