Skip to content

Commit b28544c

Browse files
committed
restructing
1 parent a3938a0 commit b28544c

13 files changed

+180
-150
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Introduction
66
:alt: Documentation Status
77

88
.. image:: https://img.shields.io/discord/327254708534116352.svg
9-
:target: https://discord.gg/nBQh6qu
9+
:target: https://adafru.it/discord
1010
:alt: Discord
1111

1212

@@ -65,11 +65,11 @@ Usage Example
6565
import time
6666
import board
6767
import busio
68-
import adafruit_lsm6ds
68+
from adafruit_lsm6ds.lsm6dsox import LSM6DSOX
6969
7070
i2c = busio.I2C(board.SCL, board.SDA)
7171
72-
sox = adafruit_lsm6ds.LSM6DSOX(i2c)
72+
sox = LSM6DSOX(i2c)
7373
7474
while True:
7575
print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2"%(sox.acceleration))

adafruit_lsm6ds.py renamed to adafruit_lsm6ds/__init__.py

Lines changed: 42 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -64,55 +64,19 @@
6464

6565
__version__ = "0.0.0-auto.0"
6666
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM6DSOX.git"
67+
68+
__version__ = "0.0.0-auto.0"
69+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM6DS.git"
70+
6771
from time import sleep
6872
from math import radians
6973
from micropython import const
7074
import adafruit_bus_device.i2c_device as i2c_device
75+
7176
from adafruit_register.i2c_struct import ROUnaryStruct, Struct
7277
from adafruit_register.i2c_bits import RWBits
7378
from adafruit_register.i2c_bit import RWBit
7479

75-
__version__ = "0.0.0-auto.0"
76-
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM6DS.git"
77-
78-
79-
_LSM6DS_DEFAULT_ADDRESS = const(0x6A)
80-
81-
_LSM6DS_CHIP_ID = const(0x6C)
82-
_ISM330DHCX_CHIP_ID = const(0x6B)
83-
_LSM6DS33_CHIP_ID = const(0x69)
84-
85-
_LSM6DS_FUNC_CFG_ACCESS = const(0x1)
86-
_LSM6DS_PIN_CTRL = const(0x2)
87-
_LSM6DS_UI_INT_OIS = const(0x6F)
88-
_LSM6DS_WHOAMI = const(0xF)
89-
_LSM6DS_CTRL1_XL = const(0x10)
90-
_LSM6DS_CTRL2_G = const(0x11)
91-
_LSM6DS_CTRL3_C = const(0x12)
92-
_LSM6DS_CTRL_5_C = const(0x14)
93-
_LSM6DS_MASTER_CONFIG = const(0x14)
94-
_LSM6DS_CTRL8_XL = const(0x17)
95-
_LSM6DS_CTRL9_XL = const(0x18)
96-
_LSM6DS_CTRL10_C = const(0x19)
97-
_LSM6DS_OUT_TEMP_L = const(0x20)
98-
_LSM6DS_OUT_TEMP_H = const(0x21)
99-
_LSM6DS_OUTX_L_G = const(0x22)
100-
_LSM6DS_OUTX_H_G = const(0x23)
101-
_LSM6DS_OUTY_L_G = const(0x24)
102-
_LSM6DS_OUTY_H_G = const(0x25)
103-
_LSM6DS_OUTZ_L_G = const(0x26)
104-
_LSM6DS_OUTZ_H_G = const(0x27)
105-
_LSM6DS_OUTX_L_A = const(0x28)
106-
_LSM6DS_OUTX_H_A = const(0x29)
107-
_LSM6DS_OUTY_L_A = const(0x2A)
108-
_LSM6DS_OUTY_H_A = const(0x2B)
109-
_LSM6DS_OUTZ_L_A = const(0x2C)
110-
_LSM6DS_OUTZ_H_A = const(0x2D)
111-
_LSM6DS_STEP_COUNTER = const(0x4B)
112-
_LSM6DS_TAP_CFG = const(0x58)
113-
114-
_MILLI_G_TO_ACCEL = 0.00980665
115-
11680

11781
class CV:
11882
"""struct helper"""
@@ -143,18 +107,6 @@ class GyroRange(CV):
143107
"""Options for ``gyro_data_range``"""
144108

145109

146-
GyroRange.add_values(
147-
(
148-
("RANGE_125_DPS", 125, 125, 4.375),
149-
("RANGE_250_DPS", 0, 250, 8.75),
150-
("RANGE_500_DPS", 1, 500, 17.50),
151-
("RANGE_1000_DPS", 2, 1000, 35.0),
152-
("RANGE_2000_DPS", 3, 2000, 70.0),
153-
("RANGE_4000_DPS", 4000, 4000, 140.0),
154-
)
155-
)
156-
157-
158110
class Rate(CV):
159111
"""Options for ``accelerometer_data_rate`` and ``gyro_data_rate``"""
160112

@@ -190,6 +142,25 @@ class AccelHPF(CV):
190142
)
191143
)
192144

145+
LSM6DS_DEFAULT_ADDRESS = const(0x6A)
146+
147+
LSM6DS_CHIP_ID = const(0x6C)
148+
149+
_LSM6DS_WHOAMI = const(0xF)
150+
_LSM6DS_CTRL1_XL = const(0x10)
151+
_LSM6DS_CTRL2_G = const(0x11)
152+
_LSM6DS_CTRL3_C = const(0x12)
153+
_LSM6DS_CTRL8_XL = const(0x17)
154+
_LSM6DS_CTRL9_XL = const(0x18)
155+
_LSM6DS_CTRL10_C = const(0x19)
156+
_LSM6DS_OUT_TEMP_L = const(0x20)
157+
_LSM6DS_OUTX_L_G = const(0x22)
158+
_LSM6DS_OUTX_L_A = const(0x28)
159+
_LSM6DS_STEP_COUNTER = const(0x4B)
160+
_LSM6DS_TAP_CFG = const(0x58)
161+
162+
_MILLI_G_TO_ACCEL = 0.00980665
163+
193164

194165
class LSM6DS: # pylint: disable=too-many-instance-attributes
195166

@@ -224,10 +195,12 @@ class LSM6DS: # pylint: disable=too-many-instance-attributes
224195
_pedometer_reset = RWBit(_LSM6DS_CTRL10_C, 1)
225196
_func_enable = RWBit(_LSM6DS_CTRL10_C, 2)
226197
_ped_enable = RWBit(_LSM6DS_TAP_CFG, 6)
227-
198+
pedometer_steps = ROUnaryStruct(_LSM6DS_STEP_COUNTER, "<h")
199+
"""The number of steps detected by the pedometer. You must enable with `pedometer_enable`
200+
before calling. Use `pedometer_reset` to reset the number of steps"""
228201
CHIP_ID = None
229202

230-
def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
203+
def __init__(self, i2c_bus, address=LSM6DS_DEFAULT_ADDRESS):
231204
self._cached_accel_range = None
232205
self._cached_gyro_range = None
233206

@@ -239,7 +212,7 @@ def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
239212
"Failed to find %s - check your wiring!" % self.__class__.__name__
240213
)
241214
self.reset()
242-
215+
self._add_gyro_ranges()
243216
self._bdu = True
244217

245218
self._add_accel_ranges()
@@ -255,6 +228,18 @@ def reset(self):
255228
while self._sw_reset:
256229
sleep(0.001)
257230

231+
@staticmethod
232+
def _add_gyro_ranges():
233+
GyroRange.add_values(
234+
(
235+
("RANGE_125_DPS", 125, 125, 4.375),
236+
("RANGE_250_DPS", 0, 250, 8.75),
237+
("RANGE_500_DPS", 1, 500, 17.50),
238+
("RANGE_1000_DPS", 2, 1000, 35.0),
239+
("RANGE_2000_DPS", 3, 2000, 70.0),
240+
)
241+
)
242+
258243
@staticmethod
259244
def _add_accel_ranges():
260245
AccelRange.add_values(
@@ -320,13 +305,10 @@ def gyro_range(self):
320305
def gyro_range(self, value):
321306
if not GyroRange.is_valid(value):
322307
raise AttributeError("range must be a `GyroRange`")
323-
if value is GyroRange.RANGE_4000_DPS and not isinstance(self, ISM330DHCX):
324-
raise AttributeError("4000 DPS is only available for ISM330DHCX")
325-
326308
if value is GyroRange.RANGE_125_DPS:
327309
self._gyro_range_125dps = True
328310
self._gyro_range_4000dps = False
329-
elif value is GyroRange.RANGE_4000_DPS:
311+
elif value == 4000:
330312
self._gyro_range_125dps = False
331313
self._gyro_range_4000dps = True
332314
else:
@@ -387,76 +369,3 @@ def high_pass_filter(self, value):
387369
if not AccelHPF.is_valid(value):
388370
raise AttributeError("range must be an `AccelHPF`")
389371
self._high_pass_filter = value
390-
391-
392-
class LSM6DSOX(LSM6DS): # pylint: disable=too-many-instance-attributes
393-
394-
"""Driver for the LSM6DSOX 6-axis accelerometer and gyroscope.
395-
396-
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DSOX is connected to.
397-
:param address: The I2C slave address of the sensor
398-
399-
"""
400-
401-
CHIP_ID = _LSM6DS_CHIP_ID
402-
403-
def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
404-
super().__init__(i2c_bus, address)
405-
self._i3c_disable = True
406-
407-
408-
class LSM6DSO32(LSM6DS): # pylint: disable=too-many-instance-attributes
409-
410-
"""Driver for the LSM6DSO32 6-axis accelerometer and gyroscope.
411-
412-
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DSO32 is connected to.
413-
:param address: The I2C slave address of the sensor
414-
415-
"""
416-
417-
CHIP_ID = _LSM6DS_CHIP_ID
418-
419-
def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
420-
super().__init__(i2c_bus, address)
421-
self._i3c_disable = True
422-
self.accelerometer_range = AccelRange.RANGE_8G # pylint:disable=no-member
423-
424-
def _add_accel_ranges(self):
425-
AccelRange.add_values(
426-
(
427-
("RANGE_4G", 0, 4, 0.122),
428-
("RANGE_32G", 1, 32, 0.976),
429-
("RANGE_8G", 2, 8, 0.244),
430-
("RANGE_16G", 3, 16, 0.488),
431-
)
432-
)
433-
434-
435-
class LSM6DS33(LSM6DS): # pylint: disable=too-many-instance-attributes
436-
437-
"""Driver for the LSM6DS33 6-axis accelerometer and gyroscope.
438-
439-
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DS33 is connected to.
440-
:param address: The I2C slave address of the sensor
441-
442-
"""
443-
444-
CHIP_ID = _LSM6DS33_CHIP_ID
445-
446-
447-
class ISM330DHCX(LSM6DS): # pylint: disable=too-many-instance-attributes
448-
449-
"""Driver for the LSM6DS33 6-axis accelerometer and gyroscope.
450-
451-
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DS33 is connected to.
452-
:param address: The I2C slave address of the sensor
453-
454-
"""
455-
456-
CHIP_ID = _ISM330DHCX_CHIP_ID
457-
458-
def __init__(self, i2c_bus, address=_LSM6DS_DEFAULT_ADDRESS):
459-
super().__init__(i2c_bus, address)
460-
461-
# Called DEVICE_CONF in the datasheet, but it recommends setting it
462-
self._i3c_disable = True

adafruit_lsm6ds/ism330dhcx.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
This module provides the ISM330DHCX subclass of LSM6DS for using ISM330DHCX sensors.
6+
"""
7+
from . import LSM6DS, LSM6DS_DEFAULT_ADDRESS, GyroRange
8+
9+
10+
class ISM330DHCX(LSM6DS): # pylint: disable=too-many-instance-attributes
11+
12+
"""Driver for the LSM6DS33 6-axis accelerometer and gyroscope.
13+
14+
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DS33 is connected to.
15+
:param address: The I2C slave address of the sensor
16+
17+
"""
18+
19+
CHIP_ID = 0x6B
20+
21+
def __init__(self, i2c_bus, address=LSM6DS_DEFAULT_ADDRESS):
22+
super().__init__(i2c_bus, address)
23+
24+
# Called DEVICE_CONF in the datasheet, but it recommends setting it
25+
self._i3c_disable = True
26+
27+
GyroRange.add_values(
28+
(
29+
("RANGE_125_DPS", 125, 125, 4.375),
30+
("RANGE_250_DPS", 0, 250, 8.75),
31+
("RANGE_500_DPS", 1, 500, 17.50),
32+
("RANGE_1000_DPS", 2, 1000, 35.0),
33+
("RANGE_2000_DPS", 3, 2000, 70.0),
34+
("RANGE_4000_DPS", 4000, 4000, 140.0),
35+
)
36+
)

adafruit_lsm6ds/lsm6ds33.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
This module provides the LSM6DS33 subclass of LSM6DS for using LSM6DS33 sensors.
6+
"""
7+
from . import LSM6DS
8+
9+
10+
class LSM6DS33(LSM6DS): # pylint: disable=too-many-instance-attributes
11+
12+
"""Driver for the LSM6DS33 6-axis accelerometer and gyroscope.
13+
14+
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DS33 is connected to.
15+
:param address: The I2C slave address of the sensor
16+
17+
"""
18+
19+
CHIP_ID = 0x69

adafruit_lsm6ds/lsm6dso32.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
This module provides the LSM6DSO32 subclass of LSM6DS for using LSM6DSO32 sensors.
6+
"""
7+
from . import LSM6DS, LSM6DS_CHIP_ID, LSM6DS_DEFAULT_ADDRESS, AccelRange
8+
9+
10+
class LSM6DSO32(LSM6DS): # pylint: disable=too-many-instance-attributes
11+
12+
"""Driver for the LSM6DSO32 6-axis accelerometer and gyroscope.
13+
14+
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DSO32 is connected to.
15+
:param address: The I2C slave address of the sensor
16+
17+
"""
18+
19+
CHIP_ID = LSM6DS_CHIP_ID
20+
21+
def __init__(self, i2c_bus, address=LSM6DS_DEFAULT_ADDRESS):
22+
super().__init__(i2c_bus, address)
23+
self._i3c_disable = True
24+
self.accelerometer_range = AccelRange.RANGE_8G # pylint:disable=no-member
25+
26+
def _add_accel_ranges(self):
27+
AccelRange.add_values(
28+
(
29+
("RANGE_4G", 0, 4, 0.122),
30+
("RANGE_32G", 1, 32, 0.976),
31+
("RANGE_8G", 2, 8, 0.244),
32+
("RANGE_16G", 3, 16, 0.488),
33+
)
34+
)

adafruit_lsm6ds/lsm6dsox.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
This module provides the LSM6DSOX subclass of LSM6DS for using LSM6DSOX sensors.
6+
"""
7+
from . import LSM6DS, LSM6DS_DEFAULT_ADDRESS, LSM6DS_CHIP_ID
8+
9+
10+
class LSM6DSOX(LSM6DS): # pylint: disable=too-many-instance-attributes
11+
12+
"""Driver for the LSM6DSOX 6-axis accelerometer and gyroscope.
13+
14+
:param ~busio.I2C i2c_bus: The I2C bus the LSM6DSOX is connected to.
15+
:param address: The I2C slave address of the sensor
16+
17+
"""
18+
19+
CHIP_ID = LSM6DS_CHIP_ID
20+
21+
def __init__(self, i2c_bus, address=LSM6DS_DEFAULT_ADDRESS):
22+
super().__init__(i2c_bus, address)
23+
self._i3c_disable = True

0 commit comments

Comments
 (0)