|
32 | 32 | * Author(s): Tony DiCola
|
33 | 33 | """
|
34 | 34 | import time
|
| 35 | +import ustruct |
35 | 36 |
|
36 | 37 | import adafruit_bus_device.i2c_device as i2c_device
|
37 | 38 |
|
@@ -98,41 +99,33 @@ def __init__(self, i2c, address=_FXAS21002C_ADDRESS,
|
98 | 99 |
|
99 | 100 | def _read_u8(self, address):
|
100 | 101 | # Read an 8-bit unsigned value from the specified 8-bit address.
|
101 |
| - with self._device: |
| 102 | + with self._device as i2c: |
102 | 103 | self._BUFFER[0] = address & 0xFF
|
103 |
| - self._device.write(self._BUFFER, end=1, stop=False) |
104 |
| - self._device.readinto(self._BUFFER, end=1) |
| 104 | + i2c.write(self._BUFFER, end=1, stop=False) |
| 105 | + i2c.readinto(self._BUFFER, end=1) |
105 | 106 | return self._BUFFER[0]
|
106 | 107 |
|
107 | 108 | def _write_u8(self, address, val):
|
108 | 109 | # Write an 8-bit unsigned value to the specified 8-bit address.
|
109 |
| - with self._device: |
| 110 | + with self._device as i2c: |
110 | 111 | self._BUFFER[0] = address & 0xFF
|
111 | 112 | self._BUFFER[1] = val & 0xFF
|
112 |
| - self._device.write(self._BUFFER, end=2) |
| 113 | + i2c.write(self._BUFFER, end=2) |
113 | 114 |
|
114 | 115 | def read_raw(self):
|
115 | 116 | """Read the raw gyroscope readings. Returns a 3-tuple of X, Y, Z axis
|
116 |
| - 16-bit unsigned values. If you want the gyroscope values in friendly |
| 117 | + 16-bit signed values. If you want the gyroscope values in friendly |
117 | 118 | units consider using the gyroscope property!
|
118 | 119 | """
|
119 |
| - # Read 7 bytes from the sensor. |
| 120 | + # Read gyro data from the sensor. |
120 | 121 | with self._device:
|
121 |
| - self._BUFFER[0] = _GYRO_REGISTER_STATUS | 0x80 |
| 122 | + self._BUFFER[0] = _GYRO_REGISTER_OUT_X_MSB |
122 | 123 | self._device.write(self._BUFFER, end=1, stop=False)
|
123 | 124 | self._device.readinto(self._BUFFER)
|
124 |
| - # Parse out the gyroscope data. |
125 |
| - status = self._BUFFER[0] |
126 |
| - xhi = self._BUFFER[1] |
127 |
| - xlo = self._BUFFER[2] |
128 |
| - yhi = self._BUFFER[3] |
129 |
| - ylo = self._BUFFER[4] |
130 |
| - zhi = self._BUFFER[5] |
131 |
| - zlo = self._BUFFER[6] |
132 |
| - # Shift values to create properly formed integers |
133 |
| - raw_x = ((xhi << 8) | xlo) & 0xFFFF |
134 |
| - raw_y = ((yhi << 8) | ylo) & 0xFFFF |
135 |
| - raw_z = ((zhi << 8) | zlo) & 0xFFFF |
| 125 | + # Parse out the gyroscope data as 16-bit signed data. |
| 126 | + raw_x = ustruct.unpack_from('>h', self._BUFFER[0:2])[0] |
| 127 | + raw_y = ustruct.unpack_from('>h', self._BUFFER[2:4])[0] |
| 128 | + raw_z = ustruct.unpack_from('>h', self._BUFFER[4:6])[0] |
136 | 129 | return (raw_x, raw_y, raw_z)
|
137 | 130 |
|
138 | 131 | @property
|
|
0 commit comments