3232from digitalio import Direction
3333from micropython import const
3434
35+ try :
36+ from typing import Optional
37+ from microcontroller import Pin
38+ from busio import I2C
39+ except ImportError :
40+ pass
41+
3542__version__ = "0.0.0+auto.0"
3643__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LIDARLite.git"
3744
@@ -115,13 +122,13 @@ class LIDARLite:
115122
116123 def __init__ (
117124 self ,
118- i2c_bus ,
125+ i2c_bus : I2C ,
119126 * ,
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 :
125132 self .i2c_device = I2CDevice (i2c_bus , address )
126133 self ._buf = bytearray (2 )
127134 self ._bias_count = 0
@@ -131,7 +138,7 @@ def __init__(
131138 self ._status = self .status
132139 self ._sensor_type = sensor_type
133140
134- def reset (self ):
141+ def reset (self ) -> None :
135142 """Hardware reset (if pin passed into init) or software reset. Will take
136143 100 readings in order to 'flush' measurement unit, otherwise data is off."""
137144 # Optional hardware reset pin
@@ -154,7 +161,7 @@ def reset(self):
154161 except RuntimeError :
155162 print ("RuntimeError" )
156163
157- def configure (self , config ) :
164+ def configure (self , config : int ) -> None :
158165 """Set the LIDAR desired style of measurement. There are a few common
159166 configurations Garmin suggests: CONFIG_DEFAULT, CONFIG_SHORTFAST,
160167 CONFIG_DEFAULTFAST, CONFIG_MAXRANGE, CONFIG_HIGHSENSITIVE, and
@@ -164,7 +171,7 @@ def configure(self, config):
164171 self ._write_reg (_REG_ACQ_CONFIG_REG , settings [1 ])
165172 self ._write_reg (_REG_THRESHOLD_BYPASS , settings [2 ])
166173
167- def read_distance_v3 (self , bias = False ):
174+ def read_distance_v3 (self , bias : Optional [ bool ] = False ) -> int :
168175 """Perform a distance reading with or without 'bias'. It's recommended
169176 to take a bias measurement every 100 non-bias readings (they're slower)"""
170177 if bias :
@@ -183,7 +190,7 @@ def read_distance_v3(self, bias=False):
183190 raise RuntimeError ("System failure" )
184191 return dist [0 ] << 8 | dist [1 ]
185192
186- def read_distance_v3hp (self ):
193+ def read_distance_v3hp (self ) -> int :
187194 """Perform a distance measurement for the v3 HP sensor"""
188195 # Any non-zero value written to _REG_ACQ_COMMAND will start a reading on v3HP, no bias vs.
189196 # non-bias
@@ -192,50 +199,50 @@ def read_distance_v3hp(self):
192199 return dist [0 ] << 8 | dist [1 ]
193200
194201 @property
195- def correlation_data (self ):
202+ def correlation_data (self ) -> int :
196203 """Reads correlation data"""
197204 # TODO: How to translate correlation data property?
198205 corr_data = self ._read_reg (_REG_CORR_DATA , 2 )
199206 return corr_data [0 ] << 8 | corr_data [1 ]
200207
201208 @property
202- def test_command (self ):
209+ def test_command (self ) -> int :
203210 """Reads the test command"""
204211 return self ._read_reg (_REG_TEST_COMMAND , 1 )[0 ]
205212
206213 @property
207- def i2c_config (self ):
214+ def i2c_config (self ) -> int :
208215 """Reads the I2C config"""
209216 return self ._read_reg (_REG_I2C_CONFIG , 1 )[0 ]
210217
211218 @property
212- def power_control (self ):
219+ def power_control (self ) -> int :
213220 """Reads the power control register"""
214221 return self ._read_reg (_REG_POWER_CONTROL , 1 )[0 ]
215222
216223 @property
217- def health_status (self ):
224+ def health_status (self ) -> int :
218225 """Reads health status for v3HP (not available on v3, will return -1)"""
219226 if self ._sensor_type == TYPE_V3HP :
220227 return self ._read_reg (_REG_HEALTH_STATUS_V3HP , 1 )[0 ]
221228
222229 return - 1
223230
224231 @property
225- def signal_strength (self ):
232+ def signal_strength (self ) -> int :
226233 """Reads the signal strength of the last measurement"""
227234 return self ._read_reg (_REG_SIGNAL_STRENGTH , 1 )[0 ]
228235
229236 @property
230- def unit_id (self ):
237+ def unit_id (self ) -> int :
231238 """Reads the serial number of the unit"""
232239 high_byte = self ._read_reg (_REG_UNIT_ID_HIGH , 1 )
233240 low_byte = self ._read_reg (_REG_UNIT_ID_LOW , 1 )
234241
235242 return high_byte [0 ] << 8 | low_byte [0 ]
236243
237244 @property
238- def distance (self ): # pylint: disable=R1710
245+ def distance (self ) -> int : # pylint: disable=R1710
239246 """The measured distance in cm. Will take a bias reading every 100 calls"""
240247 self ._bias_count -= 1
241248
@@ -250,22 +257,22 @@ def distance(self): # pylint: disable=R1710
250257 return - 1.0
251258
252259 @property
253- def status (self ):
260+ def status (self ) -> int :
254261 """The status byte, check datasheet for bitmask"""
255262 buf = bytearray ([_REG_STATUS ])
256263 with self .i2c_device as i2c :
257264 i2c .write_then_readinto (buf , buf )
258265 return buf [0 ]
259266
260- def _write_reg (self , reg , value ) :
267+ def _write_reg (self , reg : int , value : int ) -> None :
261268 self ._buf [0 ] = reg
262269 self ._buf [1 ] = value
263270 with self .i2c_device as i2c :
264271 # print("Writing: ", [hex(i) for i in self._buf])
265272 i2c .write (self ._buf )
266273 time .sleep (0.001 ) # there's a delay in arduino library
267274
268- def _read_reg (self , reg , num ) :
275+ def _read_reg (self , reg : int , num : int ) -> bytearray :
269276 while True :
270277 self ._status = self .status
271278 if not self ._status & STATUS_BUSY :
0 commit comments