Skip to content

Commit 973d3cb

Browse files
authored
Merge pull request #13 from tcfranks/main
resolves #12 Missing Type Annotations
2 parents 3619e09 + 6ab361b commit 973d3cb

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

adafruit_tmp007.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
from micropython import const
2929
from adafruit_bus_device.i2c_device import I2CDevice
3030

31+
try:
32+
from typing_extensions import Literal
33+
from busio import I2C
34+
except ImportError:
35+
pass
3136

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

68-
def __init__(self, i2c, address=_TMP007_I2CADDR, samplerate=CFG_16SAMPLE):
73+
def __init__(
74+
self,
75+
i2c: I2C,
76+
address: int = _TMP007_I2CADDR,
77+
samplerate: Literal[
78+
CFG_1SAMPLE,
79+
CFG_2SAMPLE,
80+
CFG_4SAMPLE,
81+
CFG_8SAMPLE,
82+
CFG_16SAMPLE,
83+
] = CFG_16SAMPLE,
84+
) -> None:
6985
"""Initialize TMP007 device on the specified I2C address and bus number.
7086
Address defaults to 0x40 and bus number defaults to the appropriate bus
7187
for the hardware.
7288
Start taking temperature measurements. Samplerate can be one of
7389
TMP007_CFG_1SAMPLE, TMP007_CFG_2SAMPLE, TMP007_CFG_4SAMPLE,
7490
TMP007_CFG_8SAMPLE, or TMP007_CFG_16SAMPLE. The default is 16 samples
75-
for the highest resolution. Returns True if the device is intialized,
76-
False otherwise.
91+
for the highest resolution.
7792
"""
7893
self._device = I2CDevice(i2c, address)
7994
self._write_u16(_TMP007_CONFIG, _TMP007_CFG_RESET)
@@ -98,22 +113,22 @@ def __init__(self, i2c, address=_TMP007_I2CADDR, samplerate=CFG_16SAMPLE):
98113
if dev_id != 0x78:
99114
raise RuntimeError("Init failed - Did not find TMP007")
100115

101-
def sleep(self):
116+
def sleep(self) -> None:
102117
"""Put TMP007 into low power sleep mode. No measurement data will be
103118
updated while in sleep mode.
104119
"""
105120
control = self._read_u16(_TMP007_CONFIG)
106121
control &= ~(_TMP007_CFG_MODEON)
107122
self._write_u16(_TMP007_CONFIG, control)
108123

109-
def wake(self):
124+
def wake(self) -> None:
110125
"""Wake up TMP007 from low power sleep mode."""
111126
control = self._read_u16(_TMP007_CONFIG)
112127
control |= _TMP007_CFG_MODEON
113128
self._write_u16(_TMP007_CONFIG, control)
114129

115130
@property
116-
def raw_voltage(self):
131+
def raw_voltage(self) -> int:
117132
"""Read raw voltage from TMP007 sensor. Meant to be used in the
118133
calculation of temperature values.
119134
"""
@@ -123,59 +138,59 @@ def raw_voltage(self):
123138
return raw
124139

125140
@property
126-
def raw_sensor_temperature(self):
141+
def raw_sensor_temperature(self) -> int:
127142
"""Read raw die temperature from TMP007 sensor. Meant to be used in the
128143
calculation of temperature values.
129144
"""
130145
raw = self._read_u16(_TMP007_TAMB)
131146
return raw >> 2
132147

133148
@property
134-
def die_temperature(self):
149+
def die_temperature(self) -> float:
135150
"""Read sensor die temperature and return its value in degrees celsius."""
136151
t_die = self.raw_sensor_temperature
137152
return t_die * 0.03125
138153

139154
@property
140-
def temperature(self):
155+
def temperature(self) -> float:
141156
"""Read object temperature from TMP007 sensor."""
142157
raw = self._read_u16(_TMP007_TOBJ)
143158
if raw & 1:
144159
return -9999.0
145160
raw = raw >> 2
146161
return raw * 0.03125
147162

148-
def read_register(self, register):
163+
def read_register(self, register) -> int:
149164
"""Read sensor Register."""
150165
return self._read_u16(register)
151166

152-
def _read_u8(self, address):
167+
def _read_u8(self, address: int) -> int:
153168
with self._device as i2c:
154169
self._BUFFER[0] = address & 0xFF
155170
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=1)
156171
return self._BUFFER[0]
157172

158-
def _read_u16(self, address):
173+
def _read_u16(self, address: int) -> int:
159174
with self._device as i2c:
160175
self._BUFFER[0] = address & 0xFF
161176
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=2)
162177
return self._BUFFER[0] << 8 | self._BUFFER[1]
163178

164-
def _write_u8(self, address, val):
179+
def _write_u8(self, address: int, val: int) -> None:
165180
with self._device as i2c:
166181
self._BUFFER[0] = address & 0xFF
167182
self._BUFFER[1] = val & 0xFF
168183
i2c.write(self._BUFFER, end=2)
169184

170-
def _write_u16(self, address, val):
185+
def _write_u16(self, address: int, val: int) -> None:
171186
with self._device as i2c:
172187
self._BUFFER[0] = address & 0xFF
173188
self._BUFFER[1] = (val >> 8) & 0xFF
174189
self._BUFFER[2] = val & 0xFF
175190
i2c.write(self._BUFFER, end=3)
176191

177192
@staticmethod
178-
def _read_bytes(device, address, count, buf):
193+
def _read_bytes(device, address: int, count: int, buf: bytearray) -> None:
179194
with device as i2c:
180195
buf[0] = address & 0xFF
181196
i2c.write_then_readinto(buf, buf, out_end=1, in_end=count)

0 commit comments

Comments
 (0)