Skip to content

Commit 0b40160

Browse files
authored
Merge pull request #36 from tcfranks/main
Add Missing Type Annotations
2 parents c623eb0 + a6534db commit 0b40160

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

adafruit_lsm9ds1.py

+47-33
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
from adafruit_bus_device import spi_device
4343
from micropython import const
4444

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+
4553
# Internal constants and register values:
4654
_LSM9DS1_ADDRESS_ACCELGYRO = const(0x6B)
4755
_LSM9DS1_ADDRESS_MAG = const(0x1E)
@@ -119,7 +127,7 @@
119127
GYROSCALE_2000DPS = 0b11 << 3 # +/- 2000 degrees/s rotation
120128

121129

122-
def _twos_comp(val, bits):
130+
def _twos_comp(val: int, bits: int) -> int:
123131
# Convert an unsigned integer in 2's compliment form of the specified bit
124132
# length to its signed integer value and return it.
125133
if val & (1 << (bits - 1)) != 0:
@@ -135,7 +143,7 @@ class LSM9DS1:
135143
# thread safe!
136144
_BUFFER = bytearray(6)
137145

138-
def __init__(self):
146+
def __init__(self) -> None:
139147
# soft reset & reboot accel/gyro
140148
self._write_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG8, 0x05)
141149
# soft reset & reboot magnetometer
@@ -163,7 +171,7 @@ def __init__(self):
163171
self.gyro_scale = GYROSCALE_245DPS
164172

165173
@property
166-
def accel_range(self):
174+
def accel_range(self) -> int:
167175
"""The accelerometer range. Must be a value of:
168176
169177
- ACCELRANGE_2G
@@ -176,7 +184,7 @@ def accel_range(self):
176184
return (reg & 0b00011000) & 0xFF
177185

178186
@accel_range.setter
179-
def accel_range(self, val):
187+
def accel_range(self, val: int) -> None:
180188
assert val in (ACCELRANGE_2G, ACCELRANGE_4G, ACCELRANGE_8G, ACCELRANGE_16G)
181189
reg = self._read_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG6_XL)
182190
reg = (reg & ~(0b00011000)) & 0xFF
@@ -192,7 +200,7 @@ def accel_range(self, val):
192200
self._accel_mg_lsb = _LSM9DS1_ACCEL_MG_LSB_16G
193201

194202
@property
195-
def mag_gain(self):
203+
def mag_gain(self) -> int:
196204
"""The magnetometer gain. Must be a value of:
197205
198206
- MAGGAIN_4GAUSS
@@ -205,7 +213,7 @@ def mag_gain(self):
205213
return (reg & 0b01100000) & 0xFF
206214

207215
@mag_gain.setter
208-
def mag_gain(self, val):
216+
def mag_gain(self, val: int) -> None:
209217
assert val in (MAGGAIN_4GAUSS, MAGGAIN_8GAUSS, MAGGAIN_12GAUSS, MAGGAIN_16GAUSS)
210218
reg = self._read_u8(_MAGTYPE, _LSM9DS1_REGISTER_CTRL_REG2_M)
211219
reg = (reg & ~(0b01100000)) & 0xFF
@@ -221,7 +229,7 @@ def mag_gain(self, val):
221229
self._mag_mgauss_lsb = _LSM9DS1_MAG_MGAUSS_16GAUSS
222230

223231
@property
224-
def gyro_scale(self):
232+
def gyro_scale(self) -> int:
225233
"""The gyroscope scale. Must be a value of:
226234
227235
* GYROSCALE_245DPS
@@ -233,7 +241,7 @@ def gyro_scale(self):
233241
return (reg & 0b00011000) & 0xFF
234242

235243
@gyro_scale.setter
236-
def gyro_scale(self, val):
244+
def gyro_scale(self, val: int) -> None:
237245
assert val in (GYROSCALE_245DPS, GYROSCALE_500DPS, GYROSCALE_2000DPS)
238246
reg = self._read_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG1_G)
239247
reg = (reg & ~(0b00011000)) & 0xFF
@@ -246,19 +254,19 @@ def gyro_scale(self, val):
246254
elif val == GYROSCALE_2000DPS:
247255
self._gyro_dps_digit = _LSM9DS1_GYRO_DPS_DIGIT_2000DPS
248256

249-
def read_accel_raw(self):
257+
def read_accel_raw(self) -> Tuple[int, int, int]:
250258
"""Read the raw accelerometer sensor values and return it as a
251259
3-tuple of X, Y, Z axis values that are 16-bit unsigned values. If you
252260
want the acceleration in nice units you probably want to use the
253-
accelerometer property!
261+
acceleration property!
254262
"""
255263
# Read the accelerometer
256264
self._read_bytes(_XGTYPE, 0x80 | _LSM9DS1_REGISTER_OUT_X_L_XL, 6, self._BUFFER)
257265
raw_x, raw_y, raw_z = struct.unpack_from("<hhh", self._BUFFER[0:6])
258266
return (raw_x, raw_y, raw_z)
259267

260268
@property
261-
def acceleration(self):
269+
def acceleration(self) -> Tuple[float, float, float]:
262270
"""The accelerometer X, Y, Z axis values as a 3-tuple of
263271
:math:`m/s^2` values.
264272
"""
@@ -267,45 +275,45 @@ def acceleration(self):
267275
lambda x: x * self._accel_mg_lsb / 1000.0 * _SENSORS_GRAVITY_STANDARD, raw
268276
)
269277

270-
def read_mag_raw(self):
278+
def read_mag_raw(self) -> Tuple[int, int, int]:
271279
"""Read the raw magnetometer sensor values and return it as a
272280
3-tuple of X, Y, Z axis values that are 16-bit unsigned values. If you
273281
want the magnetometer in nice units you probably want to use the
274-
magnetometer property!
282+
magnetic property!
275283
"""
276284
# Read the magnetometer
277285
self._read_bytes(_MAGTYPE, 0x80 | _LSM9DS1_REGISTER_OUT_X_L_M, 6, self._BUFFER)
278286
raw_x, raw_y, raw_z = struct.unpack_from("<hhh", self._BUFFER[0:6])
279287
return (raw_x, raw_y, raw_z)
280288

281289
@property
282-
def magnetic(self):
290+
def magnetic(self) -> Tuple[float, float, float]:
283291
"""The magnetometer X, Y, Z axis values as a 3-tuple of
284292
gauss values.
285293
"""
286294
raw = self.read_mag_raw()
287295
return map(lambda x: x * self._mag_mgauss_lsb / 1000.0, raw)
288296

289-
def read_gyro_raw(self):
297+
def read_gyro_raw(self) -> Tuple[int, int, int]:
290298
"""Read the raw gyroscope sensor values and return it as a
291299
3-tuple of X, Y, Z axis values that are 16-bit unsigned values. If you
292300
want the gyroscope in nice units you probably want to use the
293-
gyroscope property!
301+
gyro property!
294302
"""
295303
# Read the gyroscope
296304
self._read_bytes(_XGTYPE, 0x80 | _LSM9DS1_REGISTER_OUT_X_L_G, 6, self._BUFFER)
297305
raw_x, raw_y, raw_z = struct.unpack_from("<hhh", self._BUFFER[0:6])
298306
return (raw_x, raw_y, raw_z)
299307

300308
@property
301-
def gyro(self):
309+
def gyro(self) -> Tuple[float, float, float]:
302310
"""The gyroscope X, Y, Z axis values as a 3-tuple of
303311
rad/s values.
304312
"""
305313
raw = self.read_gyro_raw()
306314
return map(lambda x: radians(x * self._gyro_dps_digit), raw)
307315

308-
def read_temp_raw(self):
316+
def read_temp_raw(self) -> int:
309317
"""Read the raw temperature sensor value and return it as a 12-bit
310318
signed value. If you want the temperature in nice units you probably
311319
want to use the temperature property!
@@ -316,7 +324,7 @@ def read_temp_raw(self):
316324
return _twos_comp(temp, 12)
317325

318326
@property
319-
def temperature(self):
327+
def temperature(self) -> float:
320328
"""The temperature of the sensor in degrees Celsius."""
321329
# This is just a guess since the starting point (21C here) isn't documented :(
322330
# See discussion from:
@@ -325,21 +333,23 @@ def temperature(self):
325333
temp = 27.5 + temp / 16
326334
return temp
327335

328-
def _read_u8(self, sensor_type, address):
336+
def _read_u8(self, sensor_type: bool, address: int) -> int:
329337
# Read an 8-bit unsigned value from the specified 8-bit address.
330338
# The sensor_type boolean should be _MAGTYPE when talking to the
331339
# magnetometer, or _XGTYPE when talking to the accel or gyro.
332340
# MUST be implemented by subclasses!
333341
raise NotImplementedError()
334342

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:
336346
# Read a count number of bytes into buffer from the provided 8-bit
337347
# register address. The sensor_type boolean should be _MAGTYPE when
338348
# talking to the magnetometer, or _XGTYPE when talking to the accel or
339349
# gyro. MUST be implemented by subclasses!
340350
raise NotImplementedError()
341351

342-
def _write_u8(self, sensor_type, address, val):
352+
def _write_u8(self, sensor_type: bool, address: int, val: int) -> None:
343353
# Write an 8-bit unsigned value to the specified 8-bit address.
344354
# The sensor_type boolean should be _MAGTYPE when talking to the
345355
# magnetometer, or _XGTYPE when talking to the accel or gyro.
@@ -393,10 +403,10 @@ class LSM9DS1_I2C(LSM9DS1):
393403

394404
def __init__(
395405
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:
400410
if mag_address in (0x1C, 0x1E) and xg_address in (0x6A, 0x6B):
401411
self._mag_device = i2c_device.I2CDevice(i2c, mag_address)
402412
self._xg_device = i2c_device.I2CDevice(i2c, xg_address)
@@ -408,7 +418,7 @@ def __init__(
408418
"/api.html#adafruit_lsm9ds1.LSM9DS1_I2C"
409419
)
410420

411-
def _read_u8(self, sensor_type, address):
421+
def _read_u8(self, sensor_type: bool, address: int) -> int:
412422
if sensor_type == _MAGTYPE:
413423
device = self._mag_device
414424
else:
@@ -420,7 +430,9 @@ def _read_u8(self, sensor_type, address):
420430
)
421431
return self._BUFFER[1]
422432

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:
424436
if sensor_type == _MAGTYPE:
425437
device = self._mag_device
426438
else:
@@ -429,7 +441,7 @@ def _read_bytes(self, sensor_type, address, count, buf):
429441
buf[0] = address & 0xFF
430442
i2c.write_then_readinto(buf, buf, out_end=1, in_end=count)
431443

432-
def _write_u8(self, sensor_type, address, val):
444+
def _write_u8(self, sensor_type: bool, address: int, val: int) -> None:
433445
if sensor_type == _MAGTYPE:
434446
device = self._mag_device
435447
else:
@@ -482,7 +494,7 @@ class LSM9DS1_SPI(LSM9DS1):
482494
"""
483495

484496
# pylint: disable=no-member
485-
def __init__(self, spi, xgcs, mcs):
497+
def __init__(self, spi: SPI, xgcs: DigitalInOut, mcs: DigitalInOut) -> None:
486498
self._mag_device = spi_device.SPIDevice(
487499
spi, mcs, baudrate=200000, phase=1, polarity=1
488500
)
@@ -491,7 +503,7 @@ def __init__(self, spi, xgcs, mcs):
491503
)
492504
super().__init__()
493505

494-
def _read_u8(self, sensor_type, address):
506+
def _read_u8(self, sensor_type: bool, address: int) -> int:
495507
if sensor_type == _MAGTYPE:
496508
device = self._mag_device
497509
else:
@@ -502,7 +514,9 @@ def _read_u8(self, sensor_type, address):
502514
spi.readinto(self._BUFFER, end=1)
503515
return self._BUFFER[0]
504516

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:
506520
if sensor_type == _MAGTYPE:
507521
device = self._mag_device
508522
address |= _SPI_AUTO_INCR
@@ -513,7 +527,7 @@ def _read_bytes(self, sensor_type, address, count, buf):
513527
spi.write(buf, end=1)
514528
spi.readinto(buf, end=count)
515529

516-
def _write_u8(self, sensor_type, address, val):
530+
def _write_u8(self, sensor_type: bool, address: int, val: int) -> None:
517531
if sensor_type == _MAGTYPE:
518532
device = self._mag_device
519533
else:

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
adafruit-circuitpython-typing

0 commit comments

Comments
 (0)