@@ -33,7 +33,7 @@ def __init__(
3333 self .readbuffer = bytearray (1 )
3434
3535 # Perform a soft reset to ensure the sensor starts in a known state.
36- self ._write_reg (0x20 , LIS2MDL_CFG_REG_A ) # SOFT_RST=1 (not 0x10)
36+ self ._write_reg (LIS2MDL_CFG_REG_A , 0x20 ) # SOFT_RST=1 (not 0x10)
3737 try :
3838 sleep_ms (10 ) # Small delay for reset to complete
3939 except Exception :
@@ -44,14 +44,14 @@ def __init__(
4444 comp = 1 if temp_comp else 0
4545 lp = 1 if low_power else 0
4646 cfg_a = (comp << 7 ) | (lp << 4 ) | (odr_bits << 2 ) | 0b00
47- self ._write_reg (cfg_a , LIS2MDL_CFG_REG_A ) # Essential to exit IDLE mode
47+ self ._write_reg (LIS2MDL_CFG_REG_A , cfg_a ) # Essential to exit IDLE mode
4848
4949 # Configure low-pass filter and other optional settings.
50- self ._write_reg (0x00 , LIS2MDL_CFG_REG_B ) # Default: LPF and offset cancellation off
50+ self ._write_reg (LIS2MDL_CFG_REG_B , 0x00 ) # Default: LPF and offset cancellation off
5151
5252 # Enable block data update and optionally configure the DRDY pin.
5353 cfg_c = 0x10 | (0x01 if drdy_enable else 0x00 )
54- self ._write_reg (cfg_c , LIS2MDL_CFG_REG_C )
54+ self ._write_reg (LIS2MDL_CFG_REG_C , cfg_c )
5555
5656 ##
5757 # --- SET functions ---
@@ -63,14 +63,14 @@ def set_mode(self, mode: str):
6363 md = {"continuous" : 0b00 , "single" : 0b01 , "idle" : 0b11 }.get (mode , 0b00 )
6464 reg = self ._read_reg (LIS2MDL_CFG_REG_A )
6565 reg = (reg & ~ 0b11 ) | md
66- self ._write_reg (reg , LIS2MDL_CFG_REG_A )
66+ self ._write_reg (LIS2MDL_CFG_REG_A , reg )
6767
6868 def set_odr (self , hz : int ):
6969 # ODR1..0: 00=10Hz, 01=20Hz, 10=50Hz, 11=100Hz
7070 odr_bits = {10 : 0b00 , 20 : 0b01 , 50 : 0b10 , 100 : 0b11 }.get (hz , 0b00 )
7171 reg = self ._read_reg (LIS2MDL_CFG_REG_A )
7272 reg = (reg & ~ (0b11 << 2 )) | (odr_bits << 2 )
73- self ._write_reg (reg , LIS2MDL_CFG_REG_A )
73+ self ._write_reg (LIS2MDL_CFG_REG_A , reg )
7474
7575 def set_low_power (self , enabled : bool ):
7676 # LP bit (bit4) : 0=High-Res, 1=Low-Power
@@ -79,7 +79,7 @@ def set_low_power(self, enabled: bool):
7979 reg |= 1 << 4
8080 else :
8181 reg &= ~ (1 << 4 )
82- self ._write_reg (reg , LIS2MDL_CFG_REG_A )
82+ self ._write_reg (LIS2MDL_CFG_REG_A , reg )
8383
8484 # --- Filters / offset cancellation (CFG_REG_B: 0x61) ---
8585 def set_low_pass (self , enabled : bool ):
@@ -89,7 +89,7 @@ def set_low_pass(self, enabled: bool):
8989 reg |= 1 << 0
9090 else :
9191 reg &= ~ (1 << 0 )
92- self ._write_reg (reg , LIS2MDL_CFG_REG_B )
92+ self ._write_reg (LIS2MDL_CFG_REG_B , reg )
9393
9494 def set_offset_cancellation (self , enabled : bool , oneshot : bool = False ):
9595 # OFF_CANC (bit1), OFF_CANC_ONE_SHOT (bit4)
@@ -102,7 +102,7 @@ def set_offset_cancellation(self, enabled: bool, oneshot: bool = False):
102102 reg |= 1 << 4
103103 else :
104104 reg &= ~ (1 << 4 )
105- self ._write_reg (reg , LIS2MDL_CFG_REG_B )
105+ self ._write_reg (LIS2MDL_CFG_REG_B , reg )
106106
107107 # --- Interface options / BDU (CFG_REG_C: 0x62) ---
108108 def set_bdu (self , enable = True ):
@@ -112,7 +112,7 @@ def set_bdu(self, enable=True):
112112 reg |= 1 << 4
113113 else :
114114 reg &= ~ (1 << 4 )
115- self ._write_reg (reg , LIS2MDL_CFG_REG_C )
115+ self ._write_reg (LIS2MDL_CFG_REG_C , reg )
116116
117117 def set_endianness (self , big_endian : bool ):
118118 # BLE (bit3)
@@ -121,7 +121,7 @@ def set_endianness(self, big_endian: bool):
121121 reg |= 1 << 3
122122 else :
123123 reg &= ~ (1 << 3 )
124- self ._write_reg (reg , LIS2MDL_CFG_REG_C )
124+ self ._write_reg (LIS2MDL_CFG_REG_C , reg )
125125
126126 def use_spi_4wire (self , enable : bool ):
127127 # 4WSPI (bit2)
@@ -130,7 +130,7 @@ def use_spi_4wire(self, enable: bool):
130130 reg |= 1 << 2
131131 else :
132132 reg &= ~ (1 << 2 )
133- self ._write_reg (reg , LIS2MDL_CFG_REG_C )
133+ self ._write_reg (LIS2MDL_CFG_REG_C , reg )
134134
135135 # --- Compass: heading offset & declination (software) ---
136136 _heading_offset_deg = 0.0 # user setting: align your physical 0°
@@ -144,15 +144,15 @@ def set_declination(self, deg: float):
144144
145145 # (remember to correct your heading_flat_only with atan2(y, x) then + offsets)
146146
147- def _write_reg (self , data , reg ):
147+ def _write_reg (self , reg , data ):
148148 # Write a byte to a specific register.
149149 self .writebuffer [0 ] = data
150150 self .i2c .writeto_mem (self .address , reg , self .writebuffer )
151151
152152 def _write_16 (self , reg_l , value ):
153153 value &= 0xFFFF
154- self ._write_reg (value & 0xFF , reg_l )
155- self ._write_reg ((value >> 8 ) & 0xFF , reg_l + 1 )
154+ self ._write_reg (reg_l , value & 0xFF )
155+ self ._write_reg (reg_l + 1 , (value >> 8 ) & 0xFF )
156156
157157 def set_hw_offsets (self , x : int , y : int , z : int ):
158158 # writes to OFFSET_X/Y/Z_REG_L/H
@@ -537,14 +537,14 @@ def power_down(self):
537537 """Switches to IDLE mode (low power)."""
538538 r = self ._read_reg (LIS2MDL_CFG_REG_A )
539539 r = (r & ~ 0b11 ) | 0b11 # MD1..0 = 11
540- self ._write_reg (r , LIS2MDL_CFG_REG_A )
540+ self ._write_reg (LIS2MDL_CFG_REG_A , r )
541541
542542 def wake (self , mode : str = "continuous" ):
543543 """Wakes the sensor: 'continuous' (default) or 'single'."""
544544 md = {"continuous" : 0b00 , "single" : 0b01 }.get (mode , 0b00 )
545545 r = self ._read_reg (LIS2MDL_CFG_REG_A )
546546 r = (r & ~ 0b11 ) | md
547- self ._write_reg (r , LIS2MDL_CFG_REG_A )
547+ self ._write_reg (LIS2MDL_CFG_REG_A , r )
548548
549549 def soft_reset (self , wait_ms : int = 10 ):
550550 """
@@ -553,7 +553,7 @@ def soft_reset(self, wait_ms: int = 10):
553553 """
554554 r = self ._read_reg (LIS2MDL_CFG_REG_A )
555555 r |= 1 << 5 # SOFT_RST
556- self ._write_reg (r , LIS2MDL_CFG_REG_A )
556+ self ._write_reg (LIS2MDL_CFG_REG_A , r )
557557 sleep_ms (wait_ms )
558558
559559 def reboot (self , wait_ms : int = 10 ):
@@ -563,7 +563,7 @@ def reboot(self, wait_ms: int = 10):
563563 """
564564 r = self ._read_reg (LIS2MDL_CFG_REG_A )
565565 r |= 1 << 6 # REBOOT
566- self ._write_reg (r , LIS2MDL_CFG_REG_A )
566+ self ._write_reg (LIS2MDL_CFG_REG_A , r )
567567 sleep_ms (wait_ms )
568568
569569 def is_idle (self ) -> bool :
0 commit comments