1 from __future__ import absolute_import
\r
4 import wx, threading, re, subprocess, sys, os
\r
5 from wx.lib import buttons
\r
8 from gui import toolbarUtil
\r
9 from util import machineCom
\r
10 from util import profile
\r
11 from util import gcodeInterpreter
\r
13 printWindowMonitorHandle = None
\r
15 def printFile(filename):
\r
16 global printWindowMonitorHandle
\r
17 if printWindowMonitorHandle == None:
\r
18 printWindowMonitorHandle = printProcessMonitor()
\r
19 printWindowMonitorHandle.loadFile(filename)
\r
22 def startPrintInterface(filename):
\r
23 #startPrintInterface is called from the main script when we want the printer interface to run in a seperate process.
\r
24 # It needs to run in a seperate process, as any running python code blocks the GCode sender pyton code (http://wiki.python.org/moin/GlobalInterpreterLock).
\r
26 printWindowHandle = printWindow()
\r
27 printWindowHandle.Show(True)
\r
28 printWindowHandle.Raise()
\r
29 printWindowHandle.OnConnect(None)
\r
30 printWindowHandle.LoadGCodeFile(filename)
\r
33 class printProcessMonitor():
\r
37 def loadFile(self, filename):
\r
38 if self.handle == None:
\r
39 self.handle = subprocess.Popen([sys.executable, sys.argv[0], '-r', filename], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
\r
40 self.thread = threading.Thread(target=self.Monitor)
\r
43 self.handle.stdin.write(filename + '\n')
\r
47 line = p.stdout.readline()
\r
48 while(len(line) > 0):
\r
50 line = p.stdout.readline()
\r
55 class PrintCommandButton(buttons.GenBitmapButton):
\r
56 def __init__(self, parent, command, bitmapFilename, size=(20,20)):
\r
57 self.bitmap = toolbarUtil.getBitmapImage(bitmapFilename)
\r
58 super(PrintCommandButton, self).__init__(parent.directControlPanel, -1, self.bitmap, size=size)
\r
60 self.command = command
\r
61 self.parent = parent
\r
63 self.SetBezelWidth(1)
\r
64 self.SetUseFocusIndicator(False)
\r
66 self.Bind(wx.EVT_BUTTON, self.OnClick)
\r
68 def OnClick(self, e):
\r
69 self.parent.sendCommand("G91")
\r
70 self.parent.sendCommand(self.command)
\r
73 class printWindow(wx.Frame):
\r
74 "Main user interface window"
\r
76 super(printWindow, self).__init__(None, -1, title='Printing')
\r
77 self.machineCom = None
\r
78 self.machineConnected = False
\r
81 self.gcodeList = None
\r
83 self.printIdx = None
\r
86 self.bufferLineCount = 4
\r
88 self.feedrateRatioOuterWall = 1.0
\r
89 self.feedrateRatioInnerWall = 1.0
\r
90 self.feedrateRatioFill = 1.0
\r
91 self.feedrateRatioSupport = 1.0
\r
93 #self.SetIcon(icon.getMainIcon())
\r
95 self.SetSizer(wx.BoxSizer())
\r
96 self.panel = wx.Panel(self)
\r
97 self.GetSizer().Add(self.panel, 1, flag=wx.EXPAND)
\r
98 self.sizer = wx.GridBagSizer(2, 2)
\r
99 self.panel.SetSizer(self.sizer)
\r
101 sb = wx.StaticBox(self.panel, label="Statistics")
\r
102 boxsizer = wx.StaticBoxSizer(sb, wx.VERTICAL)
\r
103 self.statsText = wx.StaticText(self.panel, -1, "Filament: ####.##m #.##g\nPrint time: #####:##")
\r
104 boxsizer.Add(self.statsText, flag=wx.LEFT, border=5)
\r
106 self.sizer.Add(boxsizer, pos=(0,0), span=(4,1), flag=wx.EXPAND)
\r
108 self.connectButton = wx.Button(self.panel, -1, 'Connect')
\r
109 #self.loadButton = wx.Button(self.panel, -1, 'Load GCode')
\r
110 self.printButton = wx.Button(self.panel, -1, 'Print GCode')
\r
111 self.cancelButton = wx.Button(self.panel, -1, 'Cancel print')
\r
112 self.progress = wx.Gauge(self.panel, -1)
\r
114 self.sizer.Add(self.connectButton, pos=(0,1))
\r
115 #self.sizer.Add(self.loadButton, pos=(1,1))
\r
116 self.sizer.Add(self.printButton, pos=(2,1))
\r
117 self.sizer.Add(self.cancelButton, pos=(3,1))
\r
118 self.sizer.Add(self.progress, pos=(4,0), span=(1,2), flag=wx.EXPAND)
\r
120 nb = wx.Notebook(self.panel)
\r
121 self.sizer.Add(nb, pos=(0,3), span=(6,4))
\r
123 self.temperaturePanel = wx.Panel(nb)
\r
124 sizer = wx.GridBagSizer(2, 2)
\r
125 self.temperaturePanel.SetSizer(sizer)
\r
127 self.temperatureSelect = wx.SpinCtrl(self.temperaturePanel, -1, '0', size=(21*3,21), style=wx.SP_ARROW_KEYS)
\r
128 self.temperatureSelect.SetRange(0, 400)
\r
129 self.bedTemperatureLabel = wx.StaticText(self.temperaturePanel, -1, "BedTemp:")
\r
130 self.bedTemperatureSelect = wx.SpinCtrl(self.temperaturePanel, -1, '0', size=(21*3,21), style=wx.SP_ARROW_KEYS)
\r
131 self.bedTemperatureSelect.SetRange(0, 400)
\r
132 self.bedTemperatureLabel.Show(False)
\r
133 self.bedTemperatureSelect.Show(False)
\r
135 sizer.Add(wx.StaticText(self.temperaturePanel, -1, "Temp:"), pos=(0,0))
\r
136 sizer.Add(self.temperatureSelect, pos=(0,1))
\r
137 sizer.Add(self.bedTemperatureLabel, pos=(1,0))
\r
138 sizer.Add(self.bedTemperatureSelect, pos=(1,1))
\r
140 nb.AddPage(self.temperaturePanel, 'Temp')
\r
142 self.directControlPanel = wx.Panel(nb)
\r
144 sizer = wx.GridBagSizer(2, 2)
\r
145 self.directControlPanel.SetSizer(sizer)
\r
146 sizer.Add(PrintCommandButton(self, 'G1 Y100 F6000', 'print-move-y100.png'), pos=(0,3))
\r
147 sizer.Add(PrintCommandButton(self, 'G1 Y10 F6000', 'print-move-y10.png'), pos=(1,3))
\r
148 sizer.Add(PrintCommandButton(self, 'G1 Y1 F6000', 'print-move-y1.png'), pos=(2,3))
\r
150 sizer.Add(PrintCommandButton(self, 'G1 Y-1 F6000', 'print-move-y-1.png'), pos=(4,3))
\r
151 sizer.Add(PrintCommandButton(self, 'G1 Y-10 F6000', 'print-move-y-10.png'), pos=(5,3))
\r
152 sizer.Add(PrintCommandButton(self, 'G1 Y-100 F6000', 'print-move-y-100.png'), pos=(6,3))
\r
154 sizer.Add(PrintCommandButton(self, 'G1 X-100 F6000', 'print-move-x-100.png'), pos=(3,0))
\r
155 sizer.Add(PrintCommandButton(self, 'G1 X-10 F6000', 'print-move-x-10.png'), pos=(3,1))
\r
156 sizer.Add(PrintCommandButton(self, 'G1 X-1 F6000', 'print-move-x-1.png'), pos=(3,2))
\r
158 sizer.Add(PrintCommandButton(self, 'G28 X0 Y0', 'print-move-home.png'), pos=(3,3))
\r
160 sizer.Add(PrintCommandButton(self, 'G1 X1 F6000', 'print-move-x1.png'), pos=(3,4))
\r
161 sizer.Add(PrintCommandButton(self, 'G1 X10 F6000', 'print-move-x10.png'), pos=(3,5))
\r
162 sizer.Add(PrintCommandButton(self, 'G1 X100 F6000', 'print-move-x100.png'), pos=(3,6))
\r
164 sizer.Add(PrintCommandButton(self, 'G1 Z10 F200', 'print-move-z10.png'), pos=(0,7))
\r
165 sizer.Add(PrintCommandButton(self, 'G1 Z1 F200', 'print-move-z1.png'), pos=(1,7))
\r
166 sizer.Add(PrintCommandButton(self, 'G1 Z0.1 F200', 'print-move-z0.1.png'), pos=(2,7))
\r
168 sizer.Add(PrintCommandButton(self, 'G28 Z0', 'print-move-home.png'), pos=(3,7))
\r
170 sizer.Add(PrintCommandButton(self, 'G1 Z-0.1 F200', 'print-move-z-0.1.png'), pos=(4,7))
\r
171 sizer.Add(PrintCommandButton(self, 'G1 Z-1 F200', 'print-move-z-1.png'), pos=(5,7))
\r
172 sizer.Add(PrintCommandButton(self, 'G1 Z-10 F200', 'print-move-z-10.png'), pos=(6,7))
\r
174 nb.AddPage(self.directControlPanel, 'Jog')
\r
176 self.speedPanel = wx.Panel(nb)
\r
177 sizer = wx.GridBagSizer(2, 2)
\r
178 self.speedPanel.SetSizer(sizer)
\r
180 self.outerWallSpeedSelect = wx.SpinCtrl(self.speedPanel, -1, '100', size=(21*3,21), style=wx.SP_ARROW_KEYS)
\r
181 self.outerWallSpeedSelect.SetRange(5, 1000)
\r
182 self.innerWallSpeedSelect = wx.SpinCtrl(self.speedPanel, -1, '100', size=(21*3,21), style=wx.SP_ARROW_KEYS)
\r
183 self.innerWallSpeedSelect.SetRange(5, 1000)
\r
184 self.fillSpeedSelect = wx.SpinCtrl(self.speedPanel, -1, '100', size=(21*3,21), style=wx.SP_ARROW_KEYS)
\r
185 self.fillSpeedSelect.SetRange(5, 1000)
\r
186 self.supportSpeedSelect = wx.SpinCtrl(self.speedPanel, -1, '100', size=(21*3,21), style=wx.SP_ARROW_KEYS)
\r
187 self.supportSpeedSelect.SetRange(5, 1000)
\r
189 sizer.Add(wx.StaticText(self.speedPanel, -1, "Outer wall:"), pos=(0,0))
\r
190 sizer.Add(self.outerWallSpeedSelect, pos=(0,1))
\r
191 sizer.Add(wx.StaticText(self.speedPanel, -1, "%"), pos=(0,2))
\r
192 sizer.Add(wx.StaticText(self.speedPanel, -1, "Inner wall:"), pos=(1,0))
\r
193 sizer.Add(self.innerWallSpeedSelect, pos=(1,1))
\r
194 sizer.Add(wx.StaticText(self.speedPanel, -1, "%"), pos=(1,2))
\r
195 sizer.Add(wx.StaticText(self.speedPanel, -1, "Fill:"), pos=(2,0))
\r
196 sizer.Add(self.fillSpeedSelect, pos=(2,1))
\r
197 sizer.Add(wx.StaticText(self.speedPanel, -1, "%"), pos=(2,2))
\r
198 sizer.Add(wx.StaticText(self.speedPanel, -1, "Support:"), pos=(3,0))
\r
199 sizer.Add(self.supportSpeedSelect, pos=(3,1))
\r
200 sizer.Add(wx.StaticText(self.speedPanel, -1, "%"), pos=(3,2))
\r
202 nb.AddPage(self.speedPanel, 'Speed')
\r
204 self.sizer.AddGrowableRow(3)
\r
205 self.sizer.AddGrowableCol(0)
\r
207 self.Bind(wx.EVT_CLOSE, self.OnClose)
\r
208 self.connectButton.Bind(wx.EVT_BUTTON, self.OnConnect)
\r
209 #self.loadButton.Bind(wx.EVT_BUTTON, self.OnLoad)
\r
210 self.printButton.Bind(wx.EVT_BUTTON, self.OnPrint)
\r
211 self.cancelButton.Bind(wx.EVT_BUTTON, self.OnCancel)
\r
213 self.Bind(wx.EVT_SPINCTRL, self.OnTempChange, self.temperatureSelect)
\r
214 self.Bind(wx.EVT_SPINCTRL, self.OnBedTempChange, self.bedTemperatureSelect)
\r
216 self.Bind(wx.EVT_SPINCTRL, self.OnSpeedChange, self.outerWallSpeedSelect)
\r
217 self.Bind(wx.EVT_SPINCTRL, self.OnSpeedChange, self.innerWallSpeedSelect)
\r
218 self.Bind(wx.EVT_SPINCTRL, self.OnSpeedChange, self.fillSpeedSelect)
\r
219 self.Bind(wx.EVT_SPINCTRL, self.OnSpeedChange, self.supportSpeedSelect)
\r
225 self.UpdateButtonStates()
\r
226 self.UpdateProgress()
\r
228 def UpdateButtonStates(self):
\r
229 self.connectButton.Enable(not self.machineConnected)
\r
230 #self.loadButton.Enable(self.printIdx == None)
\r
231 self.printButton.Enable(self.machineConnected and self.gcodeList != None and self.printIdx == None)
\r
232 self.cancelButton.Enable(self.printIdx != None)
\r
233 self.temperatureSelect.Enable(self.machineConnected)
\r
234 self.directControlPanel.Enable(self.machineConnected)
\r
236 def UpdateProgress(self):
\r
238 if self.gcode != None:
\r
239 status += "Filament: %.2fm %.2fg\n" % (self.gcode.extrusionAmount / 1000, self.gcode.calculateWeight() * 1000)
\r
240 cost = self.gcode.calculateCost()
\r
242 status += "Filament cost: %s\n" % (cost)
\r
243 status += "Print time: %02d:%02d\n" % (int(self.gcode.totalMoveTimeMinute / 60), int(self.gcode.totalMoveTimeMinute % 60))
\r
244 if self.printIdx == None:
\r
245 self.progress.SetValue(0)
\r
246 if self.gcodeList != None:
\r
247 status += 'Line: -/%d\n' % (len(self.gcodeList))
\r
249 status += 'Line: %d/%d\n' % (self.printIdx, len(self.gcodeList))
\r
250 self.progress.SetValue(self.printIdx)
\r
251 if self.temp != None:
\r
252 status += 'Temp: %d\n' % (self.temp)
\r
253 if self.bedTemp != None and self.bedTemp > 0:
\r
254 status += 'Bed Temp: %d\n' % (self.bedTemp)
\r
255 self.bedTemperatureLabel.Show(True)
\r
256 self.bedTemperatureSelect.Show(True)
\r
257 self.temperaturePanel.Layout()
\r
258 self.statsText.SetLabel(status.strip())
\r
261 def OnConnect(self, e):
\r
262 if self.machineCom != None:
\r
263 self.machineCom.close()
\r
265 self.machineCom = machineCom.MachineCom()
\r
266 self.thread = threading.Thread(target=self.PrinterMonitor)
\r
267 self.thread.start()
\r
268 self.UpdateButtonStates()
\r
270 def OnLoad(self, e):
\r
273 def OnPrint(self, e):
\r
274 if not self.machineConnected:
\r
276 if self.gcodeList == None:
\r
278 if self.printIdx != None:
\r
282 self.sendCnt = self.bufferLineCount
\r
283 self.UpdateButtonStates()
\r
285 def OnCancel(self, e):
\r
286 self.printIdx = None
\r
287 self.sendCommand("M84")
\r
288 self.UpdateButtonStates()
\r
290 def OnClose(self, e):
\r
291 global printWindowHandle
\r
292 printWindowHandle = None
\r
293 if self.machineCom != None:
\r
294 self.machineCom.close()
\r
298 def OnTempChange(self, e):
\r
299 self.sendCommand("M104 S%d" % (self.temperatureSelect.GetValue()))
\r
301 def OnBedTempChange(self, e):
\r
302 self.sendCommand("M140 S%d" % (self.bedTemperatureSelect.GetValue()))
\r
304 def OnSpeedChange(self, e):
\r
305 self.feedrateRatioOuterWall = self.outerWallSpeedSelect.GetValue() / 100.0
\r
306 self.feedrateRatioInnerWall = self.innerWallSpeedSelect.GetValue() / 100.0
\r
307 self.feedrateRatioFill = self.fillSpeedSelect.GetValue() / 100.0
\r
308 self.feedrateRatioSupport = self.supportSpeedSelect.GetValue() / 100.0
\r
310 def LoadGCodeFile(self, filename):
\r
311 if self.printIdx != None:
\r
313 #Send an initial M110 to reset the line counter to zero.
\r
314 lineType = 'CUSTOM'
\r
315 gcodeList = ["M110"]
\r
316 typeList = [lineType]
\r
317 for line in open(filename, 'r'):
\r
318 if line.startswith(';TYPE:'):
\r
319 lineType = line[6:].strip()
\r
321 line = line[0:line.find(';')]
\r
322 line = line.strip()
\r
324 gcodeList.append(line)
\r
325 typeList.append(lineType)
\r
326 gcode = gcodeInterpreter.gcode()
\r
327 gcode.loadList(gcodeList)
\r
328 print "Loaded: %s (%d)" % (filename, len(gcodeList))
\r
329 self.progress.SetRange(len(gcodeList))
\r
331 self.gcodeList = gcodeList
\r
332 self.typeList = typeList
\r
333 self.UpdateButtonStates()
\r
334 self.UpdateProgress()
\r
336 def sendCommand(self, cmd):
\r
337 if self.machineConnected:
\r
338 if self.printIdx == None:
\r
339 self.machineCom.sendCommand(cmd)
\r
341 self.sendList.append(cmd)
\r
343 def sendLine(self, lineNr):
\r
344 if lineNr >= len(self.gcodeList):
\r
346 line = self.gcodeList[lineNr]
\r
347 if self.typeList[lineNr] == 'WALL-OUTER':
\r
348 line = re.sub('F([0-9]*)', lambda m: 'F' + str(int(int(m.group(1)) * self.feedrateRatioOuterWall)), line)
\r
349 if self.typeList[lineNr] == 'WALL-INNER':
\r
351 line = re.sub('F([0-9]*)', lambda m: 'F' + str(int(int(m.group(1)) * self.feedrateRatioInnerWall)), line)
\r
353 if self.typeList[lineNr] == 'FILL':
\r
354 line = re.sub('F([0-9]*)', lambda m: 'F' + str(int(int(m.group(1)) * self.feedrateRatioFill)), line)
\r
355 if self.typeList[lineNr] == 'SUPPORT':
\r
356 line = re.sub('F([0-9]*)', lambda m: 'F' + str(int(int(m.group(1)) * self.feedrateRatioSupport)), line)
\r
357 checksum = reduce(lambda x,y:x^y, map(ord, "N%d%s" % (lineNr, line)))
\r
358 self.machineCom.sendCommand("N%d%s*%d" % (lineNr, line, checksum))
\r
361 def PrinterMonitor(self):
\r
363 line = self.machineCom.readline()
\r
365 self.machineConnected = False
\r
366 wx.CallAfter(self.UpdateButtonStates)
\r
368 if self.machineConnected:
\r
369 while self.sendCnt > 0:
\r
370 self.sendLine(self.printIdx)
\r
373 elif line.startswith("start"):
\r
374 self.machineConnected = True
\r
375 wx.CallAfter(self.UpdateButtonStates)
\r
377 self.temp = float(re.search("[0-9\.]*", line.split('T:')[1]).group(0))
\r
379 self.bedTemp = float(re.search("[0-9\.]*", line.split('B:')[1]).group(0))
\r
380 wx.CallAfter(self.UpdateProgress)
\r
381 if self.printIdx == None:
\r
382 if line == '': #When we have a communication "timeout" and we're not sending gcode, then read the temperature.
\r
383 self.machineCom.sendCommand("M105")
\r
385 if line.startswith("ok"):
\r
386 if len(self.sendList) > 0:
\r
387 self.machineCom.sendCommand(self.sendList.pop(0))
\r
389 if self.sendLine(self.printIdx):
\r
392 self.printIdx = None
\r
393 wx.CallAfter(self.UpdateButtonStates)
\r
394 wx.CallAfter(self.UpdateProgress)
\r
395 elif "resend" in line.lower() or "rs" in line:
\r
397 lineNr=int(line.replace("N:"," ").replace("N"," ").replace(":"," ").split()[-1])
\r
400 lineNr=int(line.split()[1])
\r
401 self.printIdx = lineNr
\r
402 #we should actually resend the line here, but we also get an "ok" for each error from Marlin. And thus we'll resend on the OK.
\r