@@ -10,31 +10,34 @@ class Device:
10
10
"""Creates a single instance of the buildhat for all devices to use"""
11
11
_instance = None
12
12
_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
31
31
}
32
32
_used = { 0 : False ,
33
33
1 : False ,
34
34
2 : False ,
35
35
3 : False
36
36
}
37
-
37
+
38
+ UNKNOWN_DEVICE = "Unknown"
39
+ DISCONNECTED_DEVICE = "Disconnected"
40
+
38
41
def __init__ (self , port ):
39
42
if not isinstance (port , str ) or len (port ) != 1 :
40
43
raise DeviceNotFound ("Invalid port" )
@@ -48,7 +51,7 @@ def __init__(self, port):
48
51
self ._simplemode = - 1
49
52
self ._combimode = - 1
50
53
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 :
52
55
raise DeviceInvalid ('There is not a {} connected to port {} (Found {})' .format (type (self ).__name__ , port , self .name ))
53
56
Device ._used [p ] = True
54
57
@@ -67,14 +70,28 @@ def _setup(device="/dev/serial0"):
67
70
68
71
def __del__ (self ):
69
72
if hasattr (self , "port" ) and Device ._used [self .port ]:
70
- if self ._typeid == 64 :
73
+ if Device . _device_names [ self ._typeid ][ 0 ] == "Matrix" :
71
74
self .clear ()
72
75
Device ._used [self .port ] = False
73
76
self ._conn .callit = None
74
77
self .deselect ()
75
- if self ._typeid != 64 :
78
+ if Device . _device_names [ self ._typeid ][ 0 ] != "Matrix" :
76
79
self .off ()
77
80
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
+
78
95
@property
79
96
def _conn (self ):
80
97
return Device ._instance .connections [self .port ]
@@ -99,11 +116,21 @@ def _hat(self):
99
116
def name (self ):
100
117
"""Determines name of device on port"""
101
118
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
103
130
elif self .typeidcur in self ._device_names :
104
- return self ._device_names [self .typeidcur ]
131
+ return self ._device_names [self .typeidcur ][ 1 ]
105
132
else :
106
- return "Unknown"
133
+ return Device . UNKNOWN_DEVICE
107
134
108
135
def isconnected (self ):
109
136
if not self .connected :
0 commit comments