Skip to content

Commit 42e454e

Browse files
authored
Merge pull request #2 from adafruit/lsm6ds_refactor
Lsm6ds refactor
2 parents 69d87f0 + 116162e commit 42e454e

12 files changed

+228
-158
lines changed

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ Usage Example
6161
import time
6262
import board
6363
import busio
64-
import adafruit_lsm6dsox
64+
import adafruit_lsm6ds
6565
6666
i2c = busio.I2C(board.SCL, board.SDA)
6767
68-
sox = adafruit_lsm6dsox.LSM6DSOX(i2c)
68+
sox = adafruit_lsm6ds.LSM6DSOX(i2c)
6969
7070
while True:
7171
print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2"%(sox.acceleration))

adafruit_lsm6dsox.py renamed to adafruit_lsm6ds.py

+133-100
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_lsm6dsox`
23+
`adafruit_lsm6ds`
2424
================================================================================
2525
2626
CircuitPython library for the ST LSM6DSOX 6-axis Accelerometer and Gyro
@@ -58,35 +58,36 @@
5858
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM6DSOX.git"
5959

6060

61-
_LSM6DSOX_DEFAULT_ADDRESS = const(0x6a)
62-
_LSM6DSOX_CHIP_ID = const(0x6C)
63-
_ISM330DHCX_CHIP_ID = const(0x6B)
64-
65-
66-
_LSM6DSOX_FUNC_CFG_ACCESS = const(0x1)
67-
_LSM6DSOX_PIN_CTRL = const(0x2)
68-
_LSM6DSOX_UI_INT_OIS = const(0x6F)
69-
_LSM6DSOX_WHOAMI = const(0xF)
70-
_LSM6DSOX_CTRL1_XL = const(0x10)
71-
_LSM6DSOX_CTRL2_G = const(0x11)
72-
_LSM6DSOX_CTRL3_C = const(0x12)
73-
_LSM6DSOX_CTRL_5_C = const(0x14)
74-
_LSM6DSOX_MASTER_CONFIG = const(0x14)
75-
_LSM6DSOX_CTRL9_XL = const(0x18)
76-
_LSM6DSOX_OUT_TEMP_L = const(0x20)
77-
_LSM6DSOX_OUT_TEMP_H = const(0x21)
78-
_LSM6DSOX_OUTX_L_G = const(0x22)
79-
_LSM6DSOX_OUTX_H_G = const(0x23)
80-
_LSM6DSOX_OUTY_L_G = const(0x24)
81-
_LSM6DSOX_OUTY_H_G = const(0x25)
82-
_LSM6DSOX_OUTZ_L_G = const(0x26)
83-
_LSM6DSOX_OUTZ_H_G = const(0x27)
84-
_LSM6DSOX_OUTX_L_A = const(0x28)
85-
_LSM6DSOX_OUTX_H_A = const(0x29)
86-
_LSM6DSOX_OUTY_L_A = const(0x2A)
87-
_LSM6DSOX_OUTY_H_A = const(0x2B)
88-
_LSM6DSOX_OUTZ_L_A = const(0x2C)
89-
_LSM6DSOX_OUTZ_H_A = const(0x2D)
61+
_LSM6DS_DEFAULT_ADDRESS = const(0x6a)
62+
63+
_LSM6DS_CHIP_ID = const(0x6C)
64+
_ISM330DHCT_CHIP_ID = const(0x6B)
65+
_LSM6DS33_CHIP_ID = const(0x69)
66+
67+
_LSM6DS_FUNC_CFG_ACCESS = const(0x1)
68+
_LSM6DS_PIN_CTRL = const(0x2)
69+
_LSM6DS_UI_INT_OIS = const(0x6F)
70+
_LSM6DS_WHOAMI = const(0xF)
71+
_LSM6DS_CTRL1_XL = const(0x10)
72+
_LSM6DS_CTRL2_G = const(0x11)
73+
_LSM6DS_CTRL3_C = const(0x12)
74+
_LSM6DS_CTRL_5_C = const(0x14)
75+
_LSM6DS_MASTER_CONFIG = const(0x14)
76+
_LSM6DS_CTRL9_XL = const(0x18)
77+
_LSM6DS_OUT_TEMP_L = const(0x20)
78+
_LSM6DS_OUT_TEMP_H = const(0x21)
79+
_LSM6DS_OUTX_L_G = const(0x22)
80+
_LSM6DS_OUTX_H_G = const(0x23)
81+
_LSM6DS_OUTY_L_G = const(0x24)
82+
_LSM6DS_OUTY_H_G = const(0x25)
83+
_LSM6DS_OUTZ_L_G = const(0x26)
84+
_LSM6DS_OUTZ_H_G = const(0x27)
85+
_LSM6DS_OUTX_L_A = const(0x28)
86+
_LSM6DS_OUTX_H_A = const(0x29)
87+
_LSM6DS_OUTY_L_A = const(0x2A)
88+
_LSM6DS_OUTY_H_A = const(0x2B)
89+
_LSM6DS_OUTZ_L_A = const(0x2C)
90+
_LSM6DS_OUTZ_H_A = const(0x2D)
9091

9192
_MILLI_G_TO_ACCEL = 0.00980665
9293

@@ -153,7 +154,8 @@ class Rate(CV):
153154
('RATE_1_6_HZ', 11, 1.6, None)
154155
))
155156

156-
class LSM6DSOX: #pylint: disable=too-many-instance-attributes
157+
158+
class LSM6DS: #pylint: disable=too-many-instance-attributes
157159

158160
"""Driver for the LSM6DSOX 6-axis accelerometer and gyroscope.
159161
@@ -163,73 +165,72 @@ class LSM6DSOX: #pylint: disable=too-many-instance-attributes
163165
"""
164166

165167
#ROUnaryStructs:
166-
_chip_id = ROUnaryStruct(_LSM6DSOX_WHOAMI, "<b")
167-
_temperature = ROUnaryStruct(_LSM6DSOX_OUT_TEMP_L, "<h")
168+
_chip_id = ROUnaryStruct(_LSM6DS_WHOAMI, "<b")
169+
_temperature = ROUnaryStruct(_LSM6DS_OUT_TEMP_L, "<h")
168170

169171
#RWBits:
170-
_ois_ctrl_from_ui = RWBit(_LSM6DSOX_FUNC_CFG_ACCESS, 0)
171-
_shub_reg_access = RWBit(_LSM6DSOX_FUNC_CFG_ACCESS, 6)
172-
_func_cfg_access = RWBit(_LSM6DSOX_FUNC_CFG_ACCESS, 7)
173-
_sdo_pu_en = RWBit(_LSM6DSOX_PIN_CTRL, 6)
174-
_ois_pu_dis = RWBit(_LSM6DSOX_PIN_CTRL, 7)
175-
_spi2_read_en = RWBit(_LSM6DSOX_UI_INT_OIS, 3)
176-
_den_lh_ois = RWBit(_LSM6DSOX_UI_INT_OIS, 5)
177-
_lvl2_ois = RWBit(_LSM6DSOX_UI_INT_OIS, 6)
178-
_int2_drdy_ois = RWBit(_LSM6DSOX_UI_INT_OIS, 7)
179-
_lpf_xl = RWBit(_LSM6DSOX_CTRL1_XL, 1)
180-
181-
_accel_range = RWBits(2, _LSM6DSOX_CTRL1_XL, 2)
182-
_accel_data_rate = RWBits(4, _LSM6DSOX_CTRL1_XL, 4)
183-
184-
_gyro_data_rate = RWBits(4, _LSM6DSOX_CTRL2_G, 4)
185-
_gyro_range = RWBits(2, _LSM6DSOX_CTRL2_G, 2)
186-
_gyro_range_125dps = RWBit(_LSM6DSOX_CTRL2_G, 1)
187-
188-
_sw_reset = RWBit(_LSM6DSOX_CTRL3_C, 0)
189-
_if_inc = RWBit(_LSM6DSOX_CTRL3_C, 2)
190-
_sim = RWBit(_LSM6DSOX_CTRL3_C, 3)
191-
_pp_od = RWBit(_LSM6DSOX_CTRL3_C, 4)
192-
_h_lactive = RWBit(_LSM6DSOX_CTRL3_C, 5)
193-
_bdu = RWBit(_LSM6DSOX_CTRL3_C, 6)
194-
_boot = RWBit(_LSM6DSOX_CTRL3_C, 7)
195-
_st_xl = RWBits(2, _LSM6DSOX_CTRL_5_C, 0)
196-
_st_g = RWBits(2, _LSM6DSOX_CTRL_5_C, 2)
197-
_rounding_status = RWBit(_LSM6DSOX_CTRL_5_C, 4)
198-
_rounding = RWBits(2, _LSM6DSOX_CTRL_5_C, 5)
199-
_xl_ulp_en = RWBit(_LSM6DSOX_CTRL_5_C, 7)
200-
_aux_sens_on = RWBits(2, _LSM6DSOX_MASTER_CONFIG, 0)
201-
_master_on = RWBit(_LSM6DSOX_MASTER_CONFIG, 2)
202-
_shub_pu_en = RWBit(_LSM6DSOX_MASTER_CONFIG, 3)
203-
_pass_through_mode = RWBit(_LSM6DSOX_MASTER_CONFIG, 4)
204-
_start_config = RWBit(_LSM6DSOX_MASTER_CONFIG, 5)
205-
_write_once = RWBit(_LSM6DSOX_MASTER_CONFIG, 6)
206-
_rst_master_regs = RWBit(_LSM6DSOX_MASTER_CONFIG, 7)
207-
_i3c_disable = RWBit(_LSM6DSOX_CTRL9_XL, 1)
208-
209-
_raw_temp = ROUnaryStruct(_LSM6DSOX_OUT_TEMP_L, "<h")
210-
211-
_raw_accel_data = Struct(_LSM6DSOX_OUTX_L_A, "<hhh")
212-
_raw_gyro_data = Struct(_LSM6DSOX_OUTX_L_G, "<hhh")
213-
214-
def __init__(self, i2c_bus, address=_LSM6DSOX_DEFAULT_ADDRESS):
215-
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
172+
_ois_ctrl_from_ui = RWBit(_LSM6DS_FUNC_CFG_ACCESS, 0)
173+
_shub_reg_access = RWBit(_LSM6DS_FUNC_CFG_ACCESS, 6)
174+
_func_cfg_access = RWBit(_LSM6DS_FUNC_CFG_ACCESS, 7)
175+
_sdo_pu_en = RWBit(_LSM6DS_PIN_CTRL, 6)
176+
_ois_pu_dis = RWBit(_LSM6DS_PIN_CTRL, 7)
177+
_spi2_read_en = RWBit(_LSM6DS_UI_INT_OIS, 3)
178+
_den_lh_ois = RWBit(_LSM6DS_UI_INT_OIS, 5)
179+
_lvl2_ois = RWBit(_LSM6DS_UI_INT_OIS, 6)
180+
_int2_drdy_ois = RWBit(_LSM6DS_UI_INT_OIS, 7)
181+
_lpf_xl = RWBit(_LSM6DS_CTRL1_XL, 1)
182+
183+
_accel_range = RWBits(2, _LSM6DS_CTRL1_XL, 2)
184+
_accel_data_rate = RWBits(4, _LSM6DS_CTRL1_XL, 4)
185+
186+
_gyro_data_rate = RWBits(4, _LSM6DS_CTRL2_G, 4)
187+
_gyro_range = RWBits(2, _LSM6DS_CTRL2_G, 2)
188+
_gyro_range_125dps = RWBit(_LSM6DS_CTRL2_G, 1)
189+
_gyro_range_4000dps = RWBit(_LSM6DS_CTRL2_G, 0)
190+
191+
_sw_reset = RWBit(_LSM6DS_CTRL3_C, 0)
192+
_sim = RWBit(_LSM6DS_CTRL3_C, 3)
193+
_pp_od = RWBit(_LSM6DS_CTRL3_C, 4)
194+
_h_lactive = RWBit(_LSM6DS_CTRL3_C, 5)
195+
_bdu = RWBit(_LSM6DS_CTRL3_C, 6)
196+
_boot = RWBit(_LSM6DS_CTRL3_C, 7)
197+
_st_xl = RWBits(2, _LSM6DS_CTRL_5_C, 0)
198+
_st_g = RWBits(2, _LSM6DS_CTRL_5_C, 2)
199+
_rounding_status = RWBit(_LSM6DS_CTRL_5_C, 4)
200+
_rounding = RWBits(2, _LSM6DS_CTRL_5_C, 5)
201+
_xl_ulp_en = RWBit(_LSM6DS_CTRL_5_C, 7)
202+
_aux_sens_on = RWBits(2, _LSM6DS_MASTER_CONFIG, 0)
203+
_master_on = RWBit(_LSM6DS_MASTER_CONFIG, 2)
204+
_shub_pu_en = RWBit(_LSM6DS_MASTER_CONFIG, 3)
205+
_pass_through_mode = RWBit(_LSM6DS_MASTER_CONFIG, 4)
206+
_start_config = RWBit(_LSM6DS_MASTER_CONFIG, 5)
207+
_write_once = RWBit(_LSM6DS_MASTER_CONFIG, 6)
208+
_rst_master_regs = RWBit(_LSM6DS_MASTER_CONFIG, 7)
209+
_i3c_disable = RWBit(_LSM6DS_CTRL9_XL, 1)
210+
211+
_raw_temp = ROUnaryStruct(_LSM6DS_OUT_TEMP_L, "<h")
212+
213+
_raw_accel_data = Struct(_LSM6DS_OUTX_L_A, "<hhh")
214+
_raw_gyro_data = Struct(_LSM6DS_OUTX_L_G, "<hhh")
215+
CHIP_ID = None
216+
def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
217+
self._cached_accel_range = None
218+
self._cached_gyro_range = None
216219

217-
if self._chip_id not in [_LSM6DSOX_CHIP_ID, _ISM330DHCX_CHIP_ID]:
218-
raise RuntimeError("Failed to find LSM6DSOX or ISM330DHCX - check your wiring!")
220+
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
221+
if self.CHIP_ID is None:
222+
raise AttributeError("LSM6DS Parent Class cannot be directly instantiated")
223+
if self._chip_id != self.CHIP_ID:
224+
raise RuntimeError("Failed to find %s - check your wiring!"%self.__class__.__name__)
219225
self.reset()
220226

221227
self._bdu = True
222-
self._i3c_disable = True
223-
self._if_inc = True
224228

225-
self._accel_data_rate = Rate.RATE_104_HZ #pylint: disable=no-member
226-
self._gyro_data_rate = Rate.RATE_104_HZ #pylint: disable=no-member
227-
228-
self._accel_range = AccelRange.RANGE_4G #pylint: disable=no-member
229-
self._cached_accel_range = self._accel_range
230-
self._gyro_range = GyroRange.RANGE_250_DPS #pylint: disable=no-member
231-
self._cached_gyro_range = self._gyro_range
229+
self.accelerometer_data_rate = Rate.RATE_104_HZ #pylint: disable=no-member
230+
self.gyro_data_rate = Rate.RATE_104_HZ #pylint: disable=no-member
232231

232+
self.accelerometer_range = AccelRange.RANGE_4G #pylint: disable=no-member
233+
self.gyro_range = GyroRange.RANGE_250_DPS #pylint: disable=no-member
233234

234235
def reset(self):
235236
"Resets the sensor's configuration into an initial state"
@@ -240,12 +241,6 @@ def reset(self):
240241
while self._boot:
241242
sleep(0.001)
242243

243-
@property
244-
def is_lsm6dsox(self):
245-
"""Returns `True` if the connected sensor is an LSM6DSOX,
246-
`False` if not, it's an ICM330DHCX"""
247-
return self._chip_id is _LSM6DSOX_CHIP_ID
248-
249244
@property
250245
def acceleration(self):
251246
"""The x, y, z acceleration values returned in a 3-tuple and are in m / s ^ 2."""
@@ -291,15 +286,15 @@ def accelerometer_range(self, value):
291286
def gyro_range(self):
292287
"""Adjusts the range of values that the sensor can measure, from 125 Degrees/second to 4000
293288
degrees/s. Note that larger ranges will be less accurate. Must be a `GyroRange`. 4000 DPS
294-
is only available for the ISM330DHCX"""
289+
is only available for the ISM330DHCT"""
295290
return self._cached_gyro_range
296291

297292
@gyro_range.setter
298293
def gyro_range(self, value):
299294
if not GyroRange.is_valid(value):
300295
raise AttributeError("range must be a `GyroRange`")
301-
if value is GyroRange.RANGE_4000_DPS and self.is_lsm6dsox:
302-
raise AttributeError("4000 DPS is only available for ISM330DHCX")
296+
if value is GyroRange.RANGE_4000_DPS and not isinstance(self, ISM330DHCT):
297+
raise AttributeError("4000 DPS is only available for ISM330DHCT")
303298

304299
if value is GyroRange.RANGE_125_DPS:
305300
self._gyro_range_125dps = True
@@ -309,7 +304,7 @@ def gyro_range(self, value):
309304
self._gyro_range_4000dps = True
310305
else:
311306
self._gyro_range_125dps = False
312-
self._gyro_range_4000dps = True
307+
self._gyro_range_4000dps = False
313308
self._gyro_range = value
314309

315310
self._cached_gyro_range = value
@@ -344,3 +339,41 @@ def gyro_data_rate(self, value):
344339

345340
self._gyro_data_rate = value
346341
# sleep(.2) # needed to let new range settle
342+
343+
class LSM6DSOX(LSM6DS): #pylint: disable=too-many-instance-attributes
344+
345+
"""Driver for the LSM6DSOX 6-axis accelerometer and gyroscope.
346+
347+
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DSOX is connected to.
348+
:param address: The I2C slave address of the sensor
349+
350+
"""
351+
CHIP_ID = _LSM6DS_CHIP_ID
352+
def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
353+
super().__init__(i2c_bus, address)
354+
self._i3c_disable = True
355+
356+
class LSM6DS33(LSM6DS): #pylint: disable=too-many-instance-attributes
357+
358+
"""Driver for the LSM6DS33 6-axis accelerometer and gyroscope.
359+
360+
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DS33 is connected to.
361+
:param address: The I2C slave address of the sensor
362+
363+
"""
364+
CHIP_ID = _LSM6DS33_CHIP_ID
365+
366+
class ISM330DHCT(LSM6DS): #pylint: disable=too-many-instance-attributes
367+
368+
"""Driver for the LSM6DS33 6-axis accelerometer and gyroscope.
369+
370+
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DS33 is connected to.
371+
:param address: The I2C slave address of the sensor
372+
373+
"""
374+
CHIP_ID = _ISM330DHCT_CHIP_ID
375+
def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
376+
super().__init__(i2c_bus, address)
377+
378+
# Called DEVICE_CONF in the datasheet, but it recommends setting it
379+
self._i3c_disable = True

docs/api.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7-
.. automodule:: adafruit_lsm6dsox
7+
.. automodule:: adafruit_lsm6ds
88
:members:

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Uncomment the below if you use native CircuitPython modules such as
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
23-
# autodoc_mock_imports = ["digitalio", "busio"]
23+
autodoc_mock_imports = ["micropython", "adafruit_bus_device", "adafruit_register"]
2424

2525

2626
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'Register': ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

examples/ism330dhct_simpletest.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import time
2+
import board
3+
import busio
4+
from adafruit_lsm6ds import ISM330DHCT
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
8+
sensor = ISM330DHCT(i2c)
9+
10+
while True:
11+
print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (sensor.acceleration))
12+
print("Gyro X:%.2f, Y: %.2f, Z: %.2f degrees/s" % (sensor.gyro))
13+
print("")
14+
time.sleep(0.5)

examples/lsm6ds33_simpletest.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import time
2+
import board
3+
import busio
4+
from adafruit_lsm6ds import LSM6DS33
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
8+
sensor = LSM6DS33(i2c)
9+
10+
while True:
11+
print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (sensor.acceleration))
12+
print("Gyro X:%.2f, Y: %.2f, Z: %.2f degrees/s" % (sensor.gyro))
13+
print("")
14+
time.sleep(0.5)

examples/lsm6ds_full_test.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import time
2+
import board
3+
import busio
4+
#pylint:disable=no-member,unused-import
5+
from adafruit_lsm6ds import LSM6DS33, LSM6DSOX, ISM330DHCT, Rate, AccelRange, GyroRange
6+
7+
i2c = busio.I2C(board.SCL, board.SDA)
8+
9+
sensor = LSM6DS33(i2c)
10+
#sensor = LSM6DSOX(i2c)
11+
#sensor = ISM330DHCT(i2c)
12+
13+
sensor.accelerometer_range = AccelRange.RANGE_8G
14+
print("Accelerometer range set to: %d G" % AccelRange.string[sensor.accelerometer_range])
15+
16+
sensor.gyro_range = GyroRange.RANGE_2000_DPS
17+
print("Gyro range set to: %d DPS" % GyroRange.string[sensor.gyro_range])
18+
19+
sensor.accelerometer_data_rate = Rate.RATE_1_66K_HZ
20+
#sensor.accelerometer_data_rate = Rate.RATE_12_5_HZ
21+
print("Accelerometer rate set to: %d HZ" % Rate.string[sensor.accelerometer_data_rate])
22+
23+
sensor.gyro_data_rate = Rate.RATE_1_66K_HZ
24+
print("Gyro rate set to: %d HZ" % Rate.string[sensor.gyro_data_rate])
25+
26+
while True:
27+
print("Accel X:%.2f Y:%.2f Z:%.2f ms^2 Gyro X:%.2f Y:%.2f Z:%.2f degrees/s" %
28+
(sensor.acceleration+sensor.gyro))
29+
time.sleep(0.05)

0 commit comments

Comments
 (0)