Skip to content

Commit e2ed4eb

Browse files
committed
linted
1 parent 99ffc73 commit e2ed4eb

File tree

2 files changed

+72
-23
lines changed

2 files changed

+72
-23
lines changed

adafruit_lidarlite.py

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
3838
* Adafruit CircuitPython firmware for the supported boards:
3939
https://github.com/adafruit/circuitpython/releases
40-
40+
4141
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4242
4343
"""
@@ -52,18 +52,18 @@
5252
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LIDARLite.git"
5353

5454

55-
5655
_ADDR_DEFAULT = const(0x62)
56+
_REG_ACQ_COMMAND = const(0x00)
57+
_CMD_RESET = const(0)
58+
_CMD_DISTANCENOBIAS = const(3)
59+
_CMD_DISTANCEWITHBIAS = const(4)
60+
5761
CONFIG_DEFAULT = 0
5862
CONFIG_SHORTFAST = 1
5963
CONFIG_DEFAULTFAST = 2
6064
CONFIG_MAXRANGE = 3
6165
CONFIG_HIGHSENSITIVE = 4
6266
CONFIG_LOWSENSITIVE = 5
63-
_REG_ACQ_COMMAND = const(0x00)
64-
_CMD_RESET = const(0)
65-
_CMD_DISTANCENOBIAS = const(3)
66-
_CMD_DISTANCEWITHBIAS = const(4)
6767

6868
STATUS_BUSY = 0x01
6969
STATUS_REF_OVERFLOW = 0x02
@@ -73,14 +73,14 @@
7373
STATUS_HEALTHY = 0x20
7474
STATUS_SYS_ERROR = 0x40
7575

76-
7776
# The various configuration register values, from arduino library
78-
_configurations = ((0x80, 0x08, 0x00), # default
79-
(0x1D, 0x08, 0x00), # short range, high speed
80-
(0x80, 0x00, 0x00), # default range, higher speed short range
81-
(0xFF, 0x08, 0x00), # maximum range
82-
(0x80, 0x08, 0x80), # high sensitivity & error
83-
(0x80, 0x08, 0xb0)) # low sensitivity & error
77+
_LIDAR_CONFIGS = ((0x80, 0x08, 0x00), # default
78+
(0x1D, 0x08, 0x00), # short range, high speed
79+
(0x80, 0x00, 0x00), # default range, higher speed short range
80+
(0xFF, 0x08, 0x00), # maximum range
81+
(0x80, 0x08, 0x80), # high sensitivity & error
82+
(0x80, 0x08, 0xb0)) # low sensitivity & error
83+
8484
class LIDARLite:
8585
"""
8686
A driver for the Garmin LIDAR Lite laser distance sensor.
@@ -89,15 +89,27 @@ class LIDARLite:
8989
:param int address: (optional) The I2C address of the device to set after initialization.
9090
"""
9191

92-
def __init__(self, i2c_bus, *, reset_pin=None, configuration=CONFIG_DEFAULT, address=_ADDR_DEFAULT):
92+
def __init__(self, i2c_bus, *, reset_pin=None,
93+
configuration=CONFIG_DEFAULT, address=_ADDR_DEFAULT):
94+
"""Initialize the hardware for the LIDAR over I2C. You can pass in an
95+
optional reset_pin for when you call reset(). There are a few common
96+
configurations Garmin suggests: CONFIG_DEFAULT, CONFIG_SHORTFAST,
97+
CONFIG_DEFAULTFAST, CONFIG_MAXRANGE, CONFIG_HIGHSENSITIVE, and
98+
CONFIG_LOWSENSITIVE. For the I2C address, the default is 0x62 but if you
99+
pass a different number in, we'll try to change the address so multiple
100+
LIDARs can be connected. (Note all but one need to be in reset for this
101+
to work!)"""
93102
self.i2c_device = I2CDevice(i2c_bus, address)
94103
self._buf = bytearray(2)
95104
self._bias_count = 0
96105
self._reset = reset_pin
97106
time.sleep(0.5)
98107
self.configure(configuration)
99-
108+
self._status = self.status
109+
100110
def reset(self):
111+
"""Hardware reset (if pin passed into init) or software reset. Will take
112+
100 readings in order to 'flush' measurement unit, otherwise data is off."""
101113
# Optional hardware reset pin
102114
if self._reset is not None:
103115
self._reset.direction = Direction.OUTPUT
@@ -119,54 +131,61 @@ def reset(self):
119131
pass
120132

121133
def configure(self, config):
122-
settings = _configurations[config]
134+
"""Set the LIDAR desired style of measurement. There are a few common
135+
configurations Garmin suggests: CONFIG_DEFAULT, CONFIG_SHORTFAST,
136+
CONFIG_DEFAULTFAST, CONFIG_MAXRANGE, CONFIG_HIGHSENSITIVE, and
137+
CONFIG_LOWSENSITIVE."""
138+
settings = _LIDAR_CONFIGS[config]
123139
self._write_reg(0x02, settings[0])
124140
self._write_reg(0x04, settings[1])
125141
self._write_reg(0x1c, settings[2])
126142

127143
def read_distance(self, bias=False):
144+
"""Perform a distance reading with or without 'bias'. It's recommended
145+
to take a bias measurement every 100 non-bias readings (they're slower)"""
128146
if bias:
129147
self._write_reg(_REG_ACQ_COMMAND, _CMD_DISTANCEWITHBIAS)
130148
else:
131149
self._write_reg(_REG_ACQ_COMMAND, _CMD_DISTANCENOBIAS)
132-
d = self._read_reg(0x8F, 2)
150+
dist = self._read_reg(0x8F, 2)
133151
if self._status & (STATUS_NO_PEAK | STATUS_SECOND_RETURN):
134152
raise RuntimeError("Measurement failure")
135153
if (self._status & STATUS_SYS_ERROR) or (not self._status & STATUS_HEALTHY):
136154
raise RuntimeError("System failure")
137-
dist = d[0] << 8 | d[1]
138-
return dist
155+
return dist[0] << 8 | dist[1]
139156

140157
@property
141158
def distance(self):
159+
"""The measured distance in cm. Will take a bias reading every 100 calls"""
142160
self._bias_count -= 1
143161
if self._bias_count < 0:
144162
self._bias_count = 100 # every 100 reads, check bias
145163
return self.read_distance(self._bias_count <= 0)
146-
164+
147165
@property
148166
def status(self):
167+
"""The status byte, check datasheet for bitmask"""
149168
buf = bytearray([0x1])
150169
with self.i2c_device as i2c:
151170
i2c.write_then_readinto(buf, buf)
152171
return buf[0]
153172

154-
def _write_reg(self, reg, value):
173+
def _write_reg(self, reg, value):
155174
self._buf[0] = reg
156175
self._buf[1] = value
157176
with self.i2c_device as i2c:
158177
#print("Writing: ", [hex(i) for i in self._buf])
159178
i2c.write(self._buf)
160179
time.sleep(0.001) # there's a delay in arduino library
161180

162-
def _read_reg(self, reg, len):
181+
def _read_reg(self, reg, num):
163182
while True:
164183
self._status = self.status
165184
if not self._status & STATUS_BUSY:
166185
break
167186
# no longer busy
168187
self._buf[0] = reg
169188
with self.i2c_device as i2c:
170-
i2c.write_then_readinto(self._buf, self._buf, out_end=1, in_end=len)
189+
i2c.write_then_readinto(self._buf, self._buf, out_end=1, in_end=num)
171190
#print("Read from ", hex(reg), [hex(i) for i in self._buf])
172191
return self._buf

examples/lidarlite_simpletest.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_lidarlite
5+
6+
7+
# Create library object using our Bus I2C port
8+
i2c = busio.I2C(board.SCL, board.SDA)
9+
10+
# Default configuration, with only i2c wires
11+
sensor = adafruit_lidarlite.LIDARLite(i2c)
12+
13+
# Optionally, we can pass in a hardware reset pin, or custom config
14+
#import digitalio
15+
#reset = digitalio.DigitalInOut(board.D5)
16+
#sensor = adafruit_lidarlite.LIDARLite(i2c, reset_pin=reset,
17+
# configuration=adafruit_lidarlite.CONFIG_MAXRANGE)
18+
19+
# If you want to reset, you can do so, note that it can take 10-20 seconds
20+
# for the data to 'normalize' after a reset (and this isnt documented at all)
21+
# sensor.reset()
22+
23+
while True:
24+
try:
25+
# We print tuples so you can plot with Mu Plotter
26+
print((sensor.distance,))
27+
except RuntimeError as e:
28+
# If we get a reading error, just print it and keep truckin'
29+
print(e)
30+
time.sleep(0.01) # you can remove this for ultra-fast measurements!

0 commit comments

Comments
 (0)