Skip to content

Commit b7a1853

Browse files
committed
Added spi support
1 parent a3b960a commit b7a1853

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Introduction
1313
:target: https://travis-ci.org/adafruit/adafruit_CircuitPython_l3gd20
1414
:alt: Build Status
1515

16-
.. todo:: Describe what the library does.
16+
Adafruit 9-DOF Absolute Orientation IMU Fusion Breakout - L3GD20 Driver
1717

1818
Dependencies
1919
=============

adafruit_l3gd20.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
3636
**Hardware:**
3737
38-
* `L3GD20H Triple-Axis Gyro Breakout Board - L3GD20/L3G4200 <https://www.adafruit.com/product/1032>`_
38+
* `L3GD20H Triple-Axis Gyro Breakout Board <https://www.adafruit.com/product/1032>`_
3939
4040
**Software and Dependencies:**
4141
@@ -54,6 +54,10 @@
5454

5555
from micropython import const
5656
from adafruit_register.i2c_struct import Struct
57+
try:
58+
from struct import unpack
59+
except ImportError:
60+
from ustruct import unpack
5761

5862
__version__ = "0.0.0-auto.0"
5963
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_L3GD20.git"
@@ -84,9 +88,6 @@ class L3GD20: # pylint: disable=no-member
8488
L3DS20_RANGE_2000DPS
8589
"""
8690

87-
acceleration_raw = Struct((_L3GD20_REGISTER_OUT_X_L | 0x80), '<hhh')
88-
"""Gives the raw acceleration readings, in units of the scaled mdps."""
89-
9091
def __init__(self, rng):
9192
chip_id = self.read_register(_ID_REGISTER)
9293
if chip_id != _L3GD20_CHIP_ID and chip_id != _L3GD20H_CHIP_ID:
@@ -194,6 +195,10 @@ class L3GD20_I2C(L3GD20):
194195
L3DS20_RANGE_2000DPS
195196
:param address: the optional device address, 0x68 is the default address
196197
"""
198+
199+
acceleration_raw = Struct((_L3GD20_REGISTER_OUT_X_L | 0x80), '<hhh')
200+
"""Gives the raw acceleration readings, in units of the scaled mdps."""
201+
197202
def __init__(self, i2c, rng=L3DS20_RANGE_250DPS, address=0x6B):
198203
import adafruit_bus_device.i2c_device as i2c_device
199204
self.i2c_device = i2c_device.I2CDevice(i2c, address)
@@ -237,6 +242,8 @@ class L3GD20_SPI(L3GD20):
237242
def __init__(self, spi_busio, cs, rng=L3DS20_RANGE_250DPS, baudrate=100000):
238243
import adafruit_bus_device.spi_device as spi_device
239244
self._spi = spi_device.SPIDevice(spi_busio, cs, baudrate=baudrate)
245+
self._spi_bytearray1 = bytearray(1)
246+
self._spi_bytearray6 = bytearray(6)
240247
super().__init__(rng)
241248

242249
def write_register(self, register, value):
@@ -258,8 +265,28 @@ def read_register(self, register):
258265
"""
259266
register = (register | 0x80) & 0xFF # Read single, bit 7 high.
260267
with self._spi as spi:
261-
spi.write(bytearray([register]))
262-
result = bytearray(1)
263-
spi.readinto(result)
264-
#print("$%02X => %s" % (register, [hex(i) for i in result]))
265-
return result
268+
self._spi_bytearray1[0] = register
269+
spi.write(self._spi_bytearray1)
270+
spi.readinto(self._spi_bytearray1)
271+
print("$%02X => %s" % (register, [hex(i) for i in self._spi_bytearray1]))
272+
return self._spi_bytearray1[0]
273+
274+
def read_bytes(self, register, buffer):
275+
"""
276+
Low level register streem reading over SPI, returns a list of values
277+
278+
:param register: the register to read bytes
279+
:param bytearray buffer: buffer to fill with data from stream
280+
"""
281+
register = (register | 0x80) & 0xFF # Read single, bit 7 high.
282+
with self._spi as spi:
283+
self._spi_bytearray1[0] = register
284+
spi.write(self._spi_bytearray1)
285+
spi.readinto(buffer)
286+
287+
@property
288+
def acceleration_raw(self):
289+
"""Gives the raw acceleration readings, in units of the scaled mdps."""
290+
buffer = self._spi_bytearray6
291+
self.read_bytes((_L3GD20_REGISTER_OUT_X_L | 0x40), buffer)
292+
return unpack('<hhh', buffer)

examples/l3gd20_spi_simpletest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import time
2+
from board import SCK, MISO, MOSI, D5
3+
import busio
4+
import adafruit_l3gd20
5+
import digitalio
6+
7+
CS = digitalio.DigitalInOut(D5) # select pin is 5
8+
SPIB = busio.SPI(SCK, MOSI, MISO)
9+
SENSOR = adafruit_l3gd20.L3GD20_SPI(SPIB, CS)
10+
11+
while True:
12+
13+
print('Acceleration (m/s^2): {}'.format(SENSOR.acceleration))
14+
15+
print()
16+
17+
time.sleep(1)

0 commit comments

Comments
 (0)