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