@@ -15,9 +15,11 @@ class LIS2HH12:
1515 ACC_Z_L_REG = const (0x2C )
1616 ACC_Z_H_REG = const (0x2D )
1717
18- def __init__ (self , pytrack = None , sda = 'P22' , scl = 'P21' ):
19- if pytrack is not None :
20- self .i2c = pytrack .i2c
18+ SCALE = const (8192 )
19+
20+ def __init__ (self , pysense = None , sda = 'P22' , scl = 'P21' ):
21+ if pysense is not None :
22+ self .i2c = pysense .i2c
2123 else :
2224 from machine import I2C
2325 self .i2c = I2C (0 , mode = I2C .MASTER , pins = (sda , scl ))
@@ -30,26 +32,31 @@ def __init__(self, pytrack=None, sda='P22', scl='P21'):
3032
3133 whoami = self .i2c .readfrom_mem (ACC_I2CADDR , PRODUCTID_REG , 1 )
3234 if (whoami [0 ] != 0x41 ):
33- raise ValueError ("Incorrect product ID" )
35+ raise ValueError ("Incorrect Product ID" )
36+
3437 # enable acceleration readings
3538 self .i2c .readfrom_mem_into (ACC_I2CADDR , CTRL1_REG , self .reg )
36- self .reg [0 ] &= ~ 0x70
37- self .reg [0 ] |= 0x30
39+ self .reg [0 ] &= ~ 0b01110000
40+ self .reg [0 ] |= 0b00110000
3841 self .i2c .writeto_mem (ACC_I2CADDR , CTRL1_REG , self .reg )
42+
3943 # change the full-scale to 4g
4044 self .i2c .readfrom_mem_into (ACC_I2CADDR , CTRL4_REG , self .reg )
4145 self .reg [0 ] &= ~ 0b00110000
4246 self .reg [0 ] |= 0b00100000
4347 self .i2c .writeto_mem (ACC_I2CADDR , CTRL4_REG , self .reg )
4448
45- def read (self ):
49+ # make a first read
50+ self .acceleration ()
51+
52+ def acceleration (self ):
4653 x = self .i2c .readfrom_mem (ACC_I2CADDR , ACC_X_L_REG , 2 )
4754 self .x = struct .unpack ('<h' , x )
4855 y = self .i2c .readfrom_mem (ACC_I2CADDR , ACC_Y_L_REG , 2 )
4956 self .y = struct .unpack ('<h' , y )
5057 z = self .i2c .readfrom_mem (ACC_I2CADDR , ACC_Z_L_REG , 2 )
5158 self .z = struct .unpack ('<h' , z )
52- return (self .x [0 ], self .y [0 ], self .z [0 ])
59+ return (self .x [0 ] / SCALE , self .y [0 ] / SCALE , self .z [0 ] / SCALE )
5360
5461 def roll (self ):
5562 div = math .sqrt (math .pow (self .y [0 ], 2 ) + math .pow (self .z [0 ], 2 ))
0 commit comments