Skip to content

Commit a83dbcd

Browse files
mrmcwethytannewt
authored andcommitted
Optimize the way data in unpacked (#9)
Optimize the way data in unpacked
1 parent fa00b61 commit a83dbcd

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

adafruit_lis3dh/lis3dh.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ def acceleration(self):
9898
"""Read the x, y, z acceleration values. These values are returned in
9999
a 3-tuple and are in m / s ^ 2.
100100
"""
101-
data = self._read_register(REG_OUT_X_L | 0x80, 6)
102-
x = struct.unpack('<h', data[0:2])[0]
103-
y = struct.unpack('<h', data[2:4])[0]
104-
z = struct.unpack('<h', data[4:6])[0]
105101
divider = 1
106102
accel_range = self.range
107103
if accel_range == RANGE_16_G:
@@ -112,6 +108,9 @@ def acceleration(self):
112108
divider = 8190
113109
elif accel_range == RANGE_2_G:
114110
divider = 16380
111+
112+
x, y, z = struct.unpack('<hhh', self._read_register(REG_OUT_X_L | 0x80, 6))
113+
115114
return (x / divider * 9.806, y / divider * 9.806, z / divider * 9.806)
116115

117116
def read_adc_raw(self, adc):
@@ -120,8 +119,8 @@ def read_adc_raw(self, adc):
120119
"""
121120
if adc < 1 or adc > 3:
122121
raise ValueError('ADC must be a value 1 to 3!')
123-
data = self._read_register((REG_OUTADC1_L+((adc-1)*2)) | 0x80, 2)
124-
return struct.unpack('<h', data[0:2])[0]
122+
123+
return struct.unpack('<h', self._read_register((REG_OUTADC1_L+((adc-1)*2)) | 0x80, 2))[0]
125124

126125
def read_adc_mV(self, adc):
127126
"""Read the specified analog to digital converter value in millivolts.

0 commit comments

Comments
 (0)