Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Missing Type Annotations #27

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions adafruit_l3gd20.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
from micropython import const
from adafruit_register.i2c_struct import Struct

try:
from typing import Tuple
from digitalio import DigitalInOut
from busio import I2C, SPI
from circuitpython_typing import WriteableBuffer
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_L3GD20.git"

Expand Down Expand Up @@ -88,12 +96,13 @@ class L3GD20:
Defaults to :const:`L3DS20_RATE_100HZ`
"""

def __init__(self, rng=L3DS20_RANGE_250DPS, rate=L3DS20_RATE_100HZ):
def __init__(
self, rng: int = L3DS20_RANGE_250DPS, rate: int = L3DS20_RATE_100HZ
) -> None:
chip_id = self.read_register(_ID_REGISTER)
if chip_id not in (_L3GD20_CHIP_ID, _L3GD20H_CHIP_ID):
raise RuntimeError(
"bad chip id (%x != %x or %x)"
% (chip_id, _L3GD20_CHIP_ID, _L3GD20H_CHIP_ID)
f"bad chip id ({chip_id:#x} != {_L3GD20_CHIP_ID:#x} or {_L3GD20H_CHIP_ID:#x})"
)

if rng not in (L3DS20_RANGE_250DPS, L3DS20_RANGE_500DPS, L3DS20_RANGE_2000DPS):
Expand Down Expand Up @@ -185,7 +194,7 @@ def __init__(self, rng=L3DS20_RANGE_250DPS, rate=L3DS20_RATE_100HZ):
# ------------------------------------------------------------------

@property
def gyro(self):
def gyro(self) -> Tuple[float, float, float]:
"""
x, y, z angular momentum tuple floats, rescaled appropriately for
range selected in rad/s
Expand Down Expand Up @@ -233,8 +242,12 @@ class L3GD20_I2C(L3GD20):
"""Gives the raw gyro readings, in units of rad/s."""

def __init__(
self, i2c, rng=L3DS20_RANGE_250DPS, address=0x6B, rate=L3DS20_RATE_100HZ
):
self,
i2c: I2C,
rng: int = L3DS20_RANGE_250DPS,
address: int = 0x6B,
rate: int = L3DS20_RATE_100HZ,
) -> None:
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel
i2c_device,
)
Expand All @@ -243,7 +256,7 @@ def __init__(
self.buffer = bytearray(2)
super().__init__(rng, rate)

def write_register(self, register, value):
def write_register(self, register: int, value: int) -> None:
"""
Update a register with a byte value

Expand All @@ -255,7 +268,7 @@ def write_register(self, register, value):
with self.i2c_device as i2c:
i2c.write(self.buffer)

def read_register(self, register):
def read_register(self, register: int) -> int:
"""
Returns a byte value from a register

Expand Down Expand Up @@ -303,14 +316,14 @@ class L3GD20_SPI(L3GD20):

"""

def __init__(
def __init__( # pylint: disable=too-many-arguments
self,
spi_busio,
cs,
rng=L3DS20_RANGE_250DPS,
baudrate=100000,
rate=L3DS20_RATE_100HZ,
): # pylint: disable=too-many-arguments
spi_busio: SPI,
cs: DigitalInOut,
rng: int = L3DS20_RANGE_250DPS,
baudrate: int = 100000,
rate: int = L3DS20_RATE_100HZ,
) -> None:
from adafruit_bus_device import ( # pylint: disable=import-outside-toplevel
spi_device,
)
Expand All @@ -320,7 +333,7 @@ def __init__(
self._spi_bytearray6 = bytearray(6)
super().__init__(rng, rate)

def write_register(self, register, value):
def write_register(self, register: int, value: int) -> None:
"""
Low level register writing over SPI, writes one 8-bit value

Expand All @@ -331,7 +344,7 @@ def write_register(self, register, value):
with self._spi as spi:
spi.write(bytes([register, value & 0xFF]))

def read_register(self, register):
def read_register(self, register: int) -> int:
"""
Low level register reading over SPI, returns a list of values

Expand All @@ -346,7 +359,7 @@ def read_register(self, register):
# print("$%02X => %s" % (register, [hex(i) for i in self._spi_bytearray1]))
return self._spi_bytearray1[0]

def read_bytes(self, register, buffer):
def read_bytes(self, register: int, buffer: WriteableBuffer) -> None:
"""
Low level register stream reading over SPI, returns a list of values

Expand All @@ -360,7 +373,7 @@ def read_bytes(self, register, buffer):
spi.readinto(buffer)

@property
def gyro_raw(self):
def gyro_raw(self) -> Tuple[int, int, int]:
"""Gives the dynamic rate raw gyro readings, in units rad/s."""
buffer = self._spi_bytearray6
self.read_bytes(_L3GD20_REGISTER_OUT_X_L_X40, buffer)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

Adafruit-Blinka
adafruit-circuitpython-register
adafruit-circuitpython-typing