50
50
import struct
51
51
52
52
from micropython import const
53
- from adafruit_register .i2c_struct import ROUnaryStruct
53
+ from adafruit_register .i2c_struct import Struct , ROUnaryStruct
54
54
from adafruit_register .i2c_bit import RWBit
55
55
from adafruit_register .i2c_bits import RWBits , ROBits
56
56
import adafruit_bus_device .i2c_device as i2cdevice
@@ -230,7 +230,8 @@ def __init__(self, i2c_bus):
230
230
_disable_y = RWBit (_MSA301_REG_ODR , 6 )
231
231
_disable_z = RWBit (_MSA301_REG_ODR , 5 )
232
232
233
- _xyz_raw = ROBits (48 , _MSA301_REG_OUT_X_L , 0 , 6 )
233
+ #_xyz_raw = ROBits(48, _MSA301_REG_OUT_X_L, 0, 6)
234
+ _xyz_raw = Struct (_MSA301_REG_OUT_X_L , "<hhh" )
234
235
235
236
# tap INT enable and status
236
237
_single_tap_int_en = RWBit (_MSA301_REG_INTSET0 , 5 )
@@ -255,16 +256,6 @@ def __init__(self, i2c_bus):
255
256
def acceleration (self ):
256
257
"""The x, y, z acceleration values returned in a 3-tuple and are in m / s ^ 2."""
257
258
# 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
259
269
260
current_range = self .range
270
261
scale = 1.0
@@ -278,11 +269,9 @@ def acceleration(self):
278
269
scale = 4096.0
279
270
280
271
# 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
272
+ x , y , z = [((i >> 2 ) / scale ) * _STANDARD_GRAVITY for i in self ._xyz_raw ]
284
273
285
- return (x_acc , y_acc , z_acc )
274
+ return (x , y , z )
286
275
287
276
def enable_tap_detection (
288
277
self ,
0 commit comments