Skip to content

Commit

Permalink
Merge pull request #20 from jposada202020/type_annotations
Browse files Browse the repository at this point in the history
type_annotations
  • Loading branch information
tekktrik committed Jan 28, 2023
2 parents b946cfa + 43cf358 commit 2638333
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions adafruit_lsm303dlh_mag.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""

try:
from typing import Tuple
from busio import I2C
except ImportError:
pass

try:
import struct
except ImportError:
import ustruct as struct

from micropython import const
from adafruit_bus_device.i2c_device import I2CDevice

Expand Down Expand Up @@ -125,7 +132,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 @@ -136,7 +143,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 @@ -145,7 +152,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 @@ -157,12 +164,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 @@ -198,12 +205,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 @@ -219,20 +226,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)

0 comments on commit 2638333

Please sign in to comment.