Skip to content

type_annotations #20

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

Merged
merged 5 commits into from
Jan 28, 2023
Merged
Changes from 2 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
28 changes: 18 additions & 10 deletions adafruit_lsm303dlh_mag.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""

from typing import Tuple

try:
import struct
except ImportError:
import ustruct as struct
try:
from busio import I2C
except ImportError:
pass
from micropython import const
from adafruit_bus_device.i2c_device import I2CDevice

Expand Down Expand Up @@ -122,7 +128,7 @@ class LSM303DLH_Mag:
# thread safe!
_BUFFER = bytearray(6)

def __init__(self, i2c):
def __init__(self, i2c: I2C) -> None:
self._mag_device = I2CDevice(i2c, _ADDRESS_MAG)
self._write_u8(
self._mag_device, _REG_MAG_MR_REG_M, 0x00
Expand All @@ -133,7 +139,7 @@ def __init__(self, i2c):
self._mag_rate = MAGRATE_0_7

@property
def _raw_magnetic(self):
def _raw_magnetic(self) -> Tuple[int, int, int]:
"""The raw magnetometer sensor values.
A 3-tuple of X, Y, Z axis values that are 16-bit signed integers.
"""
Expand All @@ -142,7 +148,7 @@ def _raw_magnetic(self):
return (raw_values[0], raw_values[2], raw_values[1])

@property
def magnetic(self):
def magnetic(self) -> Tuple[float, float, float]:
"""The processed magnetometer sensor values.
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.
"""
Expand All @@ -154,12 +160,12 @@ def magnetic(self):
)

@property
def mag_gain(self):
def mag_gain(self) -> int:
"""The magnetometer's gain."""
return self._mag_gain

@mag_gain.setter
def mag_gain(self, value):
def mag_gain(self, value: int) -> None:
assert value in (
MAGGAIN_1_3,
MAGGAIN_1_9,
Expand Down Expand Up @@ -195,12 +201,12 @@ def mag_gain(self, value):
self._lsm303mag_gauss_lsb_z = 205.0

@property
def mag_rate(self):
def mag_rate(self) -> int:
"""The magnetometer update rate."""
return self._mag_rate

@mag_rate.setter
def mag_rate(self, value):
def mag_rate(self, value: int) -> None:
assert value in (
MAGRATE_0_7,
MAGRATE_1_5,
Expand All @@ -216,20 +222,22 @@ def mag_rate(self, value):
reg_m = ((value & 0x07) << 2) & 0xFF
self._write_u8(self._mag_device, _REG_MAG_CRA_REG_M, reg_m)

def _read_u8(self, device, address):
def _read_u8(self, device: I2CDevice, address: int) -> int:
with device as i2c:
self._BUFFER[0] = address & 0xFF
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=1)
return self._BUFFER[0]

def _write_u8(self, device, address, val):
def _write_u8(self, device: I2CDevice, address: int, val: int) -> None:
with device as i2c:
self._BUFFER[0] = address & 0xFF
self._BUFFER[1] = val & 0xFF
i2c.write(self._BUFFER, end=2)

@staticmethod
def _read_bytes(device, address, count, buf):
def _read_bytes(
device: I2CDevice, address: int, count: int, buf: bytearray
) -> None:
with device as i2c:
buf[0] = address & 0xFF
i2c.write_then_readinto(buf, buf, out_end=1, in_end=count)