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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
__copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
import os
import wx
import threading
import sys
import time
import serial
from Cura.avr_isp import stk500v2
from Cura.avr_isp import ispBase
from Cura.avr_isp import intelHex
from Cura.gui.util import taskbar
from Cura.util import machineCom
from Cura.util import profile
from Cura.util import resources
def getDefaultFirmware(machineIndex = None):
if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker':
name = 'MarlinUltimaker'
if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
return None
if profile.getMachineSetting('has_heated_bed', machineIndex) == 'True':
name += '-HBK'
if sys.platform.startswith('linux'):
name += '-115200'
else:
name += '-250000'
if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 1:
name += '-dual'
return resources.getPathForFirmware(name + '.hex')
if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker_plus':
name = 'MarlinUltimaker-UMOP'
if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
return None
if sys.platform.startswith('linux'):
name += '-115200'
else:
name += '-250000'
if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 1:
name += '-dual'
return resources.getPathForFirmware(name + '.hex')
if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker2':
if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
return None
if profile.getMachineSettingFloat('extruder_amount', machineIndex) == 2:
return resources.getPathForFirmware("MarlinUltimaker2-dual.hex")
return resources.getPathForFirmware("MarlinUltimaker2.hex")
if profile.getMachineSetting('machine_type', machineIndex) == 'Witbox':
return resources.getPathForFirmware("MarlinWitbox.hex")
return None
class InstallFirmware(wx.Dialog):
def __init__(self, parent = None, filename = None, port = None, machineIndex = None):
super(InstallFirmware, self).__init__(parent=parent, title="Firmware install for %s" % (profile.getMachineSetting('machine_name', machineIndex).title()), size=(250, 100))
if port is None:
port = profile.getMachineSetting('serial_port')
if filename is None:
filename = getDefaultFirmware(machineIndex)
if filename is None:
wx.MessageBox(_("I am sorry, but Cura does not ship with a default firmware for your machine configuration."), _("Firmware update"), wx.OK | wx.ICON_ERROR)
self.Destroy()
return
self._machine_type = profile.getMachineSetting('machine_type', machineIndex)
if self._machine_type == 'reprap':
wx.MessageBox(_("Cura only supports firmware updates for ATMega2560 based hardware.\nSo updating your RepRap with Cura might or might not work."), _("Firmware update"), wx.OK | wx.ICON_INFORMATION)
sizer = wx.BoxSizer(wx.VERTICAL)
self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX\nX')
sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
self.progressGauge = wx.Gauge(self, -1)
sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
self.okButton = wx.Button(self, -1, _("OK"))
self.okButton.Disable()
self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
self.SetSizer(sizer)
self.filename = filename
self.port = port
self.Layout()
self.Fit()
self.thread = threading.Thread(target=self.OnRun)
self.thread.daemon = True
self.thread.start()
self.ShowModal()
self.Destroy()
return
def OnRun(self):
wx.CallAfter(self.updateLabel, _("Reading firmware..."))
hexFile = intelHex.readHex(self.filename)
wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
programmer = stk500v2.Stk500v2()
programmer.progressCallback = self.OnProgress
if self.port == 'AUTO':
wx.CallAfter(self.updateLabel, _("Please connect the printer to\nyour computer with the USB cable."))
while not programmer.isConnected():
for self.port in machineCom.serialList(True):
try:
programmer.connect(self.port)
break
except ispBase.IspError:
programmer.close()
time.sleep(1)
if not self:
#Window destroyed
return
else:
try:
programmer.connect(self.port)
except ispBase.IspError:
programmer.close()
if not programmer.isConnected():
wx.MessageBox(_("Failed to find machine for firmware upgrade\nIs your machine connected to the PC?"),
_("Firmware update"), wx.OK | wx.ICON_ERROR)
wx.CallAfter(self.Close)
return
if self._machine_type == 'ultimaker':
if programmer.hasChecksumFunction():
wx.CallAfter(self.updateLabel, _("Failed to install firmware:\nThis firmware is not compatible with this machine.\nTrying to install UMO firmware on an UM2 or UMO+?"))
programmer.close()
wx.CallAfter(self.okButton.Enable)
return
if self._machine_type == 'ultimaker_plus' or self._machine_type == 'ultimaker2':
if not programmer.hasChecksumFunction():
wx.CallAfter(self.updateLabel, _("Failed to install firmware:\nThis firmware is not compatible with this machine.\nTrying to install UM2 or UMO+ firmware on an UMO?"))
programmer.close()
wx.CallAfter(self.okButton.Enable)
return
wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
try:
programmer.programChip(hexFile)
wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
except ispBase.IspError as e:
wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
programmer.close()
wx.CallAfter(self.okButton.Enable)
def updateLabel(self, text):
self.progressLabel.SetLabel(text)
#self.Layout()
def OnProgress(self, value, max):
wx.CallAfter(self.progressGauge.SetRange, max)
wx.CallAfter(self.progressGauge.SetValue, value)
taskbar.setProgress(self.GetParent(), value, max)
def OnOk(self, e):
self.Close()
taskbar.setBusy(self.GetParent(), False)
def OnClose(self, e):
self.Destroy()
class AutoUpdateFirmware(wx.Dialog):
def __init__(self, parent, filename = None, port = None, machineIndex = None):
super(AutoUpdateFirmware, self).__init__(parent=parent, title="Auto Firmware install", size=(250, 500))
if port is None:
port = profile.getMachineSetting('serial_port')
self._serial = None
sizer = wx.BoxSizer(wx.VERTICAL)
self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX\nX')
sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
self.progressGauge = wx.Gauge(self, -1)
sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
self.okButton = wx.Button(self, -1, _("OK"))
self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
f = wx.Font(8, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
self._termLog = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_DONTWRAP)
self._termLog.SetFont(f)
self._termLog.SetEditable(0)
self._termLog.SetMinSize((1, 400))
self._termInput = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
self._termInput.SetFont(f)
sizer.Add(self._termLog, 0, flag=wx.ALIGN_CENTER|wx.ALL|wx.EXPAND)
sizer.Add(self._termInput, 0, flag=wx.ALIGN_CENTER|wx.ALL|wx.EXPAND)
self.Bind(wx.EVT_TEXT_ENTER, self.OnTermEnterLine, self._termInput)
self.SetSizer(sizer)
self.filename = filename
self.port = port
self.Layout()
self.Fit()
self.thread = threading.Thread(target=self.OnRun)
self.thread.daemon = True
self.thread.start()
self.read_thread = threading.Thread(target=self.OnSerialRead)
self.read_thread.daemon = True
self.read_thread.start()
self.ShowModal()
self.Destroy()
return
def _addTermLog(self, line):
if self._termLog is not None:
if len(self._termLog.GetValue()) > 10000:
self._termLog.SetValue(self._termLog.GetValue()[-10000:])
self._termLog.SetInsertionPointEnd()
if type(line) != unicode:
line = unicode(line, 'utf-8', 'replace')
self._termLog.AppendText(line.encode('utf-8', 'replace'))
def OnTermEnterLine(self, e):
lines = self._termInput.GetValue().split(';')
for line in lines:
if line == '':
continue
self._addTermLog('> %s\n' % (line))
if self._serial is not None:
self._serial.write(line + '\n')
def OnRun(self):
mtime = 0
while bool(self):
new_mtime = os.stat(self.filename).st_mtime
if mtime != new_mtime:
mtime = new_mtime
if self._serial is not None:
self._serial.close()
self._serial = None
time.sleep(0.5)
self.OnInstall()
try:
self._serial = serial.Serial(self.port, 250000)
except:
pass
time.sleep(0.5)
def OnSerialRead(self):
while bool(self):
if self._serial is None:
time.sleep(0.5)
else:
try:
line = self._serial.readline()
wx.CallAfter(self._addTermLog, line)
except:
pass
def OnInstall(self):
wx.CallAfter(self.okButton.Disable)
wx.CallAfter(self.updateLabel, _("Reading firmware..."))
hexFile = intelHex.readHex(self.filename)
wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
programmer = stk500v2.Stk500v2()
programmer.progressCallback = self.OnProgress
if self.port == 'AUTO':
wx.CallAfter(self.updateLabel, _("Please connect the printer to\nyour computer with the USB cable."))
while not programmer.isConnected():
for self.port in machineCom.serialList(True):
try:
programmer.connect(self.port)
break
except ispBase.IspError:
pass
time.sleep(1)
if not self:
#Window destroyed
return
else:
try:
programmer.connect(self.port)
except ispBase.IspError:
pass
if not programmer.isConnected():
wx.CallAfter(self.updateLabel, _("Failed to connect to programmer.\n"))
return
wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
try:
programmer.programChip(hexFile)
wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
except ispBase.IspError as e:
wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
programmer.close()
wx.CallAfter(self.okButton.Enable)
def updateLabel(self, text):
self.progressLabel.SetLabel(text)
#self.Layout()
def OnProgress(self, value, max):
wx.CallAfter(self.progressGauge.SetRange, max)
wx.CallAfter(self.progressGauge.SetValue, value)
taskbar.setProgress(self.GetParent(), value, max)
def OnOk(self, e):
self.Close()
taskbar.setBusy(self.GetParent(), False)
def OnClose(self, e):
self.Destroy()
|