Skip to content

Commit

Permalink
Merge pull request #13 from tcfranks/main
Browse files Browse the repository at this point in the history
resolves #12 Missing Type Annotations
  • Loading branch information
tekktrik committed Aug 25, 2022
2 parents 3619e09 + 6ab361b commit 973d3cb
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions adafruit_tmp007.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
from micropython import const
from adafruit_bus_device.i2c_device import I2CDevice

try:
from typing_extensions import Literal
from busio import I2C
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TMP007.git"
Expand Down Expand Up @@ -65,15 +70,25 @@ class TMP007:
# thread safe!
_BUFFER = bytearray(4)

def __init__(self, i2c, address=_TMP007_I2CADDR, samplerate=CFG_16SAMPLE):
def __init__(
self,
i2c: I2C,
address: int = _TMP007_I2CADDR,
samplerate: Literal[
CFG_1SAMPLE,
CFG_2SAMPLE,
CFG_4SAMPLE,
CFG_8SAMPLE,
CFG_16SAMPLE,
] = CFG_16SAMPLE,
) -> None:
"""Initialize TMP007 device on the specified I2C address and bus number.
Address defaults to 0x40 and bus number defaults to the appropriate bus
for the hardware.
Start taking temperature measurements. Samplerate can be one of
TMP007_CFG_1SAMPLE, TMP007_CFG_2SAMPLE, TMP007_CFG_4SAMPLE,
TMP007_CFG_8SAMPLE, or TMP007_CFG_16SAMPLE. The default is 16 samples
for the highest resolution. Returns True if the device is intialized,
False otherwise.
for the highest resolution.
"""
self._device = I2CDevice(i2c, address)
self._write_u16(_TMP007_CONFIG, _TMP007_CFG_RESET)
Expand All @@ -98,22 +113,22 @@ def __init__(self, i2c, address=_TMP007_I2CADDR, samplerate=CFG_16SAMPLE):
if dev_id != 0x78:
raise RuntimeError("Init failed - Did not find TMP007")

def sleep(self):
def sleep(self) -> None:
"""Put TMP007 into low power sleep mode. No measurement data will be
updated while in sleep mode.
"""
control = self._read_u16(_TMP007_CONFIG)
control &= ~(_TMP007_CFG_MODEON)
self._write_u16(_TMP007_CONFIG, control)

def wake(self):
def wake(self) -> None:
"""Wake up TMP007 from low power sleep mode."""
control = self._read_u16(_TMP007_CONFIG)
control |= _TMP007_CFG_MODEON
self._write_u16(_TMP007_CONFIG, control)

@property
def raw_voltage(self):
def raw_voltage(self) -> int:
"""Read raw voltage from TMP007 sensor. Meant to be used in the
calculation of temperature values.
"""
Expand All @@ -123,59 +138,59 @@ def raw_voltage(self):
return raw

@property
def raw_sensor_temperature(self):
def raw_sensor_temperature(self) -> int:
"""Read raw die temperature from TMP007 sensor. Meant to be used in the
calculation of temperature values.
"""
raw = self._read_u16(_TMP007_TAMB)
return raw >> 2

@property
def die_temperature(self):
def die_temperature(self) -> float:
"""Read sensor die temperature and return its value in degrees celsius."""
t_die = self.raw_sensor_temperature
return t_die * 0.03125

@property
def temperature(self):
def temperature(self) -> float:
"""Read object temperature from TMP007 sensor."""
raw = self._read_u16(_TMP007_TOBJ)
if raw & 1:
return -9999.0
raw = raw >> 2
return raw * 0.03125

def read_register(self, register):
def read_register(self, register) -> int:
"""Read sensor Register."""
return self._read_u16(register)

def _read_u8(self, address):
def _read_u8(self, address: int) -> int:
with self._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 _read_u16(self, address):
def _read_u16(self, address: int) -> int:
with self._device as i2c:
self._BUFFER[0] = address & 0xFF
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=2)
return self._BUFFER[0] << 8 | self._BUFFER[1]

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

def _write_u16(self, address, val):
def _write_u16(self, address: int, val: int) -> None:
with self._device as i2c:
self._BUFFER[0] = address & 0xFF
self._BUFFER[1] = (val >> 8) & 0xFF
self._BUFFER[2] = val & 0xFF
i2c.write(self._BUFFER, end=3)

@staticmethod
def _read_bytes(device, address, count, buf):
def _read_bytes(device, 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 973d3cb

Please sign in to comment.