42
42
from adafruit_bus_device import spi_device
43
43
from micropython import const
44
44
45
+ try :
46
+ from typing import Tuple
47
+ from circuitpython_typing import WriteableBuffer
48
+ from digitalio import DigitalInOut
49
+ from busio import I2C , SPI
50
+ except ImportError :
51
+ pass
52
+
45
53
# Internal constants and register values:
46
54
_LSM9DS1_ADDRESS_ACCELGYRO = const (0x6B )
47
55
_LSM9DS1_ADDRESS_MAG = const (0x1E )
119
127
GYROSCALE_2000DPS = 0b11 << 3 # +/- 2000 degrees/s rotation
120
128
121
129
122
- def _twos_comp (val , bits ) :
130
+ def _twos_comp (val : int , bits : int ) -> int :
123
131
# Convert an unsigned integer in 2's compliment form of the specified bit
124
132
# length to its signed integer value and return it.
125
133
if val & (1 << (bits - 1 )) != 0 :
@@ -135,7 +143,7 @@ class LSM9DS1:
135
143
# thread safe!
136
144
_BUFFER = bytearray (6 )
137
145
138
- def __init__ (self ):
146
+ def __init__ (self ) -> None :
139
147
# soft reset & reboot accel/gyro
140
148
self ._write_u8 (_XGTYPE , _LSM9DS1_REGISTER_CTRL_REG8 , 0x05 )
141
149
# soft reset & reboot magnetometer
@@ -163,7 +171,7 @@ def __init__(self):
163
171
self .gyro_scale = GYROSCALE_245DPS
164
172
165
173
@property
166
- def accel_range (self ):
174
+ def accel_range (self ) -> int :
167
175
"""The accelerometer range. Must be a value of:
168
176
169
177
- ACCELRANGE_2G
@@ -176,7 +184,7 @@ def accel_range(self):
176
184
return (reg & 0b00011000 ) & 0xFF
177
185
178
186
@accel_range .setter
179
- def accel_range (self , val ) :
187
+ def accel_range (self , val : int ) -> None :
180
188
assert val in (ACCELRANGE_2G , ACCELRANGE_4G , ACCELRANGE_8G , ACCELRANGE_16G )
181
189
reg = self ._read_u8 (_XGTYPE , _LSM9DS1_REGISTER_CTRL_REG6_XL )
182
190
reg = (reg & ~ (0b00011000 )) & 0xFF
@@ -192,7 +200,7 @@ def accel_range(self, val):
192
200
self ._accel_mg_lsb = _LSM9DS1_ACCEL_MG_LSB_16G
193
201
194
202
@property
195
- def mag_gain (self ):
203
+ def mag_gain (self ) -> int :
196
204
"""The magnetometer gain. Must be a value of:
197
205
198
206
- MAGGAIN_4GAUSS
@@ -205,7 +213,7 @@ def mag_gain(self):
205
213
return (reg & 0b01100000 ) & 0xFF
206
214
207
215
@mag_gain .setter
208
- def mag_gain (self , val ) :
216
+ def mag_gain (self , val : int ) -> None :
209
217
assert val in (MAGGAIN_4GAUSS , MAGGAIN_8GAUSS , MAGGAIN_12GAUSS , MAGGAIN_16GAUSS )
210
218
reg = self ._read_u8 (_MAGTYPE , _LSM9DS1_REGISTER_CTRL_REG2_M )
211
219
reg = (reg & ~ (0b01100000 )) & 0xFF
@@ -221,7 +229,7 @@ def mag_gain(self, val):
221
229
self ._mag_mgauss_lsb = _LSM9DS1_MAG_MGAUSS_16GAUSS
222
230
223
231
@property
224
- def gyro_scale (self ):
232
+ def gyro_scale (self ) -> int :
225
233
"""The gyroscope scale. Must be a value of:
226
234
227
235
* GYROSCALE_245DPS
@@ -233,7 +241,7 @@ def gyro_scale(self):
233
241
return (reg & 0b00011000 ) & 0xFF
234
242
235
243
@gyro_scale .setter
236
- def gyro_scale (self , val ) :
244
+ def gyro_scale (self , val : int ) -> None :
237
245
assert val in (GYROSCALE_245DPS , GYROSCALE_500DPS , GYROSCALE_2000DPS )
238
246
reg = self ._read_u8 (_XGTYPE , _LSM9DS1_REGISTER_CTRL_REG1_G )
239
247
reg = (reg & ~ (0b00011000 )) & 0xFF
@@ -246,19 +254,19 @@ def gyro_scale(self, val):
246
254
elif val == GYROSCALE_2000DPS :
247
255
self ._gyro_dps_digit = _LSM9DS1_GYRO_DPS_DIGIT_2000DPS
248
256
249
- def read_accel_raw (self ):
257
+ def read_accel_raw (self ) -> Tuple [ int , int , int ] :
250
258
"""Read the raw accelerometer sensor values and return it as a
251
259
3-tuple of X, Y, Z axis values that are 16-bit unsigned values. If you
252
260
want the acceleration in nice units you probably want to use the
253
- accelerometer property!
261
+ acceleration property!
254
262
"""
255
263
# Read the accelerometer
256
264
self ._read_bytes (_XGTYPE , 0x80 | _LSM9DS1_REGISTER_OUT_X_L_XL , 6 , self ._BUFFER )
257
265
raw_x , raw_y , raw_z = struct .unpack_from ("<hhh" , self ._BUFFER [0 :6 ])
258
266
return (raw_x , raw_y , raw_z )
259
267
260
268
@property
261
- def acceleration (self ):
269
+ def acceleration (self ) -> Tuple [ float , float , float ] :
262
270
"""The accelerometer X, Y, Z axis values as a 3-tuple of
263
271
:math:`m/s^2` values.
264
272
"""
@@ -267,45 +275,45 @@ def acceleration(self):
267
275
lambda x : x * self ._accel_mg_lsb / 1000.0 * _SENSORS_GRAVITY_STANDARD , raw
268
276
)
269
277
270
- def read_mag_raw (self ):
278
+ def read_mag_raw (self ) -> Tuple [ int , int , int ] :
271
279
"""Read the raw magnetometer sensor values and return it as a
272
280
3-tuple of X, Y, Z axis values that are 16-bit unsigned values. If you
273
281
want the magnetometer in nice units you probably want to use the
274
- magnetometer property!
282
+ magnetic property!
275
283
"""
276
284
# Read the magnetometer
277
285
self ._read_bytes (_MAGTYPE , 0x80 | _LSM9DS1_REGISTER_OUT_X_L_M , 6 , self ._BUFFER )
278
286
raw_x , raw_y , raw_z = struct .unpack_from ("<hhh" , self ._BUFFER [0 :6 ])
279
287
return (raw_x , raw_y , raw_z )
280
288
281
289
@property
282
- def magnetic (self ):
290
+ def magnetic (self ) -> Tuple [ float , float , float ] :
283
291
"""The magnetometer X, Y, Z axis values as a 3-tuple of
284
292
gauss values.
285
293
"""
286
294
raw = self .read_mag_raw ()
287
295
return map (lambda x : x * self ._mag_mgauss_lsb / 1000.0 , raw )
288
296
289
- def read_gyro_raw (self ):
297
+ def read_gyro_raw (self ) -> Tuple [ int , int , int ] :
290
298
"""Read the raw gyroscope sensor values and return it as a
291
299
3-tuple of X, Y, Z axis values that are 16-bit unsigned values. If you
292
300
want the gyroscope in nice units you probably want to use the
293
- gyroscope property!
301
+ gyro property!
294
302
"""
295
303
# Read the gyroscope
296
304
self ._read_bytes (_XGTYPE , 0x80 | _LSM9DS1_REGISTER_OUT_X_L_G , 6 , self ._BUFFER )
297
305
raw_x , raw_y , raw_z = struct .unpack_from ("<hhh" , self ._BUFFER [0 :6 ])
298
306
return (raw_x , raw_y , raw_z )
299
307
300
308
@property
301
- def gyro (self ):
309
+ def gyro (self ) -> Tuple [ float , float , float ] :
302
310
"""The gyroscope X, Y, Z axis values as a 3-tuple of
303
311
rad/s values.
304
312
"""
305
313
raw = self .read_gyro_raw ()
306
314
return map (lambda x : radians (x * self ._gyro_dps_digit ), raw )
307
315
308
- def read_temp_raw (self ):
316
+ def read_temp_raw (self ) -> int :
309
317
"""Read the raw temperature sensor value and return it as a 12-bit
310
318
signed value. If you want the temperature in nice units you probably
311
319
want to use the temperature property!
@@ -316,7 +324,7 @@ def read_temp_raw(self):
316
324
return _twos_comp (temp , 12 )
317
325
318
326
@property
319
- def temperature (self ):
327
+ def temperature (self ) -> float :
320
328
"""The temperature of the sensor in degrees Celsius."""
321
329
# This is just a guess since the starting point (21C here) isn't documented :(
322
330
# See discussion from:
@@ -325,21 +333,23 @@ def temperature(self):
325
333
temp = 27.5 + temp / 16
326
334
return temp
327
335
328
- def _read_u8 (self , sensor_type , address ) :
336
+ def _read_u8 (self , sensor_type : bool , address : int ) -> int :
329
337
# Read an 8-bit unsigned value from the specified 8-bit address.
330
338
# The sensor_type boolean should be _MAGTYPE when talking to the
331
339
# magnetometer, or _XGTYPE when talking to the accel or gyro.
332
340
# MUST be implemented by subclasses!
333
341
raise NotImplementedError ()
334
342
335
- def _read_bytes (self , sensor_type , address , count , buf ):
343
+ def _read_bytes (
344
+ self , sensor_type : bool , address : int , count : int , buf : WriteableBuffer
345
+ ) -> None :
336
346
# Read a count number of bytes into buffer from the provided 8-bit
337
347
# register address. The sensor_type boolean should be _MAGTYPE when
338
348
# talking to the magnetometer, or _XGTYPE when talking to the accel or
339
349
# gyro. MUST be implemented by subclasses!
340
350
raise NotImplementedError ()
341
351
342
- def _write_u8 (self , sensor_type , address , val ) :
352
+ def _write_u8 (self , sensor_type : bool , address : int , val : int ) -> None :
343
353
# Write an 8-bit unsigned value to the specified 8-bit address.
344
354
# The sensor_type boolean should be _MAGTYPE when talking to the
345
355
# magnetometer, or _XGTYPE when talking to the accel or gyro.
@@ -393,10 +403,10 @@ class LSM9DS1_I2C(LSM9DS1):
393
403
394
404
def __init__ (
395
405
self ,
396
- i2c ,
397
- mag_address = _LSM9DS1_ADDRESS_MAG ,
398
- xg_address = _LSM9DS1_ADDRESS_ACCELGYRO ,
399
- ):
406
+ i2c : I2C ,
407
+ mag_address : int = _LSM9DS1_ADDRESS_MAG ,
408
+ xg_address : int = _LSM9DS1_ADDRESS_ACCELGYRO ,
409
+ ) -> None :
400
410
if mag_address in (0x1C , 0x1E ) and xg_address in (0x6A , 0x6B ):
401
411
self ._mag_device = i2c_device .I2CDevice (i2c , mag_address )
402
412
self ._xg_device = i2c_device .I2CDevice (i2c , xg_address )
@@ -408,7 +418,7 @@ def __init__(
408
418
"/api.html#adafruit_lsm9ds1.LSM9DS1_I2C"
409
419
)
410
420
411
- def _read_u8 (self , sensor_type , address ) :
421
+ def _read_u8 (self , sensor_type : bool , address : int ) -> int :
412
422
if sensor_type == _MAGTYPE :
413
423
device = self ._mag_device
414
424
else :
@@ -420,7 +430,9 @@ def _read_u8(self, sensor_type, address):
420
430
)
421
431
return self ._BUFFER [1 ]
422
432
423
- def _read_bytes (self , sensor_type , address , count , buf ):
433
+ def _read_bytes (
434
+ self , sensor_type : bool , address : int , count : int , buf : WriteableBuffer
435
+ ) -> None :
424
436
if sensor_type == _MAGTYPE :
425
437
device = self ._mag_device
426
438
else :
@@ -429,7 +441,7 @@ def _read_bytes(self, sensor_type, address, count, buf):
429
441
buf [0 ] = address & 0xFF
430
442
i2c .write_then_readinto (buf , buf , out_end = 1 , in_end = count )
431
443
432
- def _write_u8 (self , sensor_type , address , val ) :
444
+ def _write_u8 (self , sensor_type : bool , address : int , val : int ) -> None :
433
445
if sensor_type == _MAGTYPE :
434
446
device = self ._mag_device
435
447
else :
@@ -482,7 +494,7 @@ class LSM9DS1_SPI(LSM9DS1):
482
494
"""
483
495
484
496
# pylint: disable=no-member
485
- def __init__ (self , spi , xgcs , mcs ) :
497
+ def __init__ (self , spi : SPI , xgcs : DigitalInOut , mcs : DigitalInOut ) -> None :
486
498
self ._mag_device = spi_device .SPIDevice (
487
499
spi , mcs , baudrate = 200000 , phase = 1 , polarity = 1
488
500
)
@@ -491,7 +503,7 @@ def __init__(self, spi, xgcs, mcs):
491
503
)
492
504
super ().__init__ ()
493
505
494
- def _read_u8 (self , sensor_type , address ) :
506
+ def _read_u8 (self , sensor_type : bool , address : int ) -> int :
495
507
if sensor_type == _MAGTYPE :
496
508
device = self ._mag_device
497
509
else :
@@ -502,7 +514,9 @@ def _read_u8(self, sensor_type, address):
502
514
spi .readinto (self ._BUFFER , end = 1 )
503
515
return self ._BUFFER [0 ]
504
516
505
- def _read_bytes (self , sensor_type , address , count , buf ):
517
+ def _read_bytes (
518
+ self , sensor_type : bool , address : int , count : int , buf : WriteableBuffer
519
+ ) -> None :
506
520
if sensor_type == _MAGTYPE :
507
521
device = self ._mag_device
508
522
address |= _SPI_AUTO_INCR
@@ -513,7 +527,7 @@ def _read_bytes(self, sensor_type, address, count, buf):
513
527
spi .write (buf , end = 1 )
514
528
spi .readinto (buf , end = count )
515
529
516
- def _write_u8 (self , sensor_type , address , val ) :
530
+ def _write_u8 (self , sensor_type : bool , address : int , val : int ) -> None :
517
531
if sensor_type == _MAGTYPE :
518
532
device = self ._mag_device
519
533
else :
0 commit comments