Skip to content

Commit 46b524f

Browse files
committed
simplify xyz to just use one struct read and do the scaling in a listcomp
1 parent 778aa21 commit 46b524f

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

adafruit_msa301.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import struct
5151

5252
from micropython import const
53-
from adafruit_register.i2c_struct import ROUnaryStruct
53+
from adafruit_register.i2c_struct import Struct, ROUnaryStruct
5454
from adafruit_register.i2c_bit import RWBit
5555
from adafruit_register.i2c_bits import RWBits, ROBits
5656
import adafruit_bus_device.i2c_device as i2cdevice
@@ -230,7 +230,8 @@ def __init__(self, i2c_bus):
230230
_disable_y = RWBit(_MSA301_REG_ODR, 6)
231231
_disable_z = RWBit(_MSA301_REG_ODR, 5)
232232

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")
234235

235236
# tap INT enable and status
236237
_single_tap_int_en = RWBit(_MSA301_REG_INTSET0, 5)
@@ -255,16 +256,6 @@ def __init__(self, i2c_bus):
255256
def acceleration(self):
256257
"""The x, y, z acceleration values returned in a 3-tuple and are in m / s ^ 2."""
257258
# 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)
268259

269260
current_range = self.range
270261
scale = 1.0
@@ -278,11 +269,9 @@ def acceleration(self):
278269
scale = 4096.0
279270

280271
# 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]
284273

285-
return (x_acc, y_acc, z_acc)
274+
return (x, y, z)
286275

287276
def enable_tap_detection(
288277
self,

0 commit comments

Comments
 (0)