55The MIT License (MIT) 
66
77Copyright (c) 2021 Damien P. George 
8- Copyright (c) 2021-2022  Ibrahim Abdelkader <iabdalkader@openmv.io> 
8+ Copyright (c) 2021-2023  Ibrahim Abdelkader <iabdalkader@openmv.io> 
99
1010Permission is hereby granted, free of charge, to any person obtaining a copy 
1111of this software and associated documentation files (the "Software"), to deal 
3535lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12))) 
3636
3737# Or init in SPI mode. 
38- #lsm = LSM6DSOX(SPI(5), cs_pin =Pin(10)) 
38+ #lsm = LSM6DSOX(SPI(5), cs =Pin(10)) 
3939
4040while (True): 
41-  print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_accel ())) 
42-  print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.read_gyro ())) 
41+  print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.accel ())) 
42+  print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.gyro ())) 
4343 print("") 
4444 time.sleep_ms(100) 
4545""" 
4646
4747import  array 
4848from  micropython  import  const 
4949
50+ _CTRL3_C  =  const (0x12 )
51+ _CTRL1_XL  =  const (0x10 )
52+ _CTRL8_XL  =  const (0x17 )
53+ _CTRL9_XL  =  const (0x18 )
5054
51- class  LSM6DSOX :
52-  _CTRL3_C  =  const (0x12 )
53-  _CTRL1_XL  =  const (0x10 )
54-  _CTRL8_XL  =  const (0x17 )
55-  _CTRL9_XL  =  const (0x18 )
55+ _CTRL2_G  =  const (0x11 )
56+ _CTRL7_G  =  const (0x16 )
5657
57-  _CTRL2_G  =  const (0x11 )
58-  _CTRL7_G  =  const (0x16 )
58+ _OUTX_L_G  =  const (0x22 )
59+ _OUTX_L_XL  =  const (0x28 )
60+ _MLC_STATUS  =  const (0x38 )
5961
60-  _OUTX_L_G  =  const (0x22 )
61-  _OUTX_L_XL  =  const (0x28 )
62-  _MLC_STATUS  =  const (0x38 )
62+ _DEFAULT_ADDR  =  const (0x6A )
63+ _WHO_AM_I_REG  =  const (0x0F )
6364
64-  _DEFAULT_ADDR  =  const (0x6A )
65-  _WHO_AM_I_REG  =  const (0x0F )
65+ _FUNC_CFG_ACCESS  =  const (0x01 )
66+ _FUNC_CFG_BANK_USER  =  const (0 )
67+ _FUNC_CFG_BANK_HUB  =  const (1 )
68+ _FUNC_CFG_BANK_EMBED  =  const (2 )
6669
67-  _FUNC_CFG_ACCESS  =  const (0x01 )
68-  _FUNC_CFG_BANK_USER  =  const (0 )
69-  _FUNC_CFG_BANK_HUB  =  const (1 )
70-  _FUNC_CFG_BANK_EMBED  =  const (2 )
70+ _MLC0_SRC  =  const (0x70 )
71+ _MLC_INT1  =  const (0x0D )
72+ _TAP_CFG0  =  const (0x56 )
7173
72-  _MLC0_SRC  =  const (0x70 )
73-  _MLC_INT1  =  const (0x0D )
74-  _TAP_CFG0  =  const (0x56 )
74+ _EMB_FUNC_EN_A  =  const (0x04 )
75+ _EMB_FUNC_EN_B  =  const (0x05 )
7576
76-  _EMB_FUNC_EN_A  =  const (0x04 )
77-  _EMB_FUNC_EN_B  =  const (0x05 )
7877
78+ class  LSM6DSOX :
7979 def  __init__ (
8080 self ,
8181 bus ,
82-  cs_pin = None ,
82+  cs = None ,
8383 address = _DEFAULT_ADDR ,
8484 gyro_odr = 104 ,
8585 accel_odr = 104 ,
@@ -95,15 +95,15 @@ def __init__(
9595 ucf: MLC program to load. 
9696 """ 
9797 self .bus  =  bus 
98-  self .cs_pin  =  cs_pin 
98+  self .cs  =  cs 
9999 self .address  =  address 
100100 self ._use_i2c  =  hasattr (self .bus , "readfrom_mem" )
101101
102-  if  not  self ._use_i2c  and  cs_pin  is  None :
102+  if  not  self ._use_i2c  and  cs  is  None :
103103 raise  ValueError ("A CS pin must be provided in SPI mode" )
104104
105105 # check the id of the Accelerometer/Gyro 
106-  if  self .__read_reg (_WHO_AM_I_REG ) !=  108 :
106+  if  self ._read_reg (_WHO_AM_I_REG ) !=  108 :
107107 raise  OSError ("No LSM6DS device was found at address 0x%x"  %  (self .address ))
108108
109109 # allocate scratch buffer for efficient conversions and memread op's 
@@ -131,7 +131,7 @@ def __init__(
131131
132132 # Sanity checks 
133133 if  not  gyro_odr  in  ODR :
134-  raise  ValueError ("Invalid sampling rate: %d"  %  accel_odr )
134+  raise  ValueError ("Invalid sampling rate: %d"  %  gyro_odr )
135135 if  not  gyro_scale  in  SCALE_GYRO :
136136 raise  ValueError ("invalid gyro scaling: %d"  %  gyro_scale )
137137 if  not  accel_odr  in  ODR :
@@ -148,74 +148,74 @@ def __init__(
148148
149149 # Set Gyroscope datarate and scale. 
150150 # Note output from LPF2 second filtering stage is selected. See Figure 18. 
151-  self .__write_reg (_CTRL1_XL , (ODR [accel_odr ] <<  4 ) |  (SCALE_ACCEL [accel_scale ] <<  2 ) |  2 )
151+  self ._write_reg (_CTRL1_XL , (ODR [accel_odr ] <<  4 ) |  (SCALE_ACCEL [accel_scale ] <<  2 ) |  2 )
152152
153153 # Enable LPF2 and HPF fast-settling mode, ODR/4 
154-  self .__write_reg (_CTRL8_XL , 0x09 )
154+  self ._write_reg (_CTRL8_XL , 0x09 )
155155
156156 # Set Gyroscope datarate and scale. 
157-  self .__write_reg (_CTRL2_G , (ODR [gyro_odr ] <<  4 ) |  (SCALE_GYRO [gyro_scale ] <<  2 ) |  0 )
157+  self ._write_reg (_CTRL2_G , (ODR [gyro_odr ] <<  4 ) |  (SCALE_GYRO [gyro_scale ] <<  2 ) |  0 )
158158
159159 self .gyro_scale  =  32768  /  gyro_scale 
160160 self .accel_scale  =  32768  /  accel_scale 
161161
162-  def  __read_reg (self , reg , size = 1 ):
162+  def  _read_reg (self , reg , size = 1 ):
163163 if  self ._use_i2c :
164164 buf  =  self .bus .readfrom_mem (self .address , reg , size )
165165 else :
166166 try :
167-  self .cs_pin (0 )
167+  self .cs (0 )
168168 self .bus .write (bytes ([reg  |  0x80 ]))
169169 buf  =  self .bus .read (size )
170170 finally :
171-  self .cs_pin (1 )
171+  self .cs (1 )
172172 if  size  ==  1 :
173173 return  int (buf [0 ])
174174 return  [int (x ) for  x  in  buf ]
175175
176-  def  __write_reg (self , reg , val ):
176+  def  _write_reg (self , reg , val ):
177177 if  self ._use_i2c :
178178 self .bus .writeto_mem (self .address , reg , bytes ([val ]))
179179 else :
180180 try :
181-  self .cs_pin (0 )
181+  self .cs (0 )
182182 self .bus .write (bytes ([reg , val ]))
183183 finally :
184-  self .cs_pin (1 )
184+  self .cs (1 )
185185
186-  def  __read_reg_into (self , reg , buf ):
186+  def  _read_reg_into (self , reg , buf ):
187187 if  self ._use_i2c :
188188 self .bus .readfrom_mem_into (self .address , reg , buf )
189189 else :
190190 try :
191-  self .cs_pin (0 )
191+  self .cs (0 )
192192 self .bus .write (bytes ([reg  |  0x80 ]))
193193 self .bus .readinto (buf )
194194 finally :
195-  self .cs_pin (1 )
195+  self .cs (1 )
196196
197197 def  reset (self ):
198-  self .__write_reg (_CTRL3_C , self .__read_reg (_CTRL3_C ) |  0x1 )
198+  self ._write_reg (_CTRL3_C , self ._read_reg (_CTRL3_C ) |  0x1 )
199199 for  i  in  range (0 , 10 ):
200-  if  (self .__read_reg (_CTRL3_C ) &  0x01 ) ==  0 :
200+  if  (self ._read_reg (_CTRL3_C ) &  0x01 ) ==  0 :
201201 return 
202202 time .sleep_ms (10 )
203203 raise  OSError ("Failed to reset LSM6DS device." )
204204
205205 def  set_mem_bank (self , bank ):
206-  cfg  =  self .__read_reg (_FUNC_CFG_ACCESS ) &  0x3F 
207-  self .__write_reg (_FUNC_CFG_ACCESS , cfg  |  (bank  <<  6 ))
206+  cfg  =  self ._read_reg (_FUNC_CFG_ACCESS ) &  0x3F 
207+  self ._write_reg (_FUNC_CFG_ACCESS , cfg  |  (bank  <<  6 ))
208208
209209 def  set_embedded_functions (self , enable , emb_ab = None ):
210210 self .set_mem_bank (_FUNC_CFG_BANK_EMBED )
211211 if  enable :
212-  self .__write_reg (_EMB_FUNC_EN_A , emb_ab [0 ])
213-  self .__write_reg (_EMB_FUNC_EN_B , emb_ab [1 ])
212+  self ._write_reg (_EMB_FUNC_EN_A , emb_ab [0 ])
213+  self ._write_reg (_EMB_FUNC_EN_B , emb_ab [1 ])
214214 else :
215-  emb_a  =  self .__read_reg (_EMB_FUNC_EN_A )
216-  emb_b  =  self .__read_reg (_EMB_FUNC_EN_B )
217-  self .__write_reg (_EMB_FUNC_EN_A , (emb_a  &  0xC7 ))
218-  self .__write_reg (_EMB_FUNC_EN_B , (emb_b  &  0xE6 ))
215+  emb_a  =  self ._read_reg (_EMB_FUNC_EN_A )
216+  emb_b  =  self ._read_reg (_EMB_FUNC_EN_B )
217+  self ._write_reg (_EMB_FUNC_EN_A , (emb_a  &  0xC7 ))
218+  self ._write_reg (_EMB_FUNC_EN_B , (emb_b  &  0xE6 ))
219219 emb_ab  =  (emb_a , emb_b )
220220
221221 self .set_mem_bank (_FUNC_CFG_BANK_USER )
@@ -227,45 +227,45 @@ def load_mlc(self, ucf):
227227 for  l  in  ucf_file :
228228 if  l .startswith ("Ac" ):
229229 v  =  [int (v , 16 ) for  v  in  l .strip ().split (" " )[1 :3 ]]
230-  self .__write_reg (v [0 ], v [1 ])
230+  self ._write_reg (v [0 ], v [1 ])
231231
232232 emb_ab  =  self .set_embedded_functions (False )
233233
234234 # Disable I3C interface 
235-  self .__write_reg (_CTRL9_XL , self .__read_reg (_CTRL9_XL ) |  0x01 )
235+  self ._write_reg (_CTRL9_XL , self ._read_reg (_CTRL9_XL ) |  0x01 )
236236
237237 # Enable Block Data Update 
238-  self .__write_reg (_CTRL3_C , self .__read_reg (_CTRL3_C ) |  0x40 )
238+  self ._write_reg (_CTRL3_C , self ._read_reg (_CTRL3_C ) |  0x40 )
239239
240240 # Route signals on interrupt pin 1 
241241 self .set_mem_bank (_FUNC_CFG_BANK_EMBED )
242-  self .__write_reg (_MLC_INT1 , self .__read_reg (_MLC_INT1 ) &  0x01 )
242+  self ._write_reg (_MLC_INT1 , self ._read_reg (_MLC_INT1 ) &  0x01 )
243243 self .set_mem_bank (_FUNC_CFG_BANK_USER )
244244
245245 # Configure interrupt pin mode 
246-  self .__write_reg (_TAP_CFG0 , self .__read_reg (_TAP_CFG0 ) |  0x41 )
246+  self ._write_reg (_TAP_CFG0 , self ._read_reg (_TAP_CFG0 ) |  0x41 )
247247
248248 self .set_embedded_functions (True , emb_ab )
249249
250-  def  read_mlc_output (self ):
250+  def  mlc_output (self ):
251251 buf  =  None 
252-  if  self .__read_reg (_MLC_STATUS ) &  0x1 :
253-  self .__read_reg (0x1A , size = 12 )
252+  if  self ._read_reg (_MLC_STATUS ) &  0x1 :
253+  self ._read_reg (0x1A , size = 12 )
254254 self .set_mem_bank (_FUNC_CFG_BANK_EMBED )
255-  buf  =  self .__read_reg (_MLC0_SRC , 8 )
255+  buf  =  self ._read_reg (_MLC0_SRC , 8 )
256256 self .set_mem_bank (_FUNC_CFG_BANK_USER )
257257 return  buf 
258258
259-  def  read_gyro (self ):
259+  def  gyro (self ):
260260 """Returns gyroscope vector in degrees/sec.""" 
261261 mv  =  memoryview (self .scratch_int )
262262 f  =  self .gyro_scale 
263-  self .__read_reg_into (_OUTX_L_G , mv )
263+  self ._read_reg_into (_OUTX_L_G , mv )
264264 return  (mv [0 ] /  f , mv [1 ] /  f , mv [2 ] /  f )
265265
266-  def  read_accel (self ):
266+  def  accel (self ):
267267 """Returns acceleration vector in gravity units (9.81m/s^2).""" 
268268 mv  =  memoryview (self .scratch_int )
269269 f  =  self .accel_scale 
270-  self .__read_reg_into (_OUTX_L_XL , mv )
270+  self ._read_reg_into (_OUTX_L_XL , mv )
271271 return  (mv [0 ] /  f , mv [1 ] /  f , mv [2 ] /  f )
0 commit comments