34
34
import adafruit_bus_device .i2c_device as i2c_dev
35
35
from micropython import const
36
36
37
+ try :
38
+ from typing import Tuple , List
39
+ from busio import I2C
40
+ except ImportError :
41
+ pass
42
+
37
43
__version__ = "0.0.0-auto.0"
38
44
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FXOS8700.git"
39
45
76
82
ACCEL_RANGE_8G = 0x02
77
83
78
84
79
- def _twos_comp (val , bits ) :
85
+ def _twos_comp (val : int , bits : int ) -> int :
80
86
# Convert an unsigned integer in 2's compliment form of the specified bit
81
87
# length to its signed integer value and return it.
82
88
if val & (1 << (bits - 1 )) != 0 :
@@ -123,7 +129,12 @@ class FXOS8700:
123
129
# thread safe!
124
130
_BUFFER = bytearray (13 )
125
131
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 :
127
138
if accel_range not in (ACCEL_RANGE_2G , ACCEL_RANGE_4G , ACCEL_RANGE_8G ):
128
139
raise Exception ("accel_range selected is not a valid option" )
129
140
self ._accel_range = accel_range
@@ -149,21 +160,21 @@ def __init__(self, i2c, address=_FXOS8700_ADDRESS, accel_range=ACCEL_RANGE_2G):
149
160
# Jump to reg 0x33 after reading 0x06
150
161
self ._write_u8 (_FXOS8700_REGISTER_MCTRL_REG2 , 0x20 )
151
162
152
- def _read_u8 (self , address ) :
163
+ def _read_u8 (self , address : int ) -> int :
153
164
# Read an 8-bit unsigned value from the specified 8-bit address.
154
165
with self ._device as i2c :
155
166
self ._BUFFER [0 ] = address & 0xFF
156
167
i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 1 )
157
168
return self ._BUFFER [0 ]
158
169
159
- def _write_u8 (self , address , val ) :
170
+ def _write_u8 (self , address : int , val : int ) -> None :
160
171
# Write an 8-bit unsigned value to the specified 8-bit address.
161
172
with self ._device as i2c :
162
173
self ._BUFFER [0 ] = address & 0xFF
163
174
self ._BUFFER [1 ] = val & 0xFF
164
175
i2c .write (self ._BUFFER , end = 2 )
165
176
166
- def read_raw_accel_mag (self ):
177
+ def read_raw_accel_mag (self ) -> Tuple [ Tuple [ int , int , int ], Tuple [ int , int , int ]] :
167
178
"""Read the raw accelerometer and magnetometer readings. Returns a
168
179
2-tuple of 3-tuples:
169
180
@@ -199,9 +210,9 @@ def read_raw_accel_mag(self):
199
210
)
200
211
201
212
@property
202
- def accelerometer (self ):
213
+ def accelerometer (self ) -> List [ float ] :
203
214
"""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`.
205
216
"""
206
217
accel_raw , _ = self .read_raw_accel_mag ()
207
218
# Convert accel values to m/s^2
@@ -215,9 +226,10 @@ def accelerometer(self):
215
226
return [x * factor * _SENSORS_GRAVITY_STANDARD for x in accel_raw ]
216
227
217
228
@property
218
- def magnetometer (self ):
229
+ def magnetometer (self ) -> List [ float ] :
219
230
"""
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.
221
233
"""
222
234
_ , mag_raw = self .read_raw_accel_mag ()
223
235
# Convert mag values to uTesla
0 commit comments