Skip to content

Commit

Permalink
Add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Jul 10, 2022
1 parent 0dc43b0 commit d1253e0
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions adafruit_mmc56x3.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
from adafruit_register.i2c_struct import ROUnaryStruct, UnaryStruct
from adafruit_register.i2c_bit import RWBit

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

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MMC56x3.git"

Expand Down Expand Up @@ -76,7 +82,7 @@ class MMC5603:
mag_x, mag_y, mag_z = sensor.magnetic
:param ~busio.I2C i2c_bus: The I2C bus the MMC5603 is connected to.
:param address: The I2C device address. Defaults to :const:`0x30`
:param int address: The I2C device address. Defaults to :const:`0x30`
"""

_chip_id = ROUnaryStruct(_MMC5603_PRODUCT_ID, "<B")
Expand All @@ -91,7 +97,7 @@ class MMC5603:
_meas_m_done = RWBit(_MMC5603_STATUS_REG, 6)
_meas_t_done = RWBit(_MMC5603_STATUS_REG, 7)

def __init__(self, i2c_bus, address=_MMC5603_I2CADDR_DEFAULT):
def __init__(self, i2c_bus: I2C, address: int = _MMC5603_I2CADDR_DEFAULT) -> None:
# pylint: disable=no-member
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
if self._chip_id != _MMC5603_CHIP_ID:
Expand All @@ -101,7 +107,7 @@ def __init__(self, i2c_bus, address=_MMC5603_I2CADDR_DEFAULT):
self._buffer = bytearray(9)
# self.performance_mode = PerformanceMode.MODE_ULTRA

def reset(self):
def reset(self) -> None:
"""Reset the sensor to the default state set by the library"""
self._ctrl1_reg = 0x80 # write only, set topmost bit
time.sleep(0.020)
Expand All @@ -110,7 +116,7 @@ def reset(self):
self.set_reset()

@property
def temperature(self):
def temperature(self) -> float:
"""The processed temperature sensor value, returned in floating point C"""
if self.continuous_mode:
raise RuntimeError("Can only read temperature when not in continuous mode")
Expand All @@ -123,7 +129,7 @@ def temperature(self):
return temp

@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 @@ -149,13 +155,13 @@ def magnetic(self):
return (x, y, z)

@property
def data_rate(self):
def data_rate(self) -> int:
"""Output data rate, 0 for on-request data.
1-255 or 1000 for freq of continuous-mode readings"""
return self._odr_cache

@data_rate.setter
def data_rate(self, value):
def data_rate(self, value: int) -> None:
if not ((value == 1000) or (0 <= value <= 255)):
raise ValueError("Data rate must be 0-255 or 1000 Hz")
self._odr_cache = value
Expand All @@ -169,22 +175,22 @@ def data_rate(self, value):
self._ctrl2_reg = self._ctrl2_cache

@property
def continuous_mode(self):
def continuous_mode(self) -> bool:
"""Whether or not to put the chip in continous mode - be sure
to set the data_rate as well!
"""
return self._ctrl2_cache & 0x10

@continuous_mode.setter
def continuous_mode(self, value):
def continuous_mode(self, value: bool) -> None:
if value:
self._ctrl0_reg = 0x80 # turn on cmm_freq_en bit
self._ctrl2_cache |= 0x10 # turn on cmm_en bit
else:
self._ctrl2_cache &= ~0x10 # turn off cmm_en bit
self._ctrl2_reg = self._ctrl2_cache

def set_reset(self):
def set_reset(self) -> None:
"""Pulse large currents through the sense coils to clear any offset"""
self._ctrl0_reg = 0x08 # turn on set bit
time.sleep(0.001) # 1 ms
Expand Down

0 comments on commit d1253e0

Please sign in to comment.