Skip to content

Commit 187acbc

Browse files
authored
Merge pull request #29 from tcfranks/main
Add Missing Type Annotations - Partial
2 parents b802be7 + 81f5444 commit 187acbc

File tree

5 files changed

+81
-58
lines changed

5 files changed

+81
-58
lines changed

adafruit_emc2101/__init__.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939

4040
from adafruit_emc2101 import emc2101_regs
4141

42+
try:
43+
from typing import Tuple
44+
from busio import I2C
45+
except ImportError:
46+
pass
47+
4248
__version__ = "0.0.0+auto.0"
4349
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_EMC2101.git"
4450

@@ -175,7 +181,7 @@ class to add those features, at the cost of increased memory usage.
175181
"""Set whether spin-up is aborted if measured speed is lower than the limit.
176182
Ignored unless _tach_mode_enable is 1."""
177183

178-
def __init__(self, i2c_bus):
184+
def __init__(self, i2c_bus: I2C) -> None:
179185
# These devices don't ship with any other address.
180186
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, emc2101_regs.I2C_ADDR)
181187
part = self._part_id
@@ -186,27 +192,27 @@ def __init__(self, i2c_bus):
186192
not part in [emc2101_regs.PART_ID_EMC2101, emc2101_regs.PART_ID_EMC2101R]
187193
or mfg != emc2101_regs.MFG_ID_SMSC
188194
):
189-
raise RuntimeError("No EMC2101 (part={}.{})".format(part, mfg))
195+
raise RuntimeError(f"No EMC2101 (part={part}.{mfg})")
190196

191197
self._full_speed_lsb = None # See _calculate_full_speed().
192198
self.initialize()
193199

194-
def initialize(self):
200+
def initialize(self) -> None:
195201
"""Reset the controller to an initial default configuration"""
196202
self._tach_mode_enable = True
197203
self._enabled_forced_temp = False
198204
self._spin_tach_limit = False
199205
self._calculate_full_speed()
200206

201207
@property
202-
def part_info(self):
208+
def part_info(self) -> Tuple[int, int, int]:
203209
"""The part information: manufacturer, part id and revision.
204210
Normally returns (0x5d, 0x16, 0x1).
205211
"""
206212
return (self._mfg_id, self._part_id, self._part_rev)
207213

208214
@property
209-
def devconfig(self):
215+
def devconfig(self) -> int:
210216
"""Read the main device config register.
211217
See the CONFIG_* bit definitions in the emc2101_regs module, or refer
212218
to the datasheet for more detail. Note: this is not the Fan Config
@@ -215,15 +221,15 @@ def devconfig(self):
215221
return self._config
216222

217223
@property
218-
def devstatus(self):
224+
def devstatus(self) -> int:
219225
"""Read device status (alerts) register. See the STATUS_* bit
220226
definitions in the emc2101_regs module, or refer to the datasheet for
221227
more detail.
222228
"""
223229
return self._status
224230

225231
@property
226-
def internal_temperature(self):
232+
def internal_temperature(self) -> int:
227233
"""The temperature as measured by the EMC2101's internal 8-bit
228234
temperature sensor, which validly ranges from 0 to 85 and does not
229235
support fractions (unlike the external readings).
@@ -233,7 +239,7 @@ def internal_temperature(self):
233239
return self._int_temp
234240

235241
@property
236-
def external_temperature(self):
242+
def external_temperature(self) -> float:
237243
"""The temperature measured using the external diode. The value is
238244
read as a fixed-point 11-bit value ranging from -64 to approx 126,
239245
with fractional part of 1/8 degree.
@@ -259,7 +265,7 @@ def external_temperature(self):
259265
return full_tmp
260266

261267
@property
262-
def fan_speed(self):
268+
def fan_speed(self) -> float:
263269
"""The current speed in Revolutions per Minute (RPM).
264270
265271
:return: float fan speed rounded to 2dp.
@@ -270,7 +276,7 @@ def fan_speed(self):
270276
raise OSError("Connection")
271277
return round(emc2101_regs.FAN_RPM_DIVISOR / val, 2)
272278

273-
def _calculate_full_speed(self, pwm_f=None, dac=None):
279+
def _calculate_full_speed(self, pwm_f: int = None, dac: int = None) -> None:
274280
"""Determine the LSB value for a 100% fan setting"""
275281
if dac is None:
276282
dac = self.dac_output_enabled
@@ -287,12 +293,12 @@ def _calculate_full_speed(self, pwm_f=None, dac=None):
287293
# PWM_F=0 behaves like PWM_F=1.
288294
self._full_speed_lsb = 2.0 * max(1, pwm_f)
289295

290-
def _speed_to_lsb(self, percentage):
296+
def _speed_to_lsb(self, percentage: int) -> int:
291297
"""Convert a fan speed percentage to a Fan Setting byte value"""
292298
return round((percentage / 100.0) * self._full_speed_lsb)
293299

294300
@property
295-
def manual_fan_speed(self):
301+
def manual_fan_speed(self) -> float:
296302
"""The fan speed used while the LUT is being updated and is unavailable. The
297303
speed is given as the fan's PWM duty cycle represented as a float percentage.
298304
The value roughly approximates the percentage of the fan's maximum speed.
@@ -304,7 +310,7 @@ def manual_fan_speed(self):
304310
return (raw_setting / fan_speed) * 100.0
305311

306312
@manual_fan_speed.setter
307-
def manual_fan_speed(self, fan_speed):
313+
def manual_fan_speed(self, fan_speed: float) -> None:
308314
"""The fan speed used while the LUT is being updated and is unavailable. The
309315
speed is given as the fan's PWM duty cycle represented as a float percentage.
310316
The value roughly approximates the percentage of the fan's maximum speed.
@@ -324,13 +330,13 @@ def manual_fan_speed(self, fan_speed):
324330
self._fan_lut_prog = lut_disabled
325331

326332
@property
327-
def dac_output_enabled(self):
333+
def dac_output_enabled(self) -> int:
328334
"""When set, the fan control signal is output as a DC voltage instead
329335
of a PWM signal."""
330336
return self._dac_output_enabled
331337

332338
@dac_output_enabled.setter
333-
def dac_output_enabled(self, value):
339+
def dac_output_enabled(self, value: int) -> None:
334340
"""When set, the fan control signal is output as a DC voltage instead of
335341
a PWM signal. Be aware that the DAC output very likely requires different
336342
hardware to the PWM output. See datasheet and examples for info.
@@ -339,7 +345,7 @@ def dac_output_enabled(self, value):
339345
self._calculate_full_speed(dac=value)
340346

341347
@property
342-
def lut_enabled(self):
348+
def lut_enabled(self) -> bool:
343349
"""Enable or disable the internal look up table used to map a given
344350
temperature to a fan speed.
345351
@@ -350,7 +356,7 @@ def lut_enabled(self):
350356
return not self._fan_lut_prog
351357

352358
@property
353-
def tach_limit(self):
359+
def tach_limit(self) -> float:
354360
"""The maximum speed expected for the fan. If the fan exceeds this
355361
speed, the status register TACH bit will be set.
356362
@@ -365,7 +371,7 @@ def tach_limit(self):
365371
return round(emc2101_regs.FAN_RPM_DIVISOR / limit, 2)
366372

367373
@tach_limit.setter
368-
def tach_limit(self, new_limit):
374+
def tach_limit(self, new_limit: float) -> None:
369375
"""Set the speed limiter on the fan PWM signal. The value of
370376
15000 is arbitrary, but very few fans run faster than this. If the
371377
fan exceeds this speed, the status register TACH bit will be set.
@@ -384,7 +390,7 @@ def tach_limit(self, new_limit):
384390
self._tach_limit_msb = (num >> 8) & 0xFF
385391

386392
@property
387-
def spinup_time(self):
393+
def spinup_time(self) -> int:
388394
"""The amount of time the fan will spin at the currently set drive
389395
strength.
390396
@@ -393,7 +399,7 @@ def spinup_time(self):
393399
return self._spin_time
394400

395401
@spinup_time.setter
396-
def spinup_time(self, spin_time):
402+
def spinup_time(self, spin_time: int) -> None:
397403
"""Set the time that the SpinupDrive value will be used to get the
398404
fan moving before the normal speed controls are activated. This is
399405
needed because fan motors typically need a 'kick' to get them moving,
@@ -417,7 +423,7 @@ def spinup_time(self, spin_time):
417423
self._spin_time = spin_time
418424

419425
@property
420-
def spinup_drive(self):
426+
def spinup_drive(self) -> int:
421427
"""The drive strength of the fan on spinup in % max PWM duty cycle
422428
(which approximates to max fan speed).
423429
@@ -426,7 +432,7 @@ def spinup_drive(self):
426432
return self._spin_drive
427433

428434
@spinup_drive.setter
429-
def spinup_drive(self, spin_drive):
435+
def spinup_drive(self, spin_drive: int) -> None:
430436
"""Set the drive (pwm duty percentage) that the SpinupTime value is applied
431437
to move the fan before the normal speed controls are activated. This is needed
432438
because fan motors typically need a 'kick' to get them moving, but after this
@@ -450,16 +456,16 @@ def spinup_drive(self, spin_drive):
450456
self._spin_drive = spin_drive
451457

452458
@property
453-
def conversion_rate(self):
459+
def conversion_rate(self) -> int:
454460
"""The rate at which temperature measurements are taken.
455461
456462
:return int: corresponding to the ConversionRate enumeration."""
457463
return self._conversion_rate
458464

459465
@conversion_rate.setter
460-
def conversion_rate(self, rate):
466+
def conversion_rate(self, rate: int) -> None:
461467
"""Set the rate at which the external temperature is checked by
462-
by the device. Reducing this rate can reduce power consumption.
468+
the device. Reducing this rate can reduce power consumption.
463469
464470
Usage:
465471

adafruit_emc2101/emc2101_enums.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@
3232
3333
"""
3434

35+
try:
36+
from typing import Iterable, Optional, Tuple
37+
except ImportError:
38+
pass
39+
3540

3641
class CV:
3742
"""struct helper"""
3843

3944
@classmethod
40-
def add_values(cls, value_tuples):
45+
def add_values(
46+
cls, value_tuples: Iterable[Tuple[str, int, str, Optional[float]]]
47+
) -> None:
4148
"""Creates CV entries"""
4249
cls.string = {}
4350
cls.lsb = {}
@@ -48,7 +55,7 @@ def add_values(cls, value_tuples):
4855
cls.lsb[value] = lsb
4956

5057
@classmethod
51-
def is_valid(cls, value):
58+
def is_valid(cls, value: int) -> bool:
5259
"Returns true if the given value is a member of the CV"
5360
return value in cls.string
5461

0 commit comments

Comments
 (0)