-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
71 lines (63 loc) · 1.95 KB
/
scan.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from pybricks.iodevices import PUPDevice
from pybricks.pupdevices import Motor, ColorSensor
from pybricks.parameters import Port
from pybricks.tools import wait
from uerrno import ENODEV
# Scan all ports and report what is connected to it.
# Copied from
# https://docs.pybricks.com/en/stable/iodevices/pupdevice.html
# Dictionary of device identifiers along with their name.
device_names = {
34: "Wedo 2.0 Tilt Sensor",
35: "Wedo 2.0 Infrared Sensor",
37: "BOOST Color Distance Sensor",
38: "BOOST Interactive Motor",
46: "Technic Large Motor",
47: "Technic Extra Large Motor",
48: "SPIKE Medium Angular Motor",
49: "SPIKE Large Angular Motor",
61: "SPIKE Color Sensor",
62: "SPIKE Ultrasonic Sensor",
63: "SPIKE Force Sensor",
75: "Technic Medium Angular Motor",
76: "Technic Large Angular Motor",
}
# Make a list of known ports.
ports = [Port.A, Port.B]
# On hubs that support it, add more ports.
try:
ports.append(Port.C)
ports.append(Port.D)
except AttributeError:
pass
# On hubs that support it, add more ports.
try:
ports.append(Port.E)
ports.append(Port.F)
except AttributeError:
pass
# Go through all available ports.
for port in ports:
# Try to get the device, if it is attached.
try:
device = PUPDevice(port)
except OSError as ex:
if ex.args[0] == ENODEV:
# No device found on this port.
print(port, ": ---")
continue
else:
raise
# Get the device id
id = device.info()["id"]
# Look up the name.
try:
print(port, ":", device_names[id])
if device_names[id] == "SPIKE Large Angular Motor":
motor = Motor(port)
print("Angle: " + str(motor.angle()))
if device_names[id] == "SPIKE Medium Angular Motor":
motor = Motor(port)
print("Angle: " + str(motor.angle()))
except KeyError:
print(port, ":", "Unknown device with ID", id)