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.util import machineCom
14 from Cura.util import profile
15 from Cura.util import resources
17 def getDefaultFirmware(machineIndex = None):
18 if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker':
19 name = 'MarlinUltimaker'
20 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
22 if profile.getMachineSetting('has_heated_bed', machineIndex) == 'True':
24 if sys.platform.startswith('linux'):
28 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 1:
30 return resources.getPathForFirmware(name + '.hex')
32 if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker_plus':
33 name = 'MarlinUltimaker-UMOP'
34 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
36 if sys.platform.startswith('linux'):
40 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 1:
42 return resources.getPathForFirmware(name + '.hex')
44 if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker2':
45 return resources.getPathForFirmware("MarlinUltimaker2.hex")
46 if profile.getMachineSetting('machine_type', machineIndex) == 'Witbox':
47 return resources.getPathForFirmware("MarlinWitbox.hex")
50 class InstallFirmware(wx.Dialog):
51 def __init__(self, filename = None, port = None, machineIndex = None):
52 super(InstallFirmware, self).__init__(parent=None, title="Firmware install for %s" % (profile.getMachineSetting('machine_name', machineIndex).title()), size=(250, 100))
54 port = profile.getMachineSetting('serial_port')
56 filename = getDefaultFirmware(machineIndex)
58 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)
61 self._machine_type = profile.getMachineSetting('machine_type', machineIndex)
62 if self._machine_type == 'reprap':
63 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)
65 sizer = wx.BoxSizer(wx.VERTICAL)
67 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX\nX')
68 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
69 self.progressGauge = wx.Gauge(self, -1)
70 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
71 self.okButton = wx.Button(self, -1, _("OK"))
72 self.okButton.Disable()
73 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
74 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
77 self.filename = filename
83 self.thread = threading.Thread(target=self.OnRun)
84 self.thread.daemon = True
92 wx.CallAfter(self.updateLabel, _("Reading firmware..."))
93 hexFile = intelHex.readHex(self.filename)
94 wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
95 programmer = stk500v2.Stk500v2()
96 programmer.progressCallback = self.OnProgress
97 if self.port == 'AUTO':
98 wx.CallAfter(self.updateLabel, _("Please connect the printer to\nyour computer with the USB cable."))
99 while not programmer.isConnected():
100 for self.port in machineCom.serialList(True):
102 programmer.connect(self.port)
104 except ispBase.IspError:
112 programmer.connect(self.port)
113 except ispBase.IspError:
116 if not programmer.isConnected():
117 wx.MessageBox(_("Failed to find machine for firmware upgrade\nIs your machine connected to the PC?"),
118 _("Firmware update"), wx.OK | wx.ICON_ERROR)
119 wx.CallAfter(self.Close)
122 if self._machine_type == 'ultimaker':
123 if programmer.hasChecksumFunction():
124 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+?"))
126 wx.CallAfter(self.okButton.Enable)
128 if self._machine_type == 'ultimaker_plus' or self._machine_type == 'ultimaker2':
129 if not programmer.hasChecksumFunction():
130 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?"))
132 wx.CallAfter(self.okButton.Enable)
135 wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
137 programmer.programChip(hexFile)
138 wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
139 except ispBase.IspError as e:
140 wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
143 wx.CallAfter(self.okButton.Enable)
145 def updateLabel(self, text):
146 self.progressLabel.SetLabel(text)
149 def OnProgress(self, value, max):
150 wx.CallAfter(self.progressGauge.SetRange, max)
151 wx.CallAfter(self.progressGauge.SetValue, value)
156 def OnClose(self, e):