Skip to content

Commit 0d30d60

Browse files
authored
Merge pull request #13 from ladyada/master
simplify xyz to just use one struct read and do the scaling in a listcomp
2 parents 778aa21 + 749c8bc commit 0d30d60

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

adafruit_msa301.py

+7-20
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@
4747
__version__ = "0.0.0-auto.0"
4848
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MSA301.git"
4949

50-
import struct
51-
5250
from micropython import const
53-
from adafruit_register.i2c_struct import ROUnaryStruct
51+
from adafruit_register.i2c_struct import Struct, ROUnaryStruct
5452
from adafruit_register.i2c_bit import RWBit
55-
from adafruit_register.i2c_bits import RWBits, ROBits
53+
from adafruit_register.i2c_bits import RWBits
5654
import adafruit_bus_device.i2c_device as i2cdevice
5755

5856
_MSA301_I2CADDR_DEFAULT = const(0x26)
@@ -207,7 +205,7 @@ class TapDuration: # pylint: disable=too-few-public-methods,too-many-instance-a
207205
class MSA301: # pylint: disable=too-many-instance-attributes
208206
"""Driver for the MSA301 Accelerometer.
209207
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.
211209
"""
212210

213211
_part_id = ROUnaryStruct(_MSA301_REG_PARTID, "<B")
@@ -230,7 +228,8 @@ def __init__(self, i2c_bus):
230228
_disable_y = RWBit(_MSA301_REG_ODR, 6)
231229
_disable_z = RWBit(_MSA301_REG_ODR, 5)
232230

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

235234
# tap INT enable and status
236235
_single_tap_int_en = RWBit(_MSA301_REG_INTSET0, 5)
@@ -255,16 +254,6 @@ def __init__(self, i2c_bus):
255254
def acceleration(self):
256255
"""The x, y, z acceleration values returned in a 3-tuple and are in m / s ^ 2."""
257256
# 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)
268257

269258
current_range = self.range
270259
scale = 1.0
@@ -278,11 +267,9 @@ def acceleration(self):
278267
scale = 4096.0
279268

280269
# 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]
284271

285-
return (x_acc, y_acc, z_acc)
272+
return (x, y, z)
286273

287274
def enable_tap_detection(
288275
self,

0 commit comments

Comments
 (0)