Skip to content

use struct when available, change map() to list comp #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions adafruit_fxos8700.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@

* Author(s): Tony DiCola
"""
import ustruct
try:
import ustruct as struct
except ImportError:
import struct

import adafruit_bus_device.i2c_device as i2c_device
from micropython import const
Expand Down Expand Up @@ -146,9 +149,9 @@ def read_raw_accel_mag(self):
self._BUFFER[0] = _FXOS8700_REGISTER_OUT_X_MSB
i2c.write(self._BUFFER, end=1, stop=False)
i2c.readinto(self._BUFFER, end=6)
accel_raw_x = ustruct.unpack_from('>H', self._BUFFER[0:2])[0]
accel_raw_y = ustruct.unpack_from('>H', self._BUFFER[2:4])[0]
accel_raw_z = ustruct.unpack_from('>H', self._BUFFER[4:6])[0]
accel_raw_x = struct.unpack_from('>H', self._BUFFER[0:2])[0]
accel_raw_y = struct.unpack_from('>H', self._BUFFER[2:4])[0]
accel_raw_z = struct.unpack_from('>H', self._BUFFER[4:6])[0]
# Convert accelerometer data to signed 14-bit value from 16-bit
# left aligned 2's compliment value.
accel_raw_x = _twos_comp(accel_raw_x >> 2, 14)
Expand All @@ -160,38 +163,32 @@ def read_raw_accel_mag(self):
self._BUFFER[0] = _FXOS8700_REGISTER_MOUT_X_MSB
i2c.write(self._BUFFER, end=1, stop=False)
i2c.readinto(self._BUFFER, end=6)
mag_raw_x = ustruct.unpack_from('>h', self._BUFFER[0:2])[0]
mag_raw_y = ustruct.unpack_from('>h', self._BUFFER[2:4])[0]
mag_raw_z = ustruct.unpack_from('>h', self._BUFFER[4:6])[0]
mag_raw_x = struct.unpack_from('>h', self._BUFFER[0:2])[0]
mag_raw_y = struct.unpack_from('>h', self._BUFFER[2:4])[0]
mag_raw_z = struct.unpack_from('>h', self._BUFFER[4:6])[0]
return ((accel_raw_x, accel_raw_y, accel_raw_z),
(mag_raw_x, mag_raw_y, mag_raw_z))

# pylint is confused and incorrectly marking this function as bad return
# types. Perhaps it doesn't understand map returns an iterable value.
# Disable the warning.
# pylint: disable=inconsistent-return-statements
@property
def accelerometer(self):
"""Read the acceleration from the accelerometer and return its X, Y, Z axis values as a
3-tuple in m/s^2.
"""
accel_raw, _ = self.read_raw_accel_mag()
# Convert accel values to m/s^2
factor = 0
if self._accel_range == ACCEL_RANGE_2G:
return map(lambda x: x * _ACCEL_MG_LSB_2G * _SENSORS_GRAVITY_STANDARD,
accel_raw)
factor = _ACCEL_MG_LSB_2G
elif self._accel_range == ACCEL_RANGE_4G:
return map(lambda x: x * _ACCEL_MG_LSB_4G * _SENSORS_GRAVITY_STANDARD,
accel_raw)
factor = _ACCEL_MG_LSB_4G
elif self._accel_range == ACCEL_RANGE_8G:
return map(lambda x: x * _ACCEL_MG_LSB_8G * _SENSORS_GRAVITY_STANDARD,
accel_raw)
# pylint: enable=inconsistent-return-statements
factor = _ACCEL_MG_LSB_8G
return [x * factor * _SENSORS_GRAVITY_STANDARD for x in accel_raw]

@property
def magnetometer(self):
"""Read the magnetometer values and return its X, Y, Z axis values as a 3-tuple in uTeslas.
"""
_, mag_raw = self.read_raw_accel_mag()
# Convert mag values to uTesla
return map(lambda x: x * _MAG_UT_LSB, mag_raw)
return [x * _MAG_UT_LSB for x in mag_raw]
4 changes: 2 additions & 2 deletions examples/simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
accel_x, accel_y, accel_z = sensor.accelerometer
mag_x, mag_y, mag_z = sensor.magnetometer
# Print values.
print('Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(accel_x, accel_y, accel_z))
print('Magnetometer (uTesla): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(mag_x, mag_y, mag_z))
print('Acceleration (m/s^2): ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(accel_x, accel_y, accel_z))
print('Magnetometer (uTesla): ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(mag_x, mag_y, mag_z))
# Delay for a second.
time.sleep(1.0)