1+ # device.py
12from machine import I2C
23from lis2mdl .const import *
34
45class LIS2MDL (object ):
5- def __init__ (self , i2c , address = LIS2MDL_I2C_ADDR ):
6+ def __init__ (self , i2c , address = LIS2MDL_I2C_ADDR , odr_hz = 10 , temp_comp = True , low_power = False , drdy_pin = False ):
67 self .i2c = i2c
78 self .address = address
8-
99 self .writebuffer = bytearray (1 )
1010 self .readbuffer = bytearray (1 )
1111
12- # Initialize the device
13- self .reset ()
14- self .configure ()
15-
16- def reset (self ):
17- # Reset the device using CFG_REG_A
18- self .setReg (0x10 , LIS2MDL_CFG_REG_A )
19-
20- def configure (self ):
21- # Configure the device (example: enable continuous mode)
12+ # Soft reset property
13+ self .setReg (0x20 , LIS2MDL_CFG_REG_A ) # SOFT_RST=1 (not 0x10)
14+ # small delay (if needed on the MicroPython side)
15+ try :
16+ import time ; time .sleep_ms (10 )
17+ except :
18+ pass
19+
20+ # CFG_REG_A: COMP_TEMP, LP, ODR, MD=00 (continuous)
21+ odr_bits = {10 :0b00 , 20 :0b01 , 50 :0b10 , 100 :0b11 }.get (odr_hz , 0b00 )
22+ comp = 1 if temp_comp else 0
23+ lp = 1 if low_power else 0
24+ cfg_a = (comp << 7 ) | (lp << 4 ) | (odr_bits << 2 ) | 0b00
25+ self .setReg (cfg_a , LIS2MDL_CFG_REG_A ) # <- essential to exit IDLE
26+
27+ # CFG_REG_B: default 0x00 (LPF/off_canc off); you can enable LPF if desired
2228 self .setReg (0x00 , LIS2MDL_CFG_REG_B )
23- self .setReg (0x01 , LIS2MDL_CFG_REG_C )
2429
30+ # CFG_REG_C: BDU=1 (+ DRDY_on_PIN if requested)
31+ cfg_c = 0x10 | (0x01 if drdy_pin else 0x00 )
32+ self .setReg (cfg_c , LIS2MDL_CFG_REG_C )
33+
34+ # --- identical low-level ---
2535 def setReg (self , data , reg ):
2636 self .writebuffer [0 ] = data
2737 self .i2c .writeto_mem (self .address , reg , self .writebuffer )
@@ -30,22 +40,22 @@ def getReg(self, reg):
3040 self .i2c .readfrom_mem_into (self .address , reg , self .readbuffer )
3141 return self .readbuffer [0 ]
3242
33- def get2Reg ( self , reg ):
34- lowerByte = self . getReg ( reg )
35- higherByte = self . getReg ( reg + 1 )
36- return ( higherByte << 8 ) + lowerByte
43+ # signed helper
44+ @ staticmethod
45+ def _to_int16 ( v ):
46+ return v - 0x10000 if v & 0x8000 else v
3747
48+ # burst read + signed
3849 def read_magnet (self ):
39- # Read magnetic field data for X, Y, Z axes
40- x = self .get2Reg (LIS2MDL_OUTX_L_REG )
41- y = self .get2Reg (LIS2MDL_OUTY_L_REG )
42- z = self .get2Reg (LIS2MDL_OUTZ_L_REG )
50+ # auto-increment: reg | 0x80 (cf. datasheet I2C op)
51+ buf = self .i2c .readfrom_mem (self .address , LIS2MDL_OUTX_L_REG | 0x80 , 6 )
52+ x = self ._to_int16 ((buf [1 ] << 8 ) | buf [0 ])
53+ y = self ._to_int16 ((buf [3 ] << 8 ) | buf [2 ])
54+ z = self ._to_int16 ((buf [5 ] << 8 ) | buf [4 ])
4355 return (x , y , z )
4456
4557 def whoAmI (self ):
46- # Read the WHO_AM_I register
4758 return self .getReg (LIS2MDL_WHO_AM_I )
4859
4960 def status (self ):
50- # Read the STATUS register
51- return self .getReg (LIS2MDL_STATUS_REG )
61+ return self .getReg (LIS2MDL_STATUS_REG )
0 commit comments