From 1702aad25409ebadb99bdc9f8fed1ae8914aa0b8 Mon Sep 17 00:00:00 2001 From: ladyada Date: Tue, 9 Jan 2018 16:06:46 -0500 Subject: [PATCH] use struct when available, list comp instead of map() --- adafruit_fxas21002c.py | 23 +++++++++++++---------- examples/simpletest.py | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/adafruit_fxas21002c.py b/adafruit_fxas21002c.py index 783cf75..ebae15a 100644 --- a/adafruit_fxas21002c.py +++ b/adafruit_fxas21002c.py @@ -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 @@ -127,15 +130,14 @@ 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 @@ -143,12 +145,13 @@ def gyroscope(self): """ 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] diff --git a/examples/simpletest.py b/examples/simpletest.py index 63ef089..a80d13d 100644 --- a/examples/simpletest.py +++ b/examples/simpletest.py @@ -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)