32
32
from digitalio import Direction
33
33
from micropython import const
34
34
35
+ try :
36
+ from typing import Optional
37
+ from microcontroller import Pin
38
+ from busio import I2C
39
+ except ImportError :
40
+ pass
41
+
35
42
__version__ = "0.0.0+auto.0"
36
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LIDARLite.git"
37
44
@@ -115,13 +122,13 @@ class LIDARLite:
115
122
116
123
def __init__ (
117
124
self ,
118
- i2c_bus ,
125
+ i2c_bus : I2C ,
119
126
* ,
120
- reset_pin = None ,
121
- configuration = CONFIG_DEFAULT ,
122
- address = _ADDR_DEFAULT ,
123
- sensor_type = TYPE_V3 ,
124
- ):
127
+ reset_pin : Optional [ Pin ] = None ,
128
+ configuration : Optional [ int ] = CONFIG_DEFAULT ,
129
+ address : Optional [ int ] = _ADDR_DEFAULT ,
130
+ sensor_type : Optional [ str ] = TYPE_V3 ,
131
+ ) -> None :
125
132
self .i2c_device = I2CDevice (i2c_bus , address )
126
133
self ._buf = bytearray (2 )
127
134
self ._bias_count = 0
@@ -131,7 +138,7 @@ def __init__(
131
138
self ._status = self .status
132
139
self ._sensor_type = sensor_type
133
140
134
- def reset (self ):
141
+ def reset (self ) -> None :
135
142
"""Hardware reset (if pin passed into init) or software reset. Will take
136
143
100 readings in order to 'flush' measurement unit, otherwise data is off."""
137
144
# Optional hardware reset pin
@@ -154,7 +161,7 @@ def reset(self):
154
161
except RuntimeError :
155
162
print ("RuntimeError" )
156
163
157
- def configure (self , config ) :
164
+ def configure (self , config : int ) -> None :
158
165
"""Set the LIDAR desired style of measurement. There are a few common
159
166
configurations Garmin suggests: CONFIG_DEFAULT, CONFIG_SHORTFAST,
160
167
CONFIG_DEFAULTFAST, CONFIG_MAXRANGE, CONFIG_HIGHSENSITIVE, and
@@ -164,7 +171,7 @@ def configure(self, config):
164
171
self ._write_reg (_REG_ACQ_CONFIG_REG , settings [1 ])
165
172
self ._write_reg (_REG_THRESHOLD_BYPASS , settings [2 ])
166
173
167
- def read_distance_v3 (self , bias = False ):
174
+ def read_distance_v3 (self , bias : Optional [ bool ] = False ) -> int :
168
175
"""Perform a distance reading with or without 'bias'. It's recommended
169
176
to take a bias measurement every 100 non-bias readings (they're slower)"""
170
177
if bias :
@@ -183,7 +190,7 @@ def read_distance_v3(self, bias=False):
183
190
raise RuntimeError ("System failure" )
184
191
return dist [0 ] << 8 | dist [1 ]
185
192
186
- def read_distance_v3hp (self ):
193
+ def read_distance_v3hp (self ) -> int :
187
194
"""Perform a distance measurement for the v3 HP sensor"""
188
195
# Any non-zero value written to _REG_ACQ_COMMAND will start a reading on v3HP, no bias vs.
189
196
# non-bias
@@ -192,50 +199,50 @@ def read_distance_v3hp(self):
192
199
return dist [0 ] << 8 | dist [1 ]
193
200
194
201
@property
195
- def correlation_data (self ):
202
+ def correlation_data (self ) -> int :
196
203
"""Reads correlation data"""
197
204
# TODO: How to translate correlation data property?
198
205
corr_data = self ._read_reg (_REG_CORR_DATA , 2 )
199
206
return corr_data [0 ] << 8 | corr_data [1 ]
200
207
201
208
@property
202
- def test_command (self ):
209
+ def test_command (self ) -> int :
203
210
"""Reads the test command"""
204
211
return self ._read_reg (_REG_TEST_COMMAND , 1 )[0 ]
205
212
206
213
@property
207
- def i2c_config (self ):
214
+ def i2c_config (self ) -> int :
208
215
"""Reads the I2C config"""
209
216
return self ._read_reg (_REG_I2C_CONFIG , 1 )[0 ]
210
217
211
218
@property
212
- def power_control (self ):
219
+ def power_control (self ) -> int :
213
220
"""Reads the power control register"""
214
221
return self ._read_reg (_REG_POWER_CONTROL , 1 )[0 ]
215
222
216
223
@property
217
- def health_status (self ):
224
+ def health_status (self ) -> int :
218
225
"""Reads health status for v3HP (not available on v3, will return -1)"""
219
226
if self ._sensor_type == TYPE_V3HP :
220
227
return self ._read_reg (_REG_HEALTH_STATUS_V3HP , 1 )[0 ]
221
228
222
229
return - 1
223
230
224
231
@property
225
- def signal_strength (self ):
232
+ def signal_strength (self ) -> int :
226
233
"""Reads the signal strength of the last measurement"""
227
234
return self ._read_reg (_REG_SIGNAL_STRENGTH , 1 )[0 ]
228
235
229
236
@property
230
- def unit_id (self ):
237
+ def unit_id (self ) -> int :
231
238
"""Reads the serial number of the unit"""
232
239
high_byte = self ._read_reg (_REG_UNIT_ID_HIGH , 1 )
233
240
low_byte = self ._read_reg (_REG_UNIT_ID_LOW , 1 )
234
241
235
242
return high_byte [0 ] << 8 | low_byte [0 ]
236
243
237
244
@property
238
- def distance (self ): # pylint: disable=R1710
245
+ def distance (self ) -> int : # pylint: disable=R1710
239
246
"""The measured distance in cm. Will take a bias reading every 100 calls"""
240
247
self ._bias_count -= 1
241
248
@@ -250,22 +257,22 @@ def distance(self): # pylint: disable=R1710
250
257
return - 1.0
251
258
252
259
@property
253
- def status (self ):
260
+ def status (self ) -> int :
254
261
"""The status byte, check datasheet for bitmask"""
255
262
buf = bytearray ([_REG_STATUS ])
256
263
with self .i2c_device as i2c :
257
264
i2c .write_then_readinto (buf , buf )
258
265
return buf [0 ]
259
266
260
- def _write_reg (self , reg , value ) :
267
+ def _write_reg (self , reg : int , value : int ) -> None :
261
268
self ._buf [0 ] = reg
262
269
self ._buf [1 ] = value
263
270
with self .i2c_device as i2c :
264
271
# print("Writing: ", [hex(i) for i in self._buf])
265
272
i2c .write (self ._buf )
266
273
time .sleep (0.001 ) # there's a delay in arduino library
267
274
268
- def _read_reg (self , reg , num ) :
275
+ def _read_reg (self , reg : int , num : int ) -> bytearray :
269
276
while True :
270
277
self ._status = self .status
271
278
if not self ._status & STATUS_BUSY :
0 commit comments