Skip to content

Commit 76fb143

Browse files
author
Alec Delaney
committed
Added type annotations
1 parent f0b2a44 commit 76fb143

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

adafruit_ltr329_ltr303.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
from adafruit_register.i2c_bit import RWBit
3434
from adafruit_register.i2c_bits import RWBits
3535

36+
try:
37+
from typing import Tuple
38+
from busio import I2C
39+
except ImportError:
40+
pass
41+
3642
__version__ = "0.0.0+auto.0"
3743
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LTR329_LTR303.git"
3844

@@ -83,43 +89,43 @@ class LTR329:
8389
_als_data_gain_range = RWBits(3, _LTR329_REG_STATUS, 4)
8490
new_als_data_available = RWBit(_LTR329_REG_STATUS, 2)
8591

86-
def __init__(self, i2c, address=_LTR329_I2CADDR_DEFAULT):
92+
def __init__(self, i2c: I2C, address: int = _LTR329_I2CADDR_DEFAULT) -> None:
8793
self.i2c_device = i2c_device.I2CDevice(i2c, address)
8894
if self.part_id != 0xA0 or self.manufacturer_id != 0x05:
8995
raise RuntimeError("Unable to find LTR-329, check your wiring")
9096
self.reset()
9197
self.active_mode = True
9298

93-
def reset(self):
99+
def reset(self) -> None:
94100
"""Reset the sensor to the default state set by the library"""
95101
self._reset = True
96102
time.sleep(0.010)
97103

98104
@property
99-
def als_gain(self):
105+
def als_gain(self) -> int:
100106
"""ALS gain, can be: 1, 2, 4, 8, 48 or 96 times"""
101107
return _als_gains[self._als_gain]
102108

103109
@als_gain.setter
104-
def als_gain(self, gain):
110+
def als_gain(self, gain: int) -> None:
105111
if not gain in _als_gains:
106112
raise RuntimeError("Invalid gain: must be 1, 2, 4, 8, 48 or 96 x")
107113
self._als_gain = _als_gains.index(gain)
108114

109115
@property
110-
def als_data_gain(self):
116+
def als_data_gain(self) -> int:
111117
"""ALS gain for data that is being read now,
112118
can be: 1, 2, 4, 8, 48 or 96 times"""
113119
return _als_gains[self._als_data_gain_range]
114120

115121
@property
116-
def integration_time(self):
122+
def integration_time(self) -> int:
117123
"""ALS integration times, can be: 50, 100, 150, 200, 250,
118124
300, 350, or 400 millisec"""
119125
return _integration_times[self._integration_time]
120126

121127
@integration_time.setter
122-
def integration_time(self, int_time):
128+
def integration_time(self, int_time: int) -> None:
123129
if not int_time in _integration_times:
124130
raise RuntimeError(
125131
"Invalid integration time: must be 50, 100, 150, "
@@ -128,49 +134,49 @@ def integration_time(self, int_time):
128134
self._integration_time = _integration_times.index(int_time)
129135

130136
@property
131-
def measurement_rate(self):
137+
def measurement_rate(self) -> int:
132138
"""ALS measurement rate, must be = or > than ALS integration rate!
133139
Can be: 50, 100, 200, 500, 1000, or 2000 millisec"""
134140
return _measurement_rates[self._measurement_rate]
135141

136142
@measurement_rate.setter
137-
def measurement_rate(self, rate):
143+
def measurement_rate(self, rate: int) -> None:
138144
if not rate in _measurement_rates:
139145
raise RuntimeError(
140146
"Invalid measurement rate: must be 50, 100, 200, 500, 1000, or 2000 millisec"
141147
)
142148
self._measurement_rate = _measurement_rates.index(rate)
143149

144-
def throw_out_reading(self):
150+
def throw_out_reading(self) -> None:
145151
"""Throw out a reading (typically done to clear it out)"""
146152
_ = self._light_data
147153

148154
@property
149-
def light_channels(self):
155+
def light_channels(self) -> Tuple[int, int]:
150156
"""A data pair of both visible+IR light, and the IR-only light"""
151157
temp = self._light_data
152158
if self.als_data_invalid:
153159
raise ValueError("Data invalid / over-run!")
154160
return (temp >> 16, temp & 0xFFFF)
155161

156162
@property
157-
def visible_plus_ir_light(self):
163+
def visible_plus_ir_light(self) -> int:
158164
"""The visible + IR light data"""
159165
if self.als_data_invalid:
160166
_ = self._light_data # read data to clear it out
161167
raise ValueError("Data invalid / over-run!")
162168
return self._light_data >> 16
163169

164170
@property
165-
def ir_light(self):
171+
def ir_light(self) -> int:
166172
"""The IR light data"""
167173
if self.als_data_invalid:
168174
_ = self._light_data # read data to clear it out
169175
raise ValueError("Data invalid / over-run!")
170176
return self._light_data & 0xFFFF
171177

172178
@property
173-
def visible_light(self):
179+
def visible_light(self) -> int:
174180
"""The visible light data"""
175181
temp = self._light_data
176182
if self.als_data_invalid:
@@ -196,26 +202,26 @@ class LTR303(LTR329):
196202
_int_persistance = RWBits(4, _LTR303_REG_INTPERSIST, 0)
197203

198204
@property
199-
def int_persistance(self):
205+
def int_persistance(self) -> int:
200206
"""How long the data needs to be high/low to generate an interrupt.
201207
Setting of 1 means 'every measurement', 2 means "two in a row", etc
202208
up to 16
203209
"""
204210
return self._int_persistance + 1
205211

206212
@int_persistance.setter
207-
def int_persistance(self, counts):
213+
def int_persistance(self, counts: int) -> None:
208214
if not 1 <= counts <= 16:
209215
raise ValueError("Persistance counts must be 1-16")
210216
self._int_persistance = counts - 1
211217

212218
@property
213-
def enable_int(self):
219+
def enable_int(self) -> bool:
214220
"""Whether the interupt is enabled"""
215221
return self._enable_int
216222

217223
@enable_int.setter
218-
def enable_int(self, enable):
224+
def enable_int(self, enable: bool) -> None:
219225
# we must be in non-active mode to change this register!
220226
curr_mode = self.active_mode
221227
self.active_mode = False
@@ -225,12 +231,12 @@ def enable_int(self, enable):
225231
self.active_mode = curr_mode
226232

227233
@property
228-
def int_polarity(self):
234+
def int_polarity(self) -> bool:
229235
"""The polarity of the interupt (whether high or low is "active")"""
230236
return self._int_polarity
231237

232238
@int_polarity.setter
233-
def int_polarity(self, pol):
239+
def int_polarity(self, pol: bool) -> None:
234240
# we must be in non-active mode to change this register!
235241
curr_mode = self.active_mode
236242
self.active_mode = False

0 commit comments

Comments
 (0)