Skip to content

Commit 0517d45

Browse files
More typing changes
1 parent 3a75a51 commit 0517d45

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

adafruit_as7341.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,38 @@
4444
from adafruit_register.i2c_bits import ROBits, RWBits
4545

4646
try:
47+
from typing import Tuple, Optional, Any, Callable, TypeVar
48+
4749
# Only needed for typing
4850
import busio # pylint: disable=unused-import
49-
from typing import Tuple, Optional
51+
52+
TCallable = TypeVar('TCallable', bound=Callable[..., Any])
53+
5054
except ImportError:
5155
pass
5256

53-
54-
_AS7341_DEVICE_ID: int = const(0b001001) # Correct content of WHO_AM_I register
57+
# Correct content of WHO_AM_I register
58+
_AS7341_DEVICE_ID: int = const(0b001001)
5559
_AS7341_I2CADDR_DEFAULT: int = const(0x39) # AS7341 default i2c address
5660
_AS7341_CHIP_ID: int = const(0x09) # AS7341 default device id from WHOAMI
5761
_AS7341_WHOAMI: int = const(0x92) # Chip ID register
58-
_AS7341_CONFIG: int = const(0x70) # Enables LED control and sets light sensing mode
62+
# Enables LED control and sets light sensing mode
63+
_AS7341_CONFIG: int = const(0x70)
5964
_AS7341_GPIO: int = const(0x73) # Connects photo diode to GPIO or INT pins
6065
_AS7341_LED: int = const(0x74) # LED Register; Enables and sets current limit
6166
_AS7341_ENABLE: int = const(
6267
0x80
6368
) # Main enable register. Controls SMUX, Flicker Detection,Spectral and
6469
# Power
6570
_AS7341_ATIME: int = const(0x81) # Sets ADC integration step count
66-
_AS7341_SP_LOW_TH_L: int = const(0x84) # Spectral measurement Low Threshold low byte
67-
_AS7341_SP_LOW_TH_H: int = const(0x85) # 0 Spectral measurement Low Threshold high byte
68-
_AS7341_SP_HIGH_TH_L: int = const(0x86) # Spectral measurement High Threshold low byte
69-
_AS7341_SP_HIGH_TH_H: int = const(0x87) # Spectral measurement High Threshold low byte
71+
# Spectral measurement Low Threshold low byte
72+
_AS7341_SP_LOW_TH_L: int = const(0x84)
73+
# 0 Spectral measurement Low Threshold high byte
74+
_AS7341_SP_LOW_TH_H: int = const(0x85)
75+
# Spectral measurement High Threshold low byte
76+
_AS7341_SP_HIGH_TH_L: int = const(0x86)
77+
# Spectral measurement High Threshold low byte
78+
_AS7341_SP_HIGH_TH_H: int = const(0x87)
7079
_AS7341_STATUS: int = const(
7180
0x93
7281
) # Interrupt status registers. Indicates the occourance of an interrupt
@@ -104,7 +113,9 @@
104113
) # GPIO Settings and status: polarity, direction, sets output, reads
105114
_AS7341_ASTEP_L: int = const(0xCA) # Integration step size ow byte
106115
_AS7341_ASTEP_H: int = const(0xCB) # Integration step size high byte
107-
_AS7341_FD_TIME1: int = const(0xD8) # Flicker detection integration time low byte
116+
_AS7341_FD_TIME1: int = const(
117+
0xD8
118+
) # Flicker detection integration time low byte
108119
_AS7341_FD_TIME2: int = const(0xDA) # Flicker detection gain and high nibble
109120
_AS7341_FD_STATUS: int = const(
110121
0xDB
@@ -114,9 +125,9 @@
114125
_AS7341_FD_CFG0: int = const(0xD7) # Enables FIFO for flicker detection
115126

116127

117-
def _low_bank(func) -> any:
128+
def _low_bank(func: TCallable) -> Callable[[Any], TCallable]:
118129
# pylint:disable=protected-access
119-
def _decorator(self, *args, **kwargs):
130+
def _decorator(self, *args, **kwargs) -> TCallable:
120131
self._low_bank_active = True
121132
retval = func(self, *args, **kwargs)
122133
self._low_bank_active = False
@@ -131,7 +142,7 @@ class CV:
131142
@classmethod
132143
def add_values(
133144
cls,
134-
value_tuples: Tuple[str, int, any, any],
145+
value_tuples: Tuple[str, int, int, Optional[float]],
135146
) -> None:
136147
"""Add CV values to the class"""
137148
cls.string = {}
@@ -309,8 +320,8 @@ class AS7341: # pylint:disable=too-many-instance-attributes, no-member
309320
"""
310321

311322
def __init__(
312-
self, i2c_bus: busio.I2C, address: Optional[int] = _AS7341_I2CADDR_DEFAULT
313-
):
323+
self, i2c_bus: busio.I2C, address: int = _AS7341_I2CADDR_DEFAULT
324+
) -> None:
314325

315326
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
316327
if not self._device_id in [_AS7341_DEVICE_ID]:
@@ -331,7 +342,7 @@ def initialize(self) -> None:
331342
self.gain = Gain.GAIN_128X # pylint:disable=no-member
332343

333344
@property
334-
def all_channels(self) -> Struct:
345+
def all_channels(self) -> Tuple[int, ...]:
335346
"""The current readings for all six ADC channels"""
336347

337348
self._configure_f1_f4()
@@ -345,66 +356,66 @@ def all_channels(self) -> Struct:
345356
return reads
346357

347358
@property
348-
def channel_415nm(self) -> UnaryStruct:
359+
def channel_415nm(self) -> int:
349360
"""The current reading for the 415nm band"""
350361
self._configure_f1_f4()
351362
return self._channel_0_data
352363

353364
@property
354-
def channel_445nm(self) -> UnaryStruct:
365+
def channel_445nm(self) -> int:
355366
"""The current reading for the 445nm band"""
356367
self._configure_f1_f4()
357368
return self._channel_1_data
358369

359370
@property
360-
def channel_480nm(self) -> UnaryStruct:
371+
def channel_480nm(self) -> int:
361372
"""The current reading for the 480nm band"""
362373
self._configure_f1_f4()
363374
return self._channel_2_data
364375

365376
@property
366-
def channel_515nm(self) -> UnaryStruct:
377+
def channel_515nm(self) -> int:
367378
"""The current reading for the 515nm band"""
368379
self._configure_f1_f4()
369380
return self._channel_3_data
370381

371382
@property
372-
def channel_555nm(self) -> UnaryStruct:
383+
def channel_555nm(self) -> int:
373384
"""The current reading for the 555nm band"""
374385
self._configure_f5_f8()
375386
return self._channel_0_data
376387

377388
@property
378-
def channel_590nm(self) -> UnaryStruct:
389+
def channel_590nm(self) -> int:
379390
"""The current reading for the 590nm band"""
380391
self._configure_f5_f8()
381392
return self._channel_1_data
382393

383394
@property
384-
def channel_630nm(self) -> UnaryStruct:
395+
def channel_630nm(self) -> int:
385396
"""The current reading for the 630nm band"""
386397
self._configure_f5_f8()
387398
return self._channel_2_data
388399

389400
@property
390-
def channel_680nm(self) -> UnaryStruct:
401+
def channel_680nm(self) -> int:
391402
"""The current reading for the 680nm band"""
392403
self._configure_f5_f8()
393404
return self._channel_3_data
394405

395406
@property
396-
def channel_clear(self) -> UnaryStruct:
407+
def channel_clear(self) -> int:
397408
"""The current reading for the clear sensor"""
398409
self._configure_f5_f8()
399410
return self._channel_4_data
400411

401412
@property
402-
def channel_nir(self) -> UnaryStruct:
413+
def channel_nir(self) -> int:
403414
"""The current reading for the NIR (near-IR) sensor"""
404415
self._configure_f5_f8()
405416
return self._channel_5_data
406417

407-
def _wait_for_data(self, timeout: int = 1.0) -> None:
418+
def _wait_for_data(self, timeout: Optional[int] = 1.0) -> None:
408419
"""Wait for sensor data to be ready"""
409420
start = monotonic()
410421
while not self._data_ready_bit:

0 commit comments

Comments
 (0)