Skip to content

Commit 23c2c88

Browse files
committed
added option to change I2C address
- I2C & SPI classes's docs now have an explanation of what their constructor's parameters are. - added missing information in readme about how to install on Raspberry Pi -implemented I2C address checking to ensure proper functionality when passing non-default addresses
1 parent 7e30aad commit 23c2c88

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ bundles
99
*.DS_Store
1010
.eggs
1111
dist
12-
**/*.egg-info
12+
**/*.egg-info
13+
.vscode/settings.json

README.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ Please ensure all dependencies are available on the CircuitPython filesystem.
2727
This is easily achieved by downloading
2828
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.
2929

30+
Installing from PyPI
31+
=====================
32+
33+
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
34+
PyPI <https://pypi.org/project/adafruit-circuitpython-lsm9ds1/>`_. To install for current user:
35+
36+
.. code-block:: shell
37+
38+
pip3 install adafruit-circuitpython-lsm9ds1
39+
40+
To install system-wide (this may be required in some cases):
41+
42+
.. code-block:: shell
43+
44+
sudo pip3 install adafruit-circuitpython-lsm9ds1
45+
46+
To install in a virtual environment in your current project:
47+
48+
.. code-block:: shell
49+
50+
mkdir project-name && cd project-name
51+
python3 -m venv .env
52+
source .env/bin/activate
53+
pip3 install adafruit-circuitpython-lsm9ds1
54+
3055
Usage Example
3156
=============
3257

adafruit_lsm9ds1.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,24 @@ def _write_u8(self, sensor_type, address, val):
363363

364364

365365
class LSM9DS1_I2C(LSM9DS1):
366-
"""Driver for the LSM9DS1 connect over I2C."""
366+
"""Driver for the LSM9DS1 connect over I2C.
367367
368-
def __init__(self, i2c):
369-
self._mag_device = i2c_device.I2CDevice(i2c, _LSM9DS1_ADDRESS_MAG)
370-
self._xg_device = i2c_device.I2CDevice(i2c, _LSM9DS1_ADDRESS_ACCELGYRO)
371-
super().__init__()
368+
:param ~busio.I2C i2c: The I2C bus object used to connect to the LSM9DS1.
369+
370+
.. note:: This object should be shared among other driver classes that use the same I2C bus (SDA & SCL pins) to connect to different I2C devices.
371+
372+
:param int mag_address: A 16-bit integer that represents the i2c address of the LSM9DS1's magnetometer. Options are limited to ``0x1C`` or ``0x1E``. Defaults to ``0x1E``.
373+
:param int xg_address: A 16-bit integer that represents the i2c address of the LSM9DS1's accelerometer and gyroscope. Options are limited to ``0x6A`` or ``0x6B``. Defaults to ``0x6B``.
374+
375+
"""
376+
377+
def __init__(self, i2c, mag_address=_LSM9DS1_ADDRESS_MAG, xg_address=_LSM9DS1_ADDRESS_ACCELGYRO):
378+
if mag_address in (0x1c, 0x1e) and xg_address in (0x6a, 0x6b):
379+
self._mag_device = i2c_device.I2CDevice(i2c, mag_address)
380+
self._xg_device = i2c_device.I2CDevice(i2c, xg_address)
381+
super().__init__()
382+
else:
383+
raise ValueError('address parmeters are incorrect. Read the docs at https://circuitpython.readthedocs.io/projects/lsm9ds1/en/latest/api.html#adafruit_lsm9ds1.LSM9DS1_I2C')
372384

373385
def _read_u8(self, sensor_type, address):
374386
if sensor_type == _MAGTYPE:
@@ -401,7 +413,16 @@ def _write_u8(self, sensor_type, address, val):
401413

402414

403415
class LSM9DS1_SPI(LSM9DS1):
404-
"""Driver for the LSM9DS1 connect over SPI."""
416+
"""Driver for the LSM9DS1 connect over SPI.
417+
418+
:param ~busio.SPI spi: The SPI bus object used to connect to the LSM9DS1.
419+
420+
.. note:: This object should be shared among other driver classes that use the same SPI bus (SCK, MISO, MOSI pins) to connect to different SPI devices.
421+
422+
:param ~digitalio.DigitalInOut mcs: The digital output pin connected to the LSM9DS1's CSM (Chip Select Magnetometer) pin.
423+
:param ~digitalio.DigitalInOut xgcs: The digital output pin connected to the LSM9DS1's CSAG (Chip Select Accelerometer/Gyroscope) pin.
424+
425+
"""
405426
# pylint: disable=no-member
406427
def __init__(self, spi, xgcs, mcs):
407428
self._mag_device = spi_device.SPIDevice(spi, mcs, baudrate=200000, phase=1, polarity=1)

0 commit comments

Comments
 (0)