Skip to content

Commit dc6a1de

Browse files
committed
works with CPython too
1 parent 2fc35a9 commit dc6a1de

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

adafruit_tfmini.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ def __init__(self, uart, *, timeout=1):
7171
@property
7272
def distance(self):
7373
"""The most recent distance measurement in centimeters"""
74+
try:
75+
self._uart.reset_input_buffer()
76+
except AttributeError:
77+
# not implemented, we'll just keep going
78+
pass
79+
7480
# listen for new packet
7581
stamp = time.monotonic()
7682
while time.monotonic() - stamp < self.timeout:
7783
# look for the header start
7884
x = self._uart.read(1)
79-
if x is None or x[0] != 0x59:
85+
if not x or x[0] != 0x59:
8086
continue
8187
# get remaining packet
8288
data = self._uart.read(8)
@@ -118,7 +124,7 @@ def _set_config(self, command):
118124
while (time.monotonic() - stamp) < self.timeout:
119125
# look for the header start
120126
x = self._uart.read(1)
121-
if x is None or x[0] != 0x42:
127+
if not x or x[0] != 0x42:
122128
continue
123129
echo = self._uart.read(len(_STARTREPLY))
124130
#print("start ", [hex(i) for i in echo])

examples/tfmini_simpletest.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import time
2-
import board
2+
import board # comment this out if using pyserial
3+
import busio # comment this out if using pyserial
34
import adafruit_tfmini
45

56
# Use hardware uart
6-
import busio
77
uart = busio.UART(board.TX, board.RX)
88

9+
# Or, you can use pyserial on any computer
10+
#import serial
11+
#uart = serial.Serial("/dev/ttyS2", timeout=1)
12+
913
# Simplest use, connect with the uart bus object
1014
tfmini = adafruit_tfmini.TFmini(uart)
1115

12-
# You can put in 'long distance' mode
13-
#tfmini.mode = adafruit_tfmini.MODE_LONG
14-
#print("Now in mode", tfmini.mode)
16+
# You can put in 'short' or 'long' distance mode
17+
tfmini.mode = adafruit_tfmini.MODE_SHORT
18+
print("Now in mode", tfmini.mode)
1519

1620
while True:
1721
print("Distance: %d cm (strength %d, mode %x)" %

0 commit comments

Comments
 (0)