Skip to content

Issue18 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
Feb 13, 2022
Merged
Changes from 4 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
93 changes: 51 additions & 42 deletions adafruit_adt7410.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,29 @@
from adafruit_register.i2c_bit import RWBit, ROBit
from micropython import const

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


_ADT7410_TEMPMSB = const(0x0)
_ADT7410_TEMPLSB = const(0x1)
_ADT7410_STATUS = const(0x2)
_ADT7410_CONFIG = const(0x3)
_ADT7410_THIGHMSB = const(0x4)
_ADT7410_THIGHLSB = const(0x5)
_ADT7410_TLOWMSB = const(0x6)
_ADT7410_TLOWLSB = const(0x7)
_ADT7410_TCRITMSB = const(0x8)
_ADT7410_TCRITLSB = const(0x9)
_ADT7410_THYST = const(0x0A)
_ADT7410_ID = const(0xB)
_ADT7410_SWRST = const(0x2F)
try:
# Used only for typing
import busio # pylint: disable=unused-import
except ImportError:
pass

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


_ADT7410_TEMPMSB: int = const(0x0)
_ADT7410_TEMPLSB: int = const(0x1)
_ADT7410_STATUS: int = const(0x2)
_ADT7410_CONFIG: int = const(0x3)
_ADT7410_THIGHMSB: int = const(0x4)
_ADT7410_THIGHLSB: int = const(0x5)
_ADT7410_TLOWMSB: int = const(0x6)
_ADT7410_TLOWLSB: int = const(0x7)
_ADT7410_TCRITMSB: int = const(0x8)
_ADT7410_TCRITLSB: int = const(0x9)
_ADT7410_THYST: int = const(0x0A)
_ADT7410_ID: int = const(0xB)
_ADT7410_SWRST: int = const(0x2F)


class ADT7410:
Expand Down Expand Up @@ -91,17 +97,18 @@ class ADT7410:
"""

# many modes can be set with register objects for simplicity
ready = ROBit(_ADT7410_STATUS, 7)
ctpin_polarity = RWBit(_ADT7410_CONFIG, 2)
intpin_polarity = RWBit(_ADT7410_CONFIG, 3)
comparator_mode = RWBit(_ADT7410_CONFIG, 4)
high_resolution = RWBit(_ADT7410_CONFIG, 7)
ready: ROBit = ROBit(_ADT7410_STATUS, 7)
ctpin_polarity: RWBit = RWBit(_ADT7410_CONFIG, 2)
intpin_polarity: RWBit = RWBit(_ADT7410_CONFIG, 3)
comparator_mode: RWBit = RWBit(_ADT7410_CONFIG, 4)
high_resolution: RWBit = RWBit(_ADT7410_CONFIG, 7)

# Status Information configuration
temp_over_critiq = ROBit(_ADT7410_STATUS, 6)
temp_over_high = ROBit(_ADT7410_STATUS, 5)
temp_under_low = ROBit(_ADT7410_STATUS, 4)
temp_over_critiq: ROBit = ROBit(_ADT7410_STATUS, 6)
temp_over_high: ROBit = ROBit(_ADT7410_STATUS, 5)
temp_under_low: ROBit = ROBit(_ADT7410_STATUS, 4)

def __init__(self, i2c_bus, address=0x48):
def __init__(self, i2c_bus: busio.I2C, address: int = 0x48):
self.i2c_device = I2CDevice(i2c_bus, address)
self._buf = bytearray(3)
# Verify the manufacturer and device ids to ensure we are talking to
Expand All @@ -114,39 +121,39 @@ def __init__(self, i2c_bus, address=0x48):
self.reset()

@property
def temperature(self):
def temperature(self) -> float:
"""The temperature in Celsius"""
temp = self._read_register(_ADT7410_TEMPMSB, 2)
return struct.unpack(">h", temp)[0] / 128

@property
def status(self):
def status(self) -> int:
"""The ADT7410 status registers current value"""
return self._read_register(_ADT7410_STATUS)[0]

@property
def configuration(self):
def configuration(self) -> int:
"""The ADT7410 configuration register"""
return self._read_register(_ADT7410_CONFIG)[0]

@configuration.setter
def configuration(self, val):
def configuration(self, val: int) -> None:
self._write_register(_ADT7410_CONFIG, val)

def reset(self):
def reset(self) -> None:
"""Perform a software reset"""
self._write_register(_ADT7410_SWRST)
time.sleep(0.5)

def _read_register(self, addr, num=1):
def _read_register(self, addr: int, num: int = 1) -> int:
self._buf[0] = addr
with self.i2c_device as i2c:
i2c.write_then_readinto(
self._buf, self._buf, out_end=1, in_start=1, in_end=num + 1
)
return self._buf[1 : num + 1]

def _write_register(self, addr, data=None):
def _write_register(self, addr: int, data: int = None) -> None:
self._buf[0] = addr
end = 1
if data:
Expand All @@ -156,39 +163,39 @@ def _write_register(self, addr, data=None):
i2c.write(self._buf, end=end)

@property
def high_temperature(self):
def high_temperature(self) -> int:
"""The over temperature limit value in Celsius"""
temp = self._read_register(_ADT7410_THIGHMSB, 2)
return struct.unpack(">h", temp)[0] / 128

@high_temperature.setter
def high_temperature(self, value):
def high_temperature(self, value: int) -> None:
value = struct.pack(">h", int(value * 128))
self._write_register(_ADT7410_THIGHMSB, value[0])
self._write_register(_ADT7410_THIGHLSB, value[1])

@property
def low_temperature(self):
def low_temperature(self) -> int:
"""The over temperature limit value in Celsius. Only works when
comparator mode is selected"""
temp = self._read_register(_ADT7410_TLOWMSB, 2)
return struct.unpack(">h", temp)[0] / 128

@low_temperature.setter
def low_temperature(self, value):
def low_temperature(self, value: int) -> None:
value = struct.pack(">h", int(value * 128))
self._write_register(_ADT7410_TLOWMSB, value[0])
self._write_register(_ADT7410_TLOWLSB, value[1])

@property
def critical_temperature(self):
def critical_temperature(self) -> int:
"""The critical temperature limit value in Celsius. Only works when
comparator mode is selected"""
temp = self._read_register(_ADT7410_TCRITMSB, 2)
return struct.unpack(">h", temp)[0] / 128

@critical_temperature.setter
def critical_temperature(self, value):
def critical_temperature(self, value: int) -> None:
"""The over temperature limit value in Celsius
There is a bug in the sensor, so the address 0x09 could no be written to 0x00
for this reason only odd numbers could be given. We could make the 0x09 with
Expand All @@ -200,15 +207,17 @@ def critical_temperature(self, value):
self._write_register(_ADT7410_TCRITLSB, value[1])

@property
def hysteresis(self):
def hysteresis(self) -> int:
"""The hysteresis temperature limit value in Celsius. Only works when
comparator mode is selected. From 0 to 15 Celsius"""
temp = self._read_register(_ADT7410_THYST)[0]
return temp

@hysteresis.setter
def hysteresis(self, value):
def hysteresis(self, value: int) -> None:
if value > 15 or isinstance(value, float):
raise Exception("Hysteresis value must be an integer lower than 15 Celsius")
raise ValueError(
"Hysteresis value must be an integer lower than 15 Celsius"
)

self._write_register(_ADT7410_THYST, value)