5
5
The MIT License (MIT)
6
6
7
7
Copyright (c) 2021 Damien P. George
8
- Copyright (c) 2021-2022 Ibrahim Abdelkader <[email protected] >
8
+ Copyright (c) 2021-2023 Ibrahim Abdelkader <[email protected] >
9
9
10
10
Permission is hereby granted, free of charge, to any person obtaining a copy
11
11
of this software and associated documentation files (the "Software"), to deal
35
35
lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12)))
36
36
37
37
# Or init in SPI mode.
38
- #lsm = LSM6DSOX(SPI(5), cs_pin =Pin(10))
38
+ #lsm = LSM6DSOX(SPI(5), cs =Pin(10))
39
39
40
40
while (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 ()))
43
43
print("")
44
44
time.sleep_ms(100)
45
45
"""
46
46
47
47
import array
48
48
from micropython import const
49
49
50
+ _CTRL3_C = const (0x12 )
51
+ _CTRL1_XL = const (0x10 )
52
+ _CTRL8_XL = const (0x17 )
53
+ _CTRL9_XL = const (0x18 )
50
54
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 )
56
57
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 )
59
61
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 )
63
64
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 )
66
69
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 )
71
73
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 )
75
76
76
- _EMB_FUNC_EN_A = const (0x04 )
77
- _EMB_FUNC_EN_B = const (0x05 )
78
77
78
+ class LSM6DSOX :
79
79
def __init__ (
80
80
self ,
81
81
bus ,
82
- cs_pin = None ,
82
+ cs = None ,
83
83
address = _DEFAULT_ADDR ,
84
84
gyro_odr = 104 ,
85
85
accel_odr = 104 ,
@@ -95,15 +95,15 @@ def __init__(
95
95
ucf: MLC program to load.
96
96
"""
97
97
self .bus = bus
98
- self .cs_pin = cs_pin
98
+ self .cs = cs
99
99
self .address = address
100
100
self ._use_i2c = hasattr (self .bus , "readfrom_mem" )
101
101
102
- if not self ._use_i2c and cs_pin is None :
102
+ if not self ._use_i2c and cs is None :
103
103
raise ValueError ("A CS pin must be provided in SPI mode" )
104
104
105
105
# 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 :
107
107
raise OSError ("No LSM6DS device was found at address 0x%x" % (self .address ))
108
108
109
109
# allocate scratch buffer for efficient conversions and memread op's
@@ -131,7 +131,7 @@ def __init__(
131
131
132
132
# Sanity checks
133
133
if not gyro_odr in ODR :
134
- raise ValueError ("Invalid sampling rate: %d" % accel_odr )
134
+ raise ValueError ("Invalid sampling rate: %d" % gyro_odr )
135
135
if not gyro_scale in SCALE_GYRO :
136
136
raise ValueError ("invalid gyro scaling: %d" % gyro_scale )
137
137
if not accel_odr in ODR :
@@ -148,74 +148,74 @@ def __init__(
148
148
149
149
# Set Gyroscope datarate and scale.
150
150
# 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 )
152
152
153
153
# Enable LPF2 and HPF fast-settling mode, ODR/4
154
- self .__write_reg (_CTRL8_XL , 0x09 )
154
+ self ._write_reg (_CTRL8_XL , 0x09 )
155
155
156
156
# 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 )
158
158
159
159
self .gyro_scale = 32768 / gyro_scale
160
160
self .accel_scale = 32768 / accel_scale
161
161
162
- def __read_reg (self , reg , size = 1 ):
162
+ def _read_reg (self , reg , size = 1 ):
163
163
if self ._use_i2c :
164
164
buf = self .bus .readfrom_mem (self .address , reg , size )
165
165
else :
166
166
try :
167
- self .cs_pin (0 )
167
+ self .cs (0 )
168
168
self .bus .write (bytes ([reg | 0x80 ]))
169
169
buf = self .bus .read (size )
170
170
finally :
171
- self .cs_pin (1 )
171
+ self .cs (1 )
172
172
if size == 1 :
173
173
return int (buf [0 ])
174
174
return [int (x ) for x in buf ]
175
175
176
- def __write_reg (self , reg , val ):
176
+ def _write_reg (self , reg , val ):
177
177
if self ._use_i2c :
178
178
self .bus .writeto_mem (self .address , reg , bytes ([val ]))
179
179
else :
180
180
try :
181
- self .cs_pin (0 )
181
+ self .cs (0 )
182
182
self .bus .write (bytes ([reg , val ]))
183
183
finally :
184
- self .cs_pin (1 )
184
+ self .cs (1 )
185
185
186
- def __read_reg_into (self , reg , buf ):
186
+ def _read_reg_into (self , reg , buf ):
187
187
if self ._use_i2c :
188
188
self .bus .readfrom_mem_into (self .address , reg , buf )
189
189
else :
190
190
try :
191
- self .cs_pin (0 )
191
+ self .cs (0 )
192
192
self .bus .write (bytes ([reg | 0x80 ]))
193
193
self .bus .readinto (buf )
194
194
finally :
195
- self .cs_pin (1 )
195
+ self .cs (1 )
196
196
197
197
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 )
199
199
for i in range (0 , 10 ):
200
- if (self .__read_reg (_CTRL3_C ) & 0x01 ) == 0 :
200
+ if (self ._read_reg (_CTRL3_C ) & 0x01 ) == 0 :
201
201
return
202
202
time .sleep_ms (10 )
203
203
raise OSError ("Failed to reset LSM6DS device." )
204
204
205
205
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 ))
208
208
209
209
def set_embedded_functions (self , enable , emb_ab = None ):
210
210
self .set_mem_bank (_FUNC_CFG_BANK_EMBED )
211
211
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 ])
214
214
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 ))
219
219
emb_ab = (emb_a , emb_b )
220
220
221
221
self .set_mem_bank (_FUNC_CFG_BANK_USER )
@@ -227,45 +227,45 @@ def load_mlc(self, ucf):
227
227
for l in ucf_file :
228
228
if l .startswith ("Ac" ):
229
229
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 ])
231
231
232
232
emb_ab = self .set_embedded_functions (False )
233
233
234
234
# 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 )
236
236
237
237
# 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 )
239
239
240
240
# Route signals on interrupt pin 1
241
241
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 )
243
243
self .set_mem_bank (_FUNC_CFG_BANK_USER )
244
244
245
245
# 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 )
247
247
248
248
self .set_embedded_functions (True , emb_ab )
249
249
250
- def read_mlc_output (self ):
250
+ def mlc_output (self ):
251
251
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 )
254
254
self .set_mem_bank (_FUNC_CFG_BANK_EMBED )
255
- buf = self .__read_reg (_MLC0_SRC , 8 )
255
+ buf = self ._read_reg (_MLC0_SRC , 8 )
256
256
self .set_mem_bank (_FUNC_CFG_BANK_USER )
257
257
return buf
258
258
259
- def read_gyro (self ):
259
+ def gyro (self ):
260
260
"""Returns gyroscope vector in degrees/sec."""
261
261
mv = memoryview (self .scratch_int )
262
262
f = self .gyro_scale
263
- self .__read_reg_into (_OUTX_L_G , mv )
263
+ self ._read_reg_into (_OUTX_L_G , mv )
264
264
return (mv [0 ] / f , mv [1 ] / f , mv [2 ] / f )
265
265
266
- def read_accel (self ):
266
+ def accel (self ):
267
267
"""Returns acceleration vector in gravity units (9.81m/s^2)."""
268
268
mv = memoryview (self .scratch_int )
269
269
f = self .accel_scale
270
- self .__read_reg_into (_OUTX_L_XL , mv )
270
+ self ._read_reg_into (_OUTX_L_XL , mv )
271
271
return (mv [0 ] / f , mv [1 ] / f , mv [2 ] / f )
0 commit comments