Skip to content

Commit 8cff431

Browse files
ladyadadhalbert
authored andcommitted
use struct when available, list comp instead of map() (#2)
1 parent ed53265 commit 8cff431

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

adafruit_fxas21002c.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
* Author(s): Tony DiCola
3333
"""
3434
import time
35-
import ustruct
35+
try:
36+
import ustruct as struct
37+
except ImportError:
38+
import struct
3639

3740
import adafruit_bus_device.i2c_device as i2c_device
3841
from micropython import const
@@ -127,28 +130,28 @@ def read_raw(self):
127130
self._device.write(self._BUFFER, end=1, stop=False)
128131
self._device.readinto(self._BUFFER)
129132
# Parse out the gyroscope data as 16-bit signed data.
130-
raw_x = ustruct.unpack_from('>h', self._BUFFER[0:2])[0]
131-
raw_y = ustruct.unpack_from('>h', self._BUFFER[2:4])[0]
132-
raw_z = ustruct.unpack_from('>h', self._BUFFER[4:6])[0]
133+
raw_x = struct.unpack_from('>h', self._BUFFER[0:2])[0]
134+
raw_y = struct.unpack_from('>h', self._BUFFER[2:4])[0]
135+
raw_z = struct.unpack_from('>h', self._BUFFER[4:6])[0]
133136
return (raw_x, raw_y, raw_z)
134137

135138
# pylint is confused and incorrectly marking this function as bad return
136139
# types. Perhaps it doesn't understand map returns an iterable value.
137140
# Disable the warning.
138-
# pylint: disable=inconsistent-return-statements
139141
@property
140142
def gyroscope(self):
141143
"""Read the gyroscope value and return its X, Y, Z axis values as a
142144
3-tuple in radians/second.
143145
"""
144146
raw = self.read_raw()
145147
# Compensate values depending on the resolution
148+
factor = 0
146149
if self._gyro_range == GYRO_RANGE_250DPS:
147-
return map(lambda x: x * _GYRO_SENSITIVITY_250DPS, raw)
150+
factor = _GYRO_SENSITIVITY_250DPS
148151
elif self._gyro_range == GYRO_RANGE_500DPS:
149-
return map(lambda x: x * _GYRO_SENSITIVITY_500DPS, raw)
152+
factor = _GYRO_SENSITIVITY_500DPS
150153
elif self._gyro_range == GYRO_RANGE_1000DPS:
151-
return map(lambda x: x * _GYRO_SENSITIVITY_1000DPS, raw)
154+
factor = _GYRO_SENSITIVITY_1000DPS
152155
elif self._gyro_range == GYRO_RANGE_2000DPS:
153-
return map(lambda x: x * _GYRO_SENSITIVITY_2000DPS, raw)
154-
# pylint: enable=inconsistent-return-statements
156+
factor = _GYRO_SENSITIVITY_2000DPS
157+
return [x * factor for x in raw]

examples/simpletest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
# Read gyroscope.
2323
gyro_x, gyro_y, gyro_z = sensor.gyroscope
2424
# Print values.
25-
print('Gyroscope (radians/s): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(gyro_x, gyro_y, gyro_z))
25+
print('Gyroscope (radians/s): ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(gyro_x, gyro_y, gyro_z))
2626
# Delay for a second.
2727
time.sleep(1.0)

0 commit comments

Comments
 (0)