-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathultrasonic-ble-speak-2.py
52 lines (44 loc) · 1.25 KB
/
ultrasonic-ble-speak-2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
from ev3dev2.sound import Sound
import pygatt
from time import sleep
from binascii import hexlify
DELAY = 1.0
def handle_data(handle, value):
"""
handle -- integer, characteristic read handle the data was received on
value -- bytearray, the data returned in the notification
"""
distance = int(value.decode("utf-8"))
print("Dist = {0:.2f} m".format(distance/100) )
sound.speak(value.decode("utf-8"))
adapter = pygatt.GATTToolBackend()
adapter.start()
sound = Sound()
sound.speak('Welcome to the E V 3 dev project!')
while True:
try:
print("Connecting...")
device = adapter.connect('F5:91:E3:32:23:39', address_type=pygatt.BLEAddressType.random)
print("Connected")
break
except pygatt.exceptions.NotConnectedError:
print("Not connected")
sleep(1)
continue
while True:
try:
device.subscribe("6e400002-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data, indication=True)
break
except pygatt.exceptions.BLEError:
print("unknown characteristic")
try:
while True:
sleep(DELAY)
except Exception as e:
print(e)
except (KeyboardInterrupt, SystemExit):
print("Kbd/SysX")
adapter.stop()
finally:
adapter.stop()