Skip to content

Commit 667a918

Browse files
authored
Merge pull request #20 from tammymakesthings/issue18-type-annotations
Issue18 type annotations
2 parents a29a790 + 75c0742 commit 667a918

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

adafruit_adt7410.py

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,29 @@
4040
from adafruit_register.i2c_bit import RWBit, ROBit
4141
from micropython import const
4242

43+
try:
44+
# Used only for typing
45+
import busio # pylint: disable=unused-import
46+
except ImportError:
47+
pass
48+
4349
__version__ = "0.0.0-auto.0"
4450
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADT7410.git"
4551

4652

47-
_ADT7410_TEMPMSB = const(0x0)
48-
_ADT7410_TEMPLSB = const(0x1)
49-
_ADT7410_STATUS = const(0x2)
50-
_ADT7410_CONFIG = const(0x3)
51-
_ADT7410_THIGHMSB = const(0x4)
52-
_ADT7410_THIGHLSB = const(0x5)
53-
_ADT7410_TLOWMSB = const(0x6)
54-
_ADT7410_TLOWLSB = const(0x7)
55-
_ADT7410_TCRITMSB = const(0x8)
56-
_ADT7410_TCRITLSB = const(0x9)
57-
_ADT7410_THYST = const(0x0A)
58-
_ADT7410_ID = const(0xB)
59-
_ADT7410_SWRST = const(0x2F)
53+
_ADT7410_TEMPMSB: int = const(0x0)
54+
_ADT7410_TEMPLSB: int = const(0x1)
55+
_ADT7410_STATUS: int = const(0x2)
56+
_ADT7410_CONFIG: int = const(0x3)
57+
_ADT7410_THIGHMSB: int = const(0x4)
58+
_ADT7410_THIGHLSB: int = const(0x5)
59+
_ADT7410_TLOWMSB: int = const(0x6)
60+
_ADT7410_TLOWLSB: int = const(0x7)
61+
_ADT7410_TCRITMSB: int = const(0x8)
62+
_ADT7410_TCRITLSB: int = const(0x9)
63+
_ADT7410_THYST: int = const(0x0A)
64+
_ADT7410_ID: int = const(0xB)
65+
_ADT7410_SWRST: int = const(0x2F)
6066

6167

6268
class ADT7410:
@@ -91,17 +97,18 @@ class ADT7410:
9197
"""
9298

9399
# many modes can be set with register objects for simplicity
94-
ready = ROBit(_ADT7410_STATUS, 7)
95-
ctpin_polarity = RWBit(_ADT7410_CONFIG, 2)
96-
intpin_polarity = RWBit(_ADT7410_CONFIG, 3)
97-
comparator_mode = RWBit(_ADT7410_CONFIG, 4)
98-
high_resolution = RWBit(_ADT7410_CONFIG, 7)
100+
ready: ROBit = ROBit(_ADT7410_STATUS, 7)
101+
ctpin_polarity: RWBit = RWBit(_ADT7410_CONFIG, 2)
102+
intpin_polarity: RWBit = RWBit(_ADT7410_CONFIG, 3)
103+
comparator_mode: RWBit = RWBit(_ADT7410_CONFIG, 4)
104+
high_resolution: RWBit = RWBit(_ADT7410_CONFIG, 7)
105+
99106
# Status Information configuration
100-
temp_over_critiq = ROBit(_ADT7410_STATUS, 6)
101-
temp_over_high = ROBit(_ADT7410_STATUS, 5)
102-
temp_under_low = ROBit(_ADT7410_STATUS, 4)
107+
temp_over_critiq: ROBit = ROBit(_ADT7410_STATUS, 6)
108+
temp_over_high: ROBit = ROBit(_ADT7410_STATUS, 5)
109+
temp_under_low: ROBit = ROBit(_ADT7410_STATUS, 4)
103110

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

116123
@property
117-
def temperature(self):
124+
def temperature(self) -> float:
118125
"""The temperature in Celsius"""
119126
temp = self._read_register(_ADT7410_TEMPMSB, 2)
120127
return struct.unpack(">h", temp)[0] / 128
121128

122129
@property
123-
def status(self):
130+
def status(self) -> int:
124131
"""The ADT7410 status registers current value"""
125132
return self._read_register(_ADT7410_STATUS)[0]
126133

127134
@property
128-
def configuration(self):
135+
def configuration(self) -> int:
129136
"""The ADT7410 configuration register"""
130137
return self._read_register(_ADT7410_CONFIG)[0]
131138

132139
@configuration.setter
133-
def configuration(self, val):
140+
def configuration(self, val: int) -> None:
134141
self._write_register(_ADT7410_CONFIG, val)
135142

136-
def reset(self):
143+
def reset(self) -> None:
137144
"""Perform a software reset"""
138145
self._write_register(_ADT7410_SWRST)
139146
time.sleep(0.5)
140147

141-
def _read_register(self, addr, num=1):
148+
def _read_register(self, addr: int, num: int = 1) -> int:
142149
self._buf[0] = addr
143150
with self.i2c_device as i2c:
144151
i2c.write_then_readinto(
145152
self._buf, self._buf, out_end=1, in_start=1, in_end=num + 1
146153
)
147154
return self._buf[1 : num + 1]
148155

149-
def _write_register(self, addr, data=None):
156+
def _write_register(self, addr: int, data: int = None) -> None:
150157
self._buf[0] = addr
151158
end = 1
152159
if data:
@@ -156,39 +163,39 @@ def _write_register(self, addr, data=None):
156163
i2c.write(self._buf, end=end)
157164

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

164171
@high_temperature.setter
165-
def high_temperature(self, value):
172+
def high_temperature(self, value: int) -> None:
166173
value = struct.pack(">h", int(value * 128))
167174
self._write_register(_ADT7410_THIGHMSB, value[0])
168175
self._write_register(_ADT7410_THIGHLSB, value[1])
169176

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

177184
@low_temperature.setter
178-
def low_temperature(self, value):
185+
def low_temperature(self, value: int) -> None:
179186
value = struct.pack(">h", int(value * 128))
180187
self._write_register(_ADT7410_TLOWMSB, value[0])
181188
self._write_register(_ADT7410_TLOWLSB, value[1])
182189

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

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

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

209216
@hysteresis.setter
210-
def hysteresis(self, value):
217+
def hysteresis(self, value: int) -> None:
211218
if value > 15 or isinstance(value, float):
212-
raise Exception("Hysteresis value must be an integer lower than 15 Celsius")
219+
raise ValueError(
220+
"Hysteresis value must be an integer lower than 15 Celsius"
221+
)
213222

214223
self._write_register(_ADT7410_THYST, value)

0 commit comments

Comments
 (0)