-
Notifications
You must be signed in to change notification settings - Fork 6
/
hcsr04.py
38 lines (34 loc) · 1.2 KB
/
hcsr04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from microbit import spi
from microbit import pin13, pin14, pin15
class HCSR04:
def __init__(self, tpin=pin15, epin=pin14, spin=pin13):
self.trigger_pin = tpin
self.echo_pin = epin
self.sclk_pin = spin
def distance_mm(self):
spi.init(baudrate=125000, sclk=self.sclk_pin,
mosi=self.trigger_pin, miso=self.echo_pin)
pre = 0
post = 0
k = -1
length = 500
resp = bytearray(length)
resp[0] = 0xFF
spi.write_readinto(resp, resp)
# find first non zero value
try:
i, value = next((ind, v) for ind, v in enumerate(resp) if v)
except StopIteration:
i = -1
if i > 0:
pre = bin(value).count("1")
# find first non full high value afterwards
try:
k, value = next((ind, v)
for ind, v in enumerate(resp[i:length - 2]) if resp[i + ind + 1] == 0)
post = bin(value).count("1") if k else 0
k = k + i
except StopIteration:
i = -1
dist= -1 if i < 0 else round(((pre + (k - i) * 8. + post) * 8 * 0.172) / 2)
return dist