1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
9 from Cura.avr_isp import stk500v2
10 from Cura.avr_isp import ispBase
11 from Cura.avr_isp import intelHex
13 from Cura.gui.util import taskbar
14 from Cura.util import machineCom
15 from Cura.util import profile
16 from Cura.util import resources
18 def getDefaultFirmware(machineIndex = None):
19 if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker':
20 name = 'MarlinUltimaker'
21 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
23 if profile.getMachineSetting('has_heated_bed', machineIndex) == 'True':
25 if sys.platform.startswith('linux'):
29 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 1:
31 return resources.getPathForFirmware(name + '.hex')
33 if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker_plus':
34 name = 'MarlinUltimaker-UMOP'
35 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
37 if sys.platform.startswith('linux'):
41 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 1:
43 return resources.getPathForFirmware(name + '.hex')
45 if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker2':
46 return resources.getPathForFirmware("MarlinUltimaker2.hex")
47 if profile.getMachineSetting('machine_type', machineIndex) == 'Witbox':
48 return resources.getPathForFirmware("MarlinWitbox.hex")
51 class InstallFirmware(wx.Dialog):
52 def __init__(self, parent = None, filename = None, port = None, machineIndex = None):
53 super(InstallFirmware, self).__init__(parent=parent, title="Firmware install for %s" % (profile.getMachineSetting('machine_name', machineIndex).title()), size=(250, 100))
55 port = profile.getMachineSetting('serial_port')
57 filename = getDefaultFirmware(machineIndex)
59 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)
62 self._machine_type = profile.getMachineSetting('machine_type', machineIndex)
63 if self._machine_type == 'reprap':
64 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)
66 sizer = wx.BoxSizer(wx.VERTICAL)
68 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX\nX')
69 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
70 self.progressGauge = wx.Gauge(self, -1)
71 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
72 self.okButton = wx.Button(self, -1, _("OK"))
73 self.okButton.Disable()
74 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
75 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
78 self.filename = filename
84 self.thread = threading.Thread(target=self.OnRun)
85 self.thread.daemon = True
93 wx.CallAfter(self.updateLabel, _("Reading firmware..."))
94 hexFile = intelHex.readHex(self.filename)
95 wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
96 programmer = stk500v2.Stk500v2()
97 programmer.progressCallback = self.OnProgress
98 if self.port == 'AUTO':
99 wx.CallAfter(self.updateLabel, _("Please connect the printer to\nyour computer with the USB cable."))
100 while not programmer.isConnected():
101 for self.port in machineCom.serialList(True):
103 programmer.connect(self.port)
105 except ispBase.IspError:
113 programmer.connect(self.port)
114 except ispBase.IspError:
117 if not programmer.isConnected():
118 wx.MessageBox(_("Failed to find machine for firmware upgrade\nIs your machine connected to the PC?"),
119 _("Firmware update"), wx.OK | wx.ICON_ERROR)
120 wx.CallAfter(self.Close)
123 if self._machine_type == 'ultimaker':
124 if programmer.hasChecksumFunction():
125 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+?"))
127 wx.CallAfter(self.okButton.Enable)
129 if self._machine_type == 'ultimaker_plus' or self._machine_type == 'ultimaker2':
130 if not programmer.hasChecksumFunction():
131 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?"))
133 wx.CallAfter(self.okButton.Enable)
136 wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
138 programmer.programChip(hexFile)
139 wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
140 except ispBase.IspError as e:
141 wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
144 wx.CallAfter(self.okButton.Enable)
146 def updateLabel(self, text):
147 self.progressLabel.SetLabel(text)
150 def OnProgress(self, value, max):
151 wx.CallAfter(self.progressGauge.SetRange, max)
152 wx.CallAfter(self.progressGauge.SetValue, value)
153 taskbar.setProgress(self.GetParent(), value, max)
157 taskbar.setBusy(self.GetParent(), False)
159 def OnClose(self, e):
163 class AutoUpdateFirmware(wx.Dialog):
164 def __init__(self, parent, filename = None, port = None, machineIndex = None):
165 super(AutoUpdateFirmware, self).__init__(parent=parent, title="Auto Firmware install", size=(250, 100))
167 port = profile.getMachineSetting('serial_port')
169 sizer = wx.BoxSizer(wx.VERTICAL)
171 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX\nX')
172 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
173 self.progressGauge = wx.Gauge(self, -1)
174 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
175 self.okButton = wx.Button(self, -1, _("OK"))
176 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
177 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
180 self.filename = filename
186 self.thread = threading.Thread(target=self.OnRun)
187 self.thread.daemon = True
197 new_mtime = os.stat(self.filename).st_mtime
198 if mtime != new_mtime:
205 wx.CallAfter(self.okButton.Disable)
206 wx.CallAfter(self.updateLabel, _("Reading firmware..."))
207 hexFile = intelHex.readHex(self.filename)
208 wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
209 programmer = stk500v2.Stk500v2()
210 programmer.progressCallback = self.OnProgress
211 if self.port == 'AUTO':
212 wx.CallAfter(self.updateLabel, _("Please connect the printer to\nyour computer with the USB cable."))
213 while not programmer.isConnected():
214 for self.port in machineCom.serialList(True):
216 programmer.connect(self.port)
218 except ispBase.IspError:
226 programmer.connect(self.port)
227 except ispBase.IspError:
230 if not programmer.isConnected():
231 wx.CallAfter(self.updateLabel, _("Failed to connect to programmer.\n"))
234 wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
236 programmer.programChip(hexFile)
237 wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
238 except ispBase.IspError as e:
239 wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
242 wx.CallAfter(self.okButton.Enable)
244 def updateLabel(self, text):
245 self.progressLabel.SetLabel(text)
248 def OnProgress(self, value, max):
249 wx.CallAfter(self.progressGauge.SetRange, max)
250 wx.CallAfter(self.progressGauge.SetValue, value)
251 taskbar.setProgress(self.GetParent(), value, max)
255 taskbar.setBusy(self.GetParent(), False)
257 def OnClose(self, e):