Skip to content

Commit 2fc35a9

Browse files
committed
linted!
1 parent 558d835 commit 2fc35a9

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

adafruit_tfmini.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@
5757
class TFmini:
5858
"""TF mini communication module, use with just RX or TX+RX for advanced
5959
command & control.
60+
:param uart: the pyseral or busio.uart compatible uart device
61+
:param timeout: how long we'll wait for valid data or response, in seconds. Default is 1
6062
"""
61-
62-
def __init__(self, uart, *, timeout=3):
63+
64+
def __init__(self, uart, *, timeout=1):
6365
self._uart = uart
6466
self._uart.baudrate = 115200
6567
self.timeout = timeout
@@ -73,31 +75,33 @@ def distance(self):
7375
stamp = time.monotonic()
7476
while time.monotonic() - stamp < self.timeout:
7577
# look for the header start
76-
c = self._uart.read(1)
77-
if c[0] != 0x59:
78+
x = self._uart.read(1)
79+
if x is None or x[0] != 0x59:
7880
continue
7981
# get remaining packet
8082
data = self._uart.read(8)
8183
# check first byte is magicbyte
82-
framebyte, distance, self._strength, self._mode, _, checksum = struct.unpack("<BHHBBB",data)
84+
frame, dist, self._strength, self._mode, _, checksum = struct.unpack("<BHHBBB", data)
8385
# look for second 0x59 frame indicator
84-
if framebyte != 0x59:
86+
if frame != 0x59:
8587
continue
8688
# calculate and check sum
8789
mysum = (sum(data[0:7]) + 0x59) & 0xFF
8890
if mysum != checksum:
8991
continue
90-
return distance
92+
return dist
9193
raise RuntimeError("Timed out looking for valid data")
9294

9395
@property
9496
def strength(self):
95-
self.distance
97+
"""The signal validity, higher value means better measurement"""
98+
_ = self.distance # trigger distance measurement
9699
return self._strength
97100

98101
@property
99102
def mode(self):
100-
self.distance
103+
"""The measurement mode can be MODE_SHORT (2) or MODE_LONG (7)"""
104+
_ = self.distance # trigger distance measurement
101105
return self._mode
102106

103107
@mode.setter
@@ -107,13 +111,15 @@ def mode(self, newmode):
107111
self._set_config(_CONFIGPARAM + bytes([0, 0, newmode, 0x11]))
108112

109113
def _set_config(self, command):
114+
"""Manager for sending commands, put sensor into config mode, config,
115+
then exit configuration mode!"""
110116
self._uart.write(_STARTCONFIG)
111117
stamp = time.monotonic()
112118
while (time.monotonic() - stamp) < self.timeout:
113119
# look for the header start
114-
c = self._uart.read(1)
115-
if c is None or c[0] != 0x42:
116-
continue
120+
x = self._uart.read(1)
121+
if x is None or x[0] != 0x42:
122+
continue
117123
echo = self._uart.read(len(_STARTREPLY))
118124
#print("start ", [hex(i) for i in echo])
119125
if echo != _STARTREPLY:

examples/tfmini_simpletest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import time
2+
import board
3+
import adafruit_tfmini
4+
5+
# Use hardware uart
6+
import busio
7+
uart = busio.UART(board.TX, board.RX)
8+
9+
# Simplest use, connect with the uart bus object
10+
tfmini = adafruit_tfmini.TFmini(uart)
11+
12+
# You can put in 'long distance' mode
13+
#tfmini.mode = adafruit_tfmini.MODE_LONG
14+
#print("Now in mode", tfmini.mode)
15+
16+
while True:
17+
print("Distance: %d cm (strength %d, mode %x)" %
18+
(tfmini.distance, tfmini.strength, tfmini.mode))
19+
time.sleep(0.1)

0 commit comments

Comments
 (0)