35
35
36
36
**Hardware:**
37
37
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>`_
39
39
40
40
**Software and Dependencies:**
41
41
54
54
55
55
from micropython import const
56
56
from adafruit_register .i2c_struct import Struct
57
+ try :
58
+ from struct import unpack
59
+ except ImportError :
60
+ from ustruct import unpack
57
61
58
62
__version__ = "0.0.0-auto.0"
59
63
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_L3GD20.git"
@@ -84,9 +88,6 @@ class L3GD20: # pylint: disable=no-member
84
88
L3DS20_RANGE_2000DPS
85
89
"""
86
90
87
- acceleration_raw = Struct ((_L3GD20_REGISTER_OUT_X_L | 0x80 ), '<hhh' )
88
- """Gives the raw acceleration readings, in units of the scaled mdps."""
89
-
90
91
def __init__ (self , rng ):
91
92
chip_id = self .read_register (_ID_REGISTER )
92
93
if chip_id != _L3GD20_CHIP_ID and chip_id != _L3GD20H_CHIP_ID :
@@ -194,6 +195,10 @@ class L3GD20_I2C(L3GD20):
194
195
L3DS20_RANGE_2000DPS
195
196
:param address: the optional device address, 0x68 is the default address
196
197
"""
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
+
197
202
def __init__ (self , i2c , rng = L3DS20_RANGE_250DPS , address = 0x6B ):
198
203
import adafruit_bus_device .i2c_device as i2c_device
199
204
self .i2c_device = i2c_device .I2CDevice (i2c , address )
@@ -237,6 +242,8 @@ class L3GD20_SPI(L3GD20):
237
242
def __init__ (self , spi_busio , cs , rng = L3DS20_RANGE_250DPS , baudrate = 100000 ):
238
243
import adafruit_bus_device .spi_device as spi_device
239
244
self ._spi = spi_device .SPIDevice (spi_busio , cs , baudrate = baudrate )
245
+ self ._spi_bytearray1 = bytearray (1 )
246
+ self ._spi_bytearray6 = bytearray (6 )
240
247
super ().__init__ (rng )
241
248
242
249
def write_register (self , register , value ):
@@ -258,8 +265,28 @@ def read_register(self, register):
258
265
"""
259
266
register = (register | 0x80 ) & 0xFF # Read single, bit 7 high.
260
267
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 )
0 commit comments