Skip to content

Commit 1c4cd53

Browse files
committed
Add type annotations
1 parent b8da8b8 commit 1c4cd53

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

adafruit_fxos8700.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
import adafruit_bus_device.i2c_device as i2c_dev
3535
from micropython import const
3636

37+
try:
38+
from typing import Tuple, List
39+
from busio import I2C
40+
except ImportError:
41+
pass
42+
3743
__version__ = "0.0.0-auto.0"
3844
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FXOS8700.git"
3945

@@ -76,7 +82,7 @@
7682
ACCEL_RANGE_8G = 0x02
7783

7884

79-
def _twos_comp(val, bits):
85+
def _twos_comp(val: int, bits: int) -> int:
8086
# Convert an unsigned integer in 2's compliment form of the specified bit
8187
# length to its signed integer value and return it.
8288
if val & (1 << (bits - 1)) != 0:
@@ -123,7 +129,12 @@ class FXOS8700:
123129
# thread safe!
124130
_BUFFER = bytearray(13)
125131

126-
def __init__(self, i2c, address=_FXOS8700_ADDRESS, accel_range=ACCEL_RANGE_2G):
132+
def __init__(
133+
self,
134+
i2c: I2C,
135+
address: int = _FXOS8700_ADDRESS,
136+
accel_range: int = ACCEL_RANGE_2G,
137+
) -> None:
127138
if accel_range not in (ACCEL_RANGE_2G, ACCEL_RANGE_4G, ACCEL_RANGE_8G):
128139
raise Exception("accel_range selected is not a valid option")
129140
self._accel_range = accel_range
@@ -149,21 +160,21 @@ def __init__(self, i2c, address=_FXOS8700_ADDRESS, accel_range=ACCEL_RANGE_2G):
149160
# Jump to reg 0x33 after reading 0x06
150161
self._write_u8(_FXOS8700_REGISTER_MCTRL_REG2, 0x20)
151162

152-
def _read_u8(self, address):
163+
def _read_u8(self, address: int) -> int:
153164
# Read an 8-bit unsigned value from the specified 8-bit address.
154165
with self._device as i2c:
155166
self._BUFFER[0] = address & 0xFF
156167
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=1)
157168
return self._BUFFER[0]
158169

159-
def _write_u8(self, address, val):
170+
def _write_u8(self, address: int, val: int) -> None:
160171
# Write an 8-bit unsigned value to the specified 8-bit address.
161172
with self._device as i2c:
162173
self._BUFFER[0] = address & 0xFF
163174
self._BUFFER[1] = val & 0xFF
164175
i2c.write(self._BUFFER, end=2)
165176

166-
def read_raw_accel_mag(self):
177+
def read_raw_accel_mag(self) -> Tuple[Tuple[int, int, int], Tuple[int, int, int]]:
167178
"""Read the raw accelerometer and magnetometer readings. Returns a
168179
2-tuple of 3-tuples:
169180
@@ -199,9 +210,9 @@ def read_raw_accel_mag(self):
199210
)
200211

201212
@property
202-
def accelerometer(self):
213+
def accelerometer(self) -> List[float]:
203214
"""Read the acceleration from the accelerometer and return its X, Y, Z axis values as a
204-
3-tuple in :math:`m/s^2`.
215+
list of length 3 in :math:`m/s^2`.
205216
"""
206217
accel_raw, _ = self.read_raw_accel_mag()
207218
# Convert accel values to m/s^2
@@ -215,9 +226,10 @@ def accelerometer(self):
215226
return [x * factor * _SENSORS_GRAVITY_STANDARD for x in accel_raw]
216227

217228
@property
218-
def magnetometer(self):
229+
def magnetometer(self) -> List[float]:
219230
"""
220-
Read the magnetometer values and return its X, Y, Z axis values as a 3-tuple in μTeslas.
231+
Read the magnetometer values and return its X, Y, Z axis values as a list of length 3
232+
in μTeslas.
221233
"""
222234
_, mag_raw = self.read_raw_accel_mag()
223235
# Convert mag values to uTesla

0 commit comments

Comments
 (0)