Skip to content

Commit 3f2ad26

Browse files
authored
Merge pull request #14 from tcfranks/main
Add Missing Type Annotations
2 parents b6fcd49 + 3792f83 commit 3f2ad26

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

adafruit_ltr390.py

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
from adafruit_register.i2c_bits import RWBits
3737
from adafruit_register.i2c_bit import RWBit, ROBit
3838

39+
try:
40+
from typing import Iterable, Optional, Tuple, Type
41+
from busio import I2C
42+
except ImportError:
43+
pass
44+
3945
__version__ = "0.0.0+auto.0"
4046
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LTR390.git"
4147

@@ -65,7 +71,11 @@ def __init__(self, register_address, struct_format, bitwidth, length):
6571
self._width = bitwidth
6672
self._num_bytes = length
6773

68-
def __get__(self, obj, objtype=None):
74+
def __get__(
75+
self,
76+
obj: Optional["LTR390"],
77+
objtype: Optional[Type["LTR390"]] = None,
78+
) -> int:
6979
# read bytes into buffer at correct alignment
7080
raw_value = unpack_from(self.format, self.buffer, offset=1)[0]
7181

@@ -81,7 +91,7 @@ def __get__(self, obj, objtype=None):
8191
raw_value = unpack_from(self.format, self.buffer, offset=1)[0]
8292
return raw_value >> 8
8393

84-
def __set__(self, obj, value):
94+
def __set__(self, obj: Optional["LTR390"], value: int) -> None:
8595
pack_into(self.format, self.buffer, 1, value)
8696
with obj.i2c_device as i2c:
8797
i2c.write(self.buffer)
@@ -91,7 +101,12 @@ class CV:
91101
"""struct helper"""
92102

93103
@classmethod
94-
def add_values(cls, value_tuples):
104+
def add_values(
105+
cls,
106+
value_tuples: Iterable[
107+
Tuple[str, int, str, Optional[float], int, Optional[float]]
108+
],
109+
) -> None:
95110
"""Add CV values to the class"""
96111
cls.string = {}
97112
cls.lsb = {}
@@ -107,7 +122,7 @@ def add_values(cls, value_tuples):
107122
cls.integration[value] = integration
108123

109124
@classmethod
110-
def is_valid(cls, value):
125+
def is_valid(cls, value: int) -> bool:
111126
"""Validate that a given value is a member"""
112127
return value in cls.string
113128

@@ -276,7 +291,7 @@ class LTR390: # pylint:disable=too-many-instance-attributes
276291
277292
Once read, this property will be False until it is updated in the next measurement cycle"""
278293

279-
def __init__(self, i2c, address=_DEFAULT_I2C_ADDR):
294+
def __init__(self, i2c: I2C, address: int = _DEFAULT_I2C_ADDR) -> None:
280295
self.i2c_device = i2c_device.I2CDevice(i2c, address)
281296
if self._id_reg != 0xB2:
282297

@@ -285,7 +300,7 @@ def __init__(self, i2c, address=_DEFAULT_I2C_ADDR):
285300
self._mode_cache = None
286301
self.initialize()
287302

288-
def initialize(self):
303+
def initialize(self) -> None:
289304
"""Reset the sensor to it's initial unconfigured state and configure it with sensible
290305
defaults so it can be used"""
291306

@@ -303,7 +318,7 @@ def initialize(self):
303318
# self.high_threshold = 1000
304319
# ltr.configInterrupt(true, LTR390_MODE_UVS);
305320

306-
def _reset(self):
321+
def _reset(self) -> None:
307322
# The LTR390 software reset is ill behaved and can leave I2C bus in bad state.
308323
# Instead, just manually set register reset values per datasheet.
309324
with self.i2c_device as i2c:
@@ -316,11 +331,11 @@ def _reset(self):
316331
i2c.write(bytes((_THRESH_LOW, 0x00, 0x00, 0x00)))
317332

318333
@property
319-
def _mode(self):
334+
def _mode(self) -> bool:
320335
return self._mode_bit
321336

322337
@_mode.setter
323-
def _mode(self, value):
338+
def _mode(self, value: bool) -> None:
324339
if not value in [ALS, UV]:
325340
raise AttributeError("Mode must be ALS or UV")
326341
if self._mode_cache != value:
@@ -330,44 +345,44 @@ def _mode(self, value):
330345

331346
# something is wrong here; I had to add a sleep to the loop to get both to update correctly
332347
@property
333-
def uvs(self):
348+
def uvs(self) -> int:
334349
"""The calculated UV value"""
335350
self._mode = UV
336351
while not self.data_ready:
337352
sleep(0.010)
338353
return self._uvs_data_reg
339354

340355
@property
341-
def light(self):
356+
def light(self) -> int:
342357
"""The currently measured ambient light level"""
343358
self._mode = ALS
344359
while not self.data_ready:
345360
sleep(0.010)
346361
return self._als_data_reg
347362

348363
@property
349-
def gain(self):
364+
def gain(self) -> int:
350365
"""The amount of gain the raw measurements are multiplied by"""
351366
return self._gain_bits
352367

353368
@gain.setter
354-
def gain(self, value):
369+
def gain(self, value: int):
355370
if not Gain.is_valid(value):
356371
raise AttributeError("gain must be a Gain")
357372
self._gain_bits = value
358373

359374
@property
360-
def resolution(self):
375+
def resolution(self) -> int:
361376
"""Set the precision of the internal ADC used to read the light measurements"""
362377
return self._resolution_bits
363378

364379
@resolution.setter
365-
def resolution(self, value):
380+
def resolution(self, value: int):
366381
if not Resolution.is_valid(value):
367382
raise AttributeError("resolution must be a Resolution")
368383
self._resolution_bits = value
369384

370-
def enable_alerts(self, enable, source, persistance):
385+
def enable_alerts(self, enable: bool, source: bool, persistance: int) -> None:
371386
"""The configuration of alerts raised by the sensor
372387
373388
:param enable: Whether the interrupt output is enabled
@@ -386,19 +401,19 @@ def enable_alerts(self, enable, source, persistance):
386401
self._int_persistance_bits = persistance
387402

388403
@property
389-
def measurement_delay(self):
404+
def measurement_delay(self) -> int:
390405
"""The delay between measurements. This can be used to set the measurement rate which
391406
affects the sensor power usage."""
392407
return self._measurement_delay_bits
393408

394409
@measurement_delay.setter
395-
def measurement_delay(self, value):
410+
def measurement_delay(self, value: int) -> None:
396411
if not MeasurementDelay.is_valid(value):
397412
raise AttributeError("measurement_delay must be a MeasurementDelay")
398413
self._measurement_delay_bits = value
399414

400415
@property
401-
def uvi(self):
416+
def uvi(self) -> float:
402417
"""Read UV count and return calculated UV Index (UVI) value based upon the rated sensitivity
403418
of 1 UVI per 2300 counts at 18X gain factor and 20-bit resolution."""
404419
return (
@@ -413,22 +428,22 @@ def uvi(self):
413428
)
414429

415430
@property
416-
def lux(self):
431+
def lux(self) -> float:
417432
"""Read light level and return calculated Lux value."""
418433
return (
419434
(self.light * 0.6)
420435
/ (Gain.factor[self.gain] * Resolution.integration[self.resolution])
421436
) * self._window_factor
422437

423438
@property
424-
def window_factor(self):
439+
def window_factor(self) -> float:
425440
"""Window transmission factor (Wfac) for UVI and Lux calculations.
426441
A factor of 1 (default) represents no window or clear glass; > 1 for a tinted window.
427442
Factor of > 1 requires an empirical calibration with a reference light source."""
428443
return self._window_factor
429444

430445
@window_factor.setter
431-
def window_factor(self, factor=1):
446+
def window_factor(self, factor: float = 1) -> None:
432447
if factor < 1:
433448
raise ValueError(
434449
"window transmission factor must be a value of 1.0 or greater"

0 commit comments

Comments
 (0)