36
36
from adafruit_register .i2c_bits import RWBits
37
37
from adafruit_register .i2c_bit import RWBit , ROBit
38
38
39
+ try :
40
+ from typing import Iterable , Optional , Tuple , Type
41
+ from busio import I2C
42
+ except ImportError :
43
+ pass
44
+
39
45
__version__ = "0.0.0+auto.0"
40
46
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LTR390.git"
41
47
@@ -65,7 +71,11 @@ def __init__(self, register_address, struct_format, bitwidth, length):
65
71
self ._width = bitwidth
66
72
self ._num_bytes = length
67
73
68
- def __get__ (self , obj , objtype = None ):
74
+ def __get__ (
75
+ self ,
76
+ obj : Optional ["LTR390" ],
77
+ objtype : Optional [Type ["LTR390" ]] = None ,
78
+ ) -> int :
69
79
# read bytes into buffer at correct alignment
70
80
raw_value = unpack_from (self .format , self .buffer , offset = 1 )[0 ]
71
81
@@ -81,7 +91,7 @@ def __get__(self, obj, objtype=None):
81
91
raw_value = unpack_from (self .format , self .buffer , offset = 1 )[0 ]
82
92
return raw_value >> 8
83
93
84
- def __set__ (self , obj , value ) :
94
+ def __set__ (self , obj : Optional [ "LTR390" ] , value : int ) -> None :
85
95
pack_into (self .format , self .buffer , 1 , value )
86
96
with obj .i2c_device as i2c :
87
97
i2c .write (self .buffer )
@@ -91,7 +101,12 @@ class CV:
91
101
"""struct helper"""
92
102
93
103
@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 :
95
110
"""Add CV values to the class"""
96
111
cls .string = {}
97
112
cls .lsb = {}
@@ -107,7 +122,7 @@ def add_values(cls, value_tuples):
107
122
cls .integration [value ] = integration
108
123
109
124
@classmethod
110
- def is_valid (cls , value ) :
125
+ def is_valid (cls , value : int ) -> bool :
111
126
"""Validate that a given value is a member"""
112
127
return value in cls .string
113
128
@@ -276,7 +291,7 @@ class LTR390: # pylint:disable=too-many-instance-attributes
276
291
277
292
Once read, this property will be False until it is updated in the next measurement cycle"""
278
293
279
- def __init__ (self , i2c , address = _DEFAULT_I2C_ADDR ):
294
+ def __init__ (self , i2c : I2C , address : int = _DEFAULT_I2C_ADDR ) -> None :
280
295
self .i2c_device = i2c_device .I2CDevice (i2c , address )
281
296
if self ._id_reg != 0xB2 :
282
297
@@ -285,7 +300,7 @@ def __init__(self, i2c, address=_DEFAULT_I2C_ADDR):
285
300
self ._mode_cache = None
286
301
self .initialize ()
287
302
288
- def initialize (self ):
303
+ def initialize (self ) -> None :
289
304
"""Reset the sensor to it's initial unconfigured state and configure it with sensible
290
305
defaults so it can be used"""
291
306
@@ -303,7 +318,7 @@ def initialize(self):
303
318
# self.high_threshold = 1000
304
319
# ltr.configInterrupt(true, LTR390_MODE_UVS);
305
320
306
- def _reset (self ):
321
+ def _reset (self ) -> None :
307
322
# The LTR390 software reset is ill behaved and can leave I2C bus in bad state.
308
323
# Instead, just manually set register reset values per datasheet.
309
324
with self .i2c_device as i2c :
@@ -316,11 +331,11 @@ def _reset(self):
316
331
i2c .write (bytes ((_THRESH_LOW , 0x00 , 0x00 , 0x00 )))
317
332
318
333
@property
319
- def _mode (self ):
334
+ def _mode (self ) -> bool :
320
335
return self ._mode_bit
321
336
322
337
@_mode .setter
323
- def _mode (self , value ) :
338
+ def _mode (self , value : bool ) -> None :
324
339
if not value in [ALS , UV ]:
325
340
raise AttributeError ("Mode must be ALS or UV" )
326
341
if self ._mode_cache != value :
@@ -330,44 +345,44 @@ def _mode(self, value):
330
345
331
346
# something is wrong here; I had to add a sleep to the loop to get both to update correctly
332
347
@property
333
- def uvs (self ):
348
+ def uvs (self ) -> int :
334
349
"""The calculated UV value"""
335
350
self ._mode = UV
336
351
while not self .data_ready :
337
352
sleep (0.010 )
338
353
return self ._uvs_data_reg
339
354
340
355
@property
341
- def light (self ):
356
+ def light (self ) -> int :
342
357
"""The currently measured ambient light level"""
343
358
self ._mode = ALS
344
359
while not self .data_ready :
345
360
sleep (0.010 )
346
361
return self ._als_data_reg
347
362
348
363
@property
349
- def gain (self ):
364
+ def gain (self ) -> int :
350
365
"""The amount of gain the raw measurements are multiplied by"""
351
366
return self ._gain_bits
352
367
353
368
@gain .setter
354
- def gain (self , value ):
369
+ def gain (self , value : int ):
355
370
if not Gain .is_valid (value ):
356
371
raise AttributeError ("gain must be a Gain" )
357
372
self ._gain_bits = value
358
373
359
374
@property
360
- def resolution (self ):
375
+ def resolution (self ) -> int :
361
376
"""Set the precision of the internal ADC used to read the light measurements"""
362
377
return self ._resolution_bits
363
378
364
379
@resolution .setter
365
- def resolution (self , value ):
380
+ def resolution (self , value : int ):
366
381
if not Resolution .is_valid (value ):
367
382
raise AttributeError ("resolution must be a Resolution" )
368
383
self ._resolution_bits = value
369
384
370
- def enable_alerts (self , enable , source , persistance ) :
385
+ def enable_alerts (self , enable : bool , source : bool , persistance : int ) -> None :
371
386
"""The configuration of alerts raised by the sensor
372
387
373
388
:param enable: Whether the interrupt output is enabled
@@ -386,19 +401,19 @@ def enable_alerts(self, enable, source, persistance):
386
401
self ._int_persistance_bits = persistance
387
402
388
403
@property
389
- def measurement_delay (self ):
404
+ def measurement_delay (self ) -> int :
390
405
"""The delay between measurements. This can be used to set the measurement rate which
391
406
affects the sensor power usage."""
392
407
return self ._measurement_delay_bits
393
408
394
409
@measurement_delay .setter
395
- def measurement_delay (self , value ) :
410
+ def measurement_delay (self , value : int ) -> None :
396
411
if not MeasurementDelay .is_valid (value ):
397
412
raise AttributeError ("measurement_delay must be a MeasurementDelay" )
398
413
self ._measurement_delay_bits = value
399
414
400
415
@property
401
- def uvi (self ):
416
+ def uvi (self ) -> float :
402
417
"""Read UV count and return calculated UV Index (UVI) value based upon the rated sensitivity
403
418
of 1 UVI per 2300 counts at 18X gain factor and 20-bit resolution."""
404
419
return (
@@ -413,22 +428,22 @@ def uvi(self):
413
428
)
414
429
415
430
@property
416
- def lux (self ):
431
+ def lux (self ) -> float :
417
432
"""Read light level and return calculated Lux value."""
418
433
return (
419
434
(self .light * 0.6 )
420
435
/ (Gain .factor [self .gain ] * Resolution .integration [self .resolution ])
421
436
) * self ._window_factor
422
437
423
438
@property
424
- def window_factor (self ):
439
+ def window_factor (self ) -> float :
425
440
"""Window transmission factor (Wfac) for UVI and Lux calculations.
426
441
A factor of 1 (default) represents no window or clear glass; > 1 for a tinted window.
427
442
Factor of > 1 requires an empirical calibration with a reference light source."""
428
443
return self ._window_factor
429
444
430
445
@window_factor .setter
431
- def window_factor (self , factor = 1 ) :
446
+ def window_factor (self , factor : float = 1 ) -> None :
432
447
if factor < 1 :
433
448
raise ValueError (
434
449
"window transmission factor must be a value of 1.0 or greater"
0 commit comments