Skip to content

Commit dc048f8

Browse files
committed
Correct Missing Type Annotations
1 parent 9627ddb commit dc048f8

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

adafruit_tmp007.py

+29-13
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,7 +70,18 @@ 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.
@@ -98,22 +114,22 @@ def __init__(self, i2c, address=_TMP007_I2CADDR, samplerate=CFG_16SAMPLE):
98114
if dev_id != 0x78:
99115
raise RuntimeError("Init failed - Did not find TMP007")
100116

101-
def sleep(self):
117+
def sleep(self) -> None:
102118
"""Put TMP007 into low power sleep mode. No measurement data will be
103119
updated while in sleep mode.
104120
"""
105121
control = self._read_u16(_TMP007_CONFIG)
106122
control &= ~(_TMP007_CFG_MODEON)
107123
self._write_u16(_TMP007_CONFIG, control)
108124

109-
def wake(self):
125+
def wake(self) -> None:
110126
"""Wake up TMP007 from low power sleep mode."""
111127
control = self._read_u16(_TMP007_CONFIG)
112128
control |= _TMP007_CFG_MODEON
113129
self._write_u16(_TMP007_CONFIG, control)
114130

115131
@property
116-
def raw_voltage(self):
132+
def raw_voltage(self) -> int:
117133
"""Read raw voltage from TMP007 sensor. Meant to be used in the
118134
calculation of temperature values.
119135
"""
@@ -123,59 +139,59 @@ def raw_voltage(self):
123139
return raw
124140

125141
@property
126-
def raw_sensor_temperature(self):
142+
def raw_sensor_temperature(self) -> int:
127143
"""Read raw die temperature from TMP007 sensor. Meant to be used in the
128144
calculation of temperature values.
129145
"""
130146
raw = self._read_u16(_TMP007_TAMB)
131147
return raw >> 2
132148

133149
@property
134-
def die_temperature(self):
150+
def die_temperature(self) -> float:
135151
"""Read sensor die temperature and return its value in degrees celsius."""
136152
t_die = self.raw_sensor_temperature
137153
return t_die * 0.03125
138154

139155
@property
140-
def temperature(self):
156+
def temperature(self) -> float:
141157
"""Read object temperature from TMP007 sensor."""
142158
raw = self._read_u16(_TMP007_TOBJ)
143159
if raw & 1:
144160
return -9999.0
145161
raw = raw >> 2
146162
return raw * 0.03125
147163

148-
def read_register(self, register):
164+
def read_register(self, register) -> int:
149165
"""Read sensor Register."""
150166
return self._read_u16(register)
151167

152-
def _read_u8(self, address):
168+
def _read_u8(self, address: int) -> int:
153169
with self._device as i2c:
154170
self._BUFFER[0] = address & 0xFF
155171
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=1)
156172
return self._BUFFER[0]
157173

158-
def _read_u16(self, address):
174+
def _read_u16(self, address: int) -> int:
159175
with self._device as i2c:
160176
self._BUFFER[0] = address & 0xFF
161177
i2c.write_then_readinto(self._BUFFER, self._BUFFER, out_end=1, in_end=2)
162178
return self._BUFFER[0] << 8 | self._BUFFER[1]
163179

164-
def _write_u8(self, address, val):
180+
def _write_u8(self, address: int, val: int) -> None:
165181
with self._device as i2c:
166182
self._BUFFER[0] = address & 0xFF
167183
self._BUFFER[1] = val & 0xFF
168184
i2c.write(self._BUFFER, end=2)
169185

170-
def _write_u16(self, address, val):
186+
def _write_u16(self, address: int, val: int) -> None:
171187
with self._device as i2c:
172188
self._BUFFER[0] = address & 0xFF
173189
self._BUFFER[1] = (val >> 8) & 0xFF
174190
self._BUFFER[2] = val & 0xFF
175191
i2c.write(self._BUFFER, end=3)
176192

177193
@staticmethod
178-
def _read_bytes(device, address, count, buf):
194+
def _read_bytes(device, address: int, count: int, buf: bytearray) -> None:
179195
with device as i2c:
180196
buf[0] = address & 0xFF
181197
i2c.write_then_readinto(buf, buf, out_end=1, in_end=count)

0 commit comments

Comments
 (0)