Repository navigation
Expand file tree
/
Copy pathmachineInterface.py
More file actions
284 lines (242 loc) · 9.83 KB
/
Copy pathmachineInterface.py
File metadata and controls
284 lines (242 loc) · 9.83 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
########################################################
# Control Design GUI machine.py
# Reads and sets up datastructures for the machine's commands
# and parameters, and abstracts serial communication with the device.
#
# Ben Weiss, University of Washington
# Spring 2014
#
# NOTE: Machine.xml contains "Action" fields which are directly
# executed in the current Python context with exec. If this is evern
# used in a situation in which security matters, this practice should
# be changed!
#
# XML parsing tutorial used:
# http://www.blog.pythonlibrary.org/2013/04/30/python-101-intro-to-xml-parsing-with-elementtree/
#
# Other code snipped from:
# http://stackoverflow.com/questions/2175080/sscanf-in-python
#
# The MIT License (MIT)
#
# Copyright (c) 2014 Ben Weiss
#
# 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.
#
########################################################
import xml.etree.cElementTree as ET
from comm import *
import time
import re
import scripts
machine = None
# Machine can't currently create or delete params or cmds and have
# the results propigate back to the xml file.
class Machine :
# load a machine from an xml ElementTree root
def __init__(self, fname) :
self.name = ''
self.debugstr = ''
self.get_cmd = ''
self.set_cmd = ''
self.cmds = []
self.params = []
self.tabs = []
self.xmltree = ET.ElementTree(file=fname)
root = self.xmltree.getroot()
if root.tag != 'Machine' :
print("Could not parse XML. Root is not a 'Machine'")
return
if root.attrib.has_key('name') :
self.name = root.attrib['name']
if root.attrib.has_key('debugstr') :
self.debugstr = root.attrib['debugstr']
if root.attrib.has_key('getparam') :
self.get_cmd = root.attrib['getparam']
if root.attrib.has_key('setparam') :
self.set_cmd = root.attrib['setparam']
for child in root :
if child.tag == 'Commands' :
for cmd in child :
self.cmds.append(Command(cmd))
elif child.tag == 'Parameters' :
for param in child :
p = Param(param)
self.params.append(p)
try :
self.tabs.index(p.tab)
except ValueError :
self.tabs.append(p.tab)
# loads only settings from an xml file, not structure!
def loadSettings(self, fname) :
valuetree = ET.ElementTree(file=fname)
vroot = valuetree.getroot()
if vroot.tag != 'Machine' :
print('Invalid settings file!')
return
for child in vroot :
if child.tag == 'Parameters' :
for vparam in child :
if vparam.attrib.has_key('name') :
for param in self.params :
if param.name.lower() == vparam.attrib['name'].lower() :
param.value = vparam.text
param.default_value = vparam.text
# saves back the Machine structure to the xml file
def save(self, fname) :
root = self.xmltree.getroot()
root.set('name', self.name)
root.set('debugstr', self.debugstr)
root.set('getparam', self.get_cmd)
root.set('setparam', self.set_cmd)
for cmd in self.cmds :
cmd.updatexml()
for param in self.params :
param.updatexml()
self.xmltree.write(fname)
class Command :
def __init__(self) :
self.name = ''
self.cmd = ''
self.action = ''
self.xmlnode = None
def __init__(self, xmlnode) :
self.xmlnode = xmlnode
if xmlnode.tag != "Command" :
print("XML syntax error!")
return
if xmlnode.attrib.has_key('name') :
self.name = xmlnode.attrib['name']
if xmlnode.attrib.has_key('cmd') :
self.cmd = xmlnode.attrib['cmd']
self.action = xmlnode.text
# updates the xml object linked to this class in the hierarchy.
def updatexml(self) :
if self.xmlnode != None :
self.xmlnode.set('name', self.name)
self.xmlnode.set('cmd', self.cmd)
self.xmlnode.text = self.action
# Performs the action by sending the serial command
# to the device, then performing whatever action is desired.
def doAction(self) :
if comm.IsOpen() :
print ">" + self.cmd
comm.Write(self.cmd)
if self.action != '' and self.action != None :
exec self.action
class Param :
def __init__(self) :
self.name = ''
self.cmd = ''
self.readOnly = False
self.datatype = 'Int'
self.value = 0
self.default_value = 0
self.tab = ''
self.xmlnode = None
def __init__(self, xmlnode) :
self.xmlnode = xmlnode
if xmlnode.tag != 'Parameter' :
print("XML syntax error!")
return
if xmlnode.attrib.has_key('name') :
self.name = xmlnode.attrib['name']
if xmlnode.attrib.has_key('cmd') :
self.cmd = xmlnode.attrib['cmd']
if xmlnode.attrib.has_key('readonly') :
self.readOnly = xmlnode.attrib['readonly']
if xmlnode.attrib.has_key('type') :
self.datatype = xmlnode.attrib['type']
if xmlnode.attrib.has_key('tab') :
self.tab = xmlnode.attrib['tab']
self.value = xmlnode.text
self.default_value = xmlnode.text
def updatexml(self) :
if self.xmlnode != None :
self.xmlnode.set('name', self.name)
self.xmlnode.set('cmd', self.cmd)
self.xmlnode.set('readonly', str(self.readOnly))
self.xmlnode.set('type', self.datatype)
self.xmlnode.set('tab', self.tab)
self.xmlnode.text = str(self.value)
# Queries the device for the value of this parameter and updates self.value
def getFromDevice(self, quiet=False) :
if comm.IsOpen() :
if not quiet :
print ">" + machine.get_cmd + self.cmd
comm.Write(machine.get_cmd + self.cmd)
for i in range(0,15) : # read a few lines in case there's other dribble
line = comm.ReadLn()
#print '"' + line + '"'
if '' == line :
print "No return!"
return True
if line[0] != machine.debugstr :
r = re.compile("[ \t\n\r]+")
data = r.split(line)[0]
# cast data to the right form
try :
if self.datatype.lower() == "int" :
self.value = int(data)
elif self.datatype.lower() == "uint" :
self.value = int(data)
elif self.datatype.lower() == "float" :
self.value = float(data)
else : # assumed to be string, keep the whole line, sans white-space
self.value = line.strip()
return True
except ValueError :
if not quiet :
print("Error parseing response from chip! Tried to parse '%s' and failed. Whole line: %s" % (data, line))
return False
else :
print line,
return False
# Sets the value to the device
def setToDevice(self) :
if comm.IsOpen() :
print ">" + machine.set_cmd + self.cmd + ' ' + str(self.value)
comm.Write(machine.set_cmd + self.cmd + ' ' + str(self.value))
# Restores the value to default and sets the value to the device.
def restoreDefault(self) :
self.value = self.default_value
if comm.IsOpen() :
print ">" + machine.set_cmd + self.cmd + ' ' + str(self.value)
comm.Write(machine.set_cmd + self.cmd + ' ' + str(self.value))
# Validates the value newvalue, and if it is OK, stores it locally.
# Returns True for successful parse and store, False otherwise
def setValidValue(self, newvalue) :
try :
if self.datatype.lower() == "uint" :
self.value = abs(int(newvalue))
elif self.datatype.lower() == "int" :
self.value = int(newvalue)
elif self.datatype.lower() == "float" :
self.value = float(newvalue)
else :
self.value = newvalue
return True
except ValueError :
return False
# Loads a machine from a file
def loadMachine(fname) :
global machine
machine = Machine(fname)
if __name__ == "__main__" :
loadMachine('machine.xml')