47
47
__version__ = "0.0.0-auto.0"
48
48
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MSA301.git"
49
49
50
- import struct
51
-
52
50
from micropython import const
53
- from adafruit_register .i2c_struct import ROUnaryStruct
51
+ from adafruit_register .i2c_struct import Struct , ROUnaryStruct
54
52
from adafruit_register .i2c_bit import RWBit
55
- from adafruit_register .i2c_bits import RWBits , ROBits
53
+ from adafruit_register .i2c_bits import RWBits
56
54
import adafruit_bus_device .i2c_device as i2cdevice
57
55
58
56
_MSA301_I2CADDR_DEFAULT = const (0x26 )
@@ -207,7 +205,7 @@ class TapDuration: # pylint: disable=too-few-public-methods,too-many-instance-a
207
205
class MSA301 : # pylint: disable=too-many-instance-attributes
208
206
"""Driver for the MSA301 Accelerometer.
209
207
210
- :param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.
208
+ :param ~busio.I2C i2c_bus: The I2C bus the MSA is connected to.
211
209
"""
212
210
213
211
_part_id = ROUnaryStruct (_MSA301_REG_PARTID , "<B" )
@@ -230,7 +228,8 @@ def __init__(self, i2c_bus):
230
228
_disable_y = RWBit (_MSA301_REG_ODR , 6 )
231
229
_disable_z = RWBit (_MSA301_REG_ODR , 5 )
232
230
233
- _xyz_raw = ROBits (48 , _MSA301_REG_OUT_X_L , 0 , 6 )
231
+ # _xyz_raw = ROBits(48, _MSA301_REG_OUT_X_L, 0, 6)
232
+ _xyz_raw = Struct (_MSA301_REG_OUT_X_L , "<hhh" )
234
233
235
234
# tap INT enable and status
236
235
_single_tap_int_en = RWBit (_MSA301_REG_INTSET0 , 5 )
@@ -255,16 +254,6 @@ def __init__(self, i2c_bus):
255
254
def acceleration (self ):
256
255
"""The x, y, z acceleration values returned in a 3-tuple and are in m / s ^ 2."""
257
256
# read the 6 bytes of acceleration data
258
- # zh, zl, yh, yl, xh, xl
259
- raw_data = self ._xyz_raw
260
- acc_bytes = bytearray ()
261
- # shift out bytes, reversing the order
262
- for shift in range (6 ):
263
- bottom_byte = raw_data >> (8 * shift ) & 0xFF
264
- acc_bytes .append (bottom_byte )
265
-
266
- # unpack three LE, signed shorts
267
- x , y , z = struct .unpack_from ("<hhh" , acc_bytes )
268
257
269
258
current_range = self .range
270
259
scale = 1.0
@@ -278,11 +267,9 @@ def acceleration(self):
278
267
scale = 4096.0
279
268
280
269
# shift down to the actual 14 bits and scale based on the range
281
- x_acc = ((x >> 2 ) / scale ) * _STANDARD_GRAVITY
282
- y_acc = ((y >> 2 ) / scale ) * _STANDARD_GRAVITY
283
- z_acc = ((z >> 2 ) / scale ) * _STANDARD_GRAVITY
270
+ x , y , z = [((i >> 2 ) / scale ) * _STANDARD_GRAVITY for i in self ._xyz_raw ]
284
271
285
- return (x_acc , y_acc , z_acc )
272
+ return (x , y , z )
286
273
287
274
def enable_tap_detection (
288
275
self ,
0 commit comments