Skip to content

Commit bbc65e1

Browse files
authored
Add device descriptions to Hat.get() (#124)
1 parent 16485fc commit bbc65e1

File tree

2 files changed

+60
-30
lines changed

2 files changed

+60
-30
lines changed

buildhat/devices.py

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,34 @@ class Device:
1010
"""Creates a single instance of the buildhat for all devices to use"""
1111
_instance = None
1212
_started = 0
13-
_device_names = { 1: "PassiveMotor",
14-
2: "PassiveMotor",
15-
8: "Light",
16-
34: "TiltSensor",
17-
35: "MotionSensor",
18-
37: "ColorDistanceSensor",
19-
61: "ColorSensor",
20-
62: "DistanceSensor",
21-
63: "ForceSensor",
22-
64: "Matrix",
23-
38: "Motor",
24-
46: "Motor",
25-
47: "Motor",
26-
48: "Motor",
27-
49: "Motor",
28-
65: "Motor",
29-
75: "Motor",
30-
76: "Motor"
13+
_device_names = { 1: ("PassiveMotor", "PassiveMotor"),
14+
2: ("PassiveMotor", "PassiveMotor"),
15+
8: ("Light", "Light"), # 88005
16+
34: ("TiltSensor", "WeDo 2.0 Tilt Sensor"), # 45305
17+
35: ("MotionSensor", "MotionSensor"), # 45304
18+
37: ("ColorDistanceSensor", "Color & Distance Sensor"), # 88007
19+
61: ("ColorSensor", "Color Sensor"), # 45605
20+
62: ("DistanceSensor","Distance Sensor"), # 45604
21+
63: ("ForceSensor", "Force Sensor"), # 45606
22+
64: ("Matrix", "3x3 Color Light Matrix"), # 45608
23+
38: ("Motor", "Medium Linear Motor"), # 88008
24+
46: ("Motor", "Large Motor"), # 88013
25+
47: ("Motor", "XL Motor"), # 88014
26+
48: ("Motor", "Medium Angular Motor (Cyan)"), # 45603
27+
49: ("Motor", "Large Angular Motor (Cyan)"), # 45602
28+
65: ("Motor", "Small Angular Motor"), # 45607
29+
75: ("Motor", "Medium Angular Motor (Grey)"), # 88018
30+
76: ("Motor", "Large Angular Motor (Grey)") # 88017
3131
}
3232
_used = { 0: False,
3333
1: False,
3434
2: False,
3535
3: False
3636
}
37-
37+
38+
UNKNOWN_DEVICE = "Unknown"
39+
DISCONNECTED_DEVICE = "Disconnected"
40+
3841
def __init__(self, port):
3942
if not isinstance(port, str) or len(port) != 1:
4043
raise DeviceNotFound("Invalid port")
@@ -48,7 +51,7 @@ def __init__(self, port):
4851
self._simplemode = -1
4952
self._combimode = -1
5053
self._typeid = self._conn.typeid
51-
if (self._typeid in Device._device_names and Device._device_names[self._typeid] != type(self).__name__) or self._typeid == -1:
54+
if (self._typeid in Device._device_names and Device._device_names[self._typeid][0] != type(self).__name__) or self._typeid == -1:
5255
raise DeviceInvalid('There is not a {} connected to port {} (Found {})'.format(type(self).__name__, port, self.name))
5356
Device._used[p] = True
5457

@@ -67,14 +70,28 @@ def _setup(device="/dev/serial0"):
6770

6871
def __del__(self):
6972
if hasattr(self, "port") and Device._used[self.port]:
70-
if self._typeid == 64:
73+
if Device._device_names[self._typeid][0] == "Matrix":
7174
self.clear()
7275
Device._used[self.port] = False
7376
self._conn.callit = None
7477
self.deselect()
75-
if self._typeid != 64:
78+
if Device._device_names[self._typeid][0] != "Matrix":
7679
self.off()
7780

81+
def name_for_id(typeid):
82+
"""Translate integer type id to device name (python class)"""
83+
if typeid in Device._device_names:
84+
return Device._device_names[typeid][0]
85+
else:
86+
return Device.UNKNOWN_DEVICE
87+
88+
def desc_for_id(typeid):
89+
"""Translate integer type id to something more descriptive than the device name"""
90+
if typeid in Device._device_names:
91+
return Device._device_names[typeid][1]
92+
else:
93+
return Device.UNKNOWN_DEVICE
94+
7895
@property
7996
def _conn(self):
8097
return Device._instance.connections[self.port]
@@ -99,11 +116,21 @@ def _hat(self):
99116
def name(self):
100117
"""Determines name of device on port"""
101118
if self.connected == False:
102-
return "No device"
119+
return Device.DISCONNECTED_DEVICE
120+
elif self.typeidcur in self._device_names:
121+
return self._device_names[self.typeidcur][0]
122+
else:
123+
return Device.UNKNOWN_DEVICE
124+
125+
@property
126+
def description(self):
127+
"""Description of device on port"""
128+
if self.connected == False:
129+
return Device.DISCONNECTED_DEVICE
103130
elif self.typeidcur in self._device_names:
104-
return self._device_names[self.typeidcur]
131+
return self._device_names[self.typeidcur][1]
105132
else:
106-
return "Unknown"
133+
return Device.UNKNOWN_DEVICE
107134

108135
def isconnected(self):
109136
if not self.connected:

buildhat/hat.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ def __init__(self, device=None):
1414
Device._setup(device)
1515

1616
def get(self):
17-
"""Gets devices which are connected or disconnected
17+
"""Gets devices which are connected or disconnected
1818
1919
:return: Dictionary of devices
2020
:rtype: dict
2121
"""
2222
devices = {}
2323
for i in range(4):
24-
name = "Other"
24+
name = Device.UNKNOWN_DEVICE
2525
if Device._instance.connections[i].typeid in Device._device_names:
26-
name = Device._device_names[Device._instance.connections[i].typeid]
26+
name = Device._device_names[Device._instance.connections[i].typeid][0]
27+
desc = Device._device_names[Device._instance.connections[i].typeid][1]
2728
elif Device._instance.connections[i].typeid == -1:
28-
name = "Disconnected"
29+
name = Device.DISCONNECTED_DEVICE
30+
desc = ''
2931
devices[chr(ord('A')+i)] = {"typeid" : Device._instance.connections[i].typeid,
3032
"connected" : Device._instance.connections[i].connected,
31-
"name" : name}
33+
"name" : name,
34+
"description" : desc }
3235
return devices
3336

3437
def get_vin(self):

0 commit comments

Comments
 (0)