-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_devices.py
142 lines (118 loc) · 5.19 KB
/
detect_devices.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
Class Detect Devices used in PortaBrick Arcade project
Copyright <2023> <LC-jrx>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port
from uerrno import ENODEV
class NotRunException(Exception):
def __init__(self):
print("Before detecting devices run port scan.")
class DetectDevices:
""" Class to detect which devices are connected to which port.
Basis taken from: https://docs.pybricks.com/en/latest/iodevices/pupdevice.html#detecting-devices"""
def __init__(self):
self.matrix_available = 0
self.matrix_ports = []
self.device_names = None
self.__ports = None
self.__scan_ports()
self.__detect_matrixes()
def __scan_ports(self):
# Dictionary of device identifiers along with their name.
self.device_names = {
# pybricks.pupdevices.DCMotor
1: "Wedo 2.0 Medium Motor",
2: "Powered Up Train Motor",
# pybricks.pupdevices.Light
8: "Powered Up Light",
# pybricks.pupdevices.Motor
38: "BOOST Interactive Motor",
46: "Technic Large Motor",
47: "Technic Extra Large Motor",
48: "SPIKE Medium Angular Motor",
49: "SPIKE Large Angular Motor",
65: "SPIKE Small Angular Motor",
75: "Technic Medium Angular Motor",
76: "Technic Large Angular Motor",
# pybricks.pupdevices.TiltSensor
34: "Wedo 2.0 Tilt Sensor",
# pybricks.pupdevices.InfraredSensor
35: "Wedo 2.0 Infrared Motion Sensor",
# pybricks.pupdevices.ColorDistanceSensor
37: "BOOST Color Distance Sensor",
# pybricks.pupdevices.ColorSensor
61: "SPIKE Color Sensor",
# pybricks.pupdevices.UltrasonicSensor
62: "SPIKE Ultrasonic Sensor",
# pybricks.pupdevices.ForceSensor
63: "SPIKE Force Sensor",
# pybricks.pupdevices.ColorLightMatrix
64: "SPIKE 3x3 Color Light Matrix",
}
# Make a list of known ports.
self.__ports = [Port.A, Port.B]
# On hubs that support it, add more ports.
try:
self.__ports.append(Port.C)
self.__ports.append(Port.D)
except AttributeError:
pass
# On hubs that support it, add more ports.
try:
self.__ports.append(Port.E)
self.__ports.append(Port.F)
except AttributeError:
pass
# print("Detected Ports", self.ports)
# print("Matrixes: ", self.matrix_available)
# print("Matrix Ports: ", self.matrix_ports)
def __detect_matrixes(self):
try:
if self.__ports is None:
raise NotRunException
elif self.device_names is None:
raise NotRunException
except NotRunException:
raise
# Go through all available ports.
for port in self.__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
device_id = device.info()["id"]
# Look up the name.
# try:
# print(port, ":", self.device_names[device_id], 'ID:', device_id)
# except KeyError:
# print(port, ":", "Unknown device with ID", device_id)
# Register the count of ColorLightMatrix modules and its ports
if device_id == 64:
self.matrix_available += 1
# ColorLightMatrix(port).on(Color.YELLOW)
# print("Matrixes: ", self.matrix_available)
self.matrix_ports.append(port)
# wait(500)
# ColorLightMatrix(port).off()
if __name__ == "__main__":
testrun = DetectDevices()
print("Matrixes: ", testrun.matrix_available)
print("Matrix Ports: ", testrun.matrix_ports)