Skip to content

use struct when available, list comp instead of map() #2

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
23 changes: 13 additions & 10 deletions adafruit_fxas21002c.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
* Author(s): Tony DiCola
"""
import time
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 @@ -127,28 +130,28 @@ def read_raw(self):
self._device.write(self._BUFFER, end=1, stop=False)
self._device.readinto(self._BUFFER)
# Parse out the gyroscope data as 16-bit signed data.
raw_x = ustruct.unpack_from('>h', self._BUFFER[0:2])[0]
raw_y = ustruct.unpack_from('>h', self._BUFFER[2:4])[0]
raw_z = ustruct.unpack_from('>h', self._BUFFER[4:6])[0]
raw_x = struct.unpack_from('>h', self._BUFFER[0:2])[0]
raw_y = struct.unpack_from('>h', self._BUFFER[2:4])[0]
raw_z = struct.unpack_from('>h', self._BUFFER[4:6])[0]
return (raw_x, raw_y, 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 gyroscope(self):
"""Read the gyroscope value and return its X, Y, Z axis values as a
3-tuple in radians/second.
"""
raw = self.read_raw()
# Compensate values depending on the resolution
factor = 0
if self._gyro_range == GYRO_RANGE_250DPS:
return map(lambda x: x * _GYRO_SENSITIVITY_250DPS, raw)
factor = _GYRO_SENSITIVITY_250DPS
elif self._gyro_range == GYRO_RANGE_500DPS:
return map(lambda x: x * _GYRO_SENSITIVITY_500DPS, raw)
factor = _GYRO_SENSITIVITY_500DPS
elif self._gyro_range == GYRO_RANGE_1000DPS:
return map(lambda x: x * _GYRO_SENSITIVITY_1000DPS, raw)
factor = _GYRO_SENSITIVITY_1000DPS
elif self._gyro_range == GYRO_RANGE_2000DPS:
return map(lambda x: x * _GYRO_SENSITIVITY_2000DPS, raw)
# pylint: enable=inconsistent-return-statements
factor = _GYRO_SENSITIVITY_2000DPS
return [x * factor for x in raw]
2 changes: 1 addition & 1 deletion examples/simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
# Read gyroscope.
gyro_x, gyro_y, gyro_z = sensor.gyroscope
# Print values.
print('Gyroscope (radians/s): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(gyro_x, gyro_y, gyro_z))
print('Gyroscope (radians/s): ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(gyro_x, gyro_y, gyro_z))
# Delay for a second.
time.sleep(1.0)