1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
4 import os, wx, threading, sys
6 from Cura.avr_isp import stk500v2
7 from Cura.avr_isp import ispBase
8 from Cura.avr_isp import intelHex
10 from Cura.util import machineCom
11 from Cura.util import profile
12 from Cura.util import resources
14 def getDefaultFirmware():
15 if profile.getMachineSetting('machine_type') == 'ultimaker':
16 if profile.getMachineSetting('has_heated_bed') == 'True':
18 if profile.getMachineSettingFloat('extruder_amount') > 2:
20 if profile.getMachineSettingFloat('extruder_amount') > 1:
21 if sys.platform.startswith('linux'):
22 return resources.getPathForFirmware("MarlinUltimaker-115200-dual.hex")
24 return resources.getPathForFirmware("MarlinUltimaker-250000-dual.hex")
25 if sys.platform.startswith('linux'):
26 return resources.getPathForFirmware("MarlinUltimaker-115200.hex")
28 return resources.getPathForFirmware("MarlinUltimaker-250000.hex")
29 if profile.getMachineSetting('machine_type') == 'ultimaker2':
30 return resources.getPathForFirmware("MarlinUltimaker2.hex")
33 class InstallFirmware(wx.Dialog):
34 def __init__(self, filename = None, port = None):
35 super(InstallFirmware, self).__init__(parent=None, title="Firmware install", size=(250, 100))
37 port = profile.getMachineSetting('serial_port')
39 filename = getDefaultFirmware()
41 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)
45 sizer = wx.BoxSizer(wx.VERTICAL)
47 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX')
48 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER)
49 self.progressGauge = wx.Gauge(self, -1)
50 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
51 self.okButton = wx.Button(self, -1, _("OK"))
52 self.okButton.Disable()
53 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
54 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER)
57 self.filename = filename
63 threading.Thread(target=self.OnRun).start()
70 wx.CallAfter(self.updateLabel, _("Reading firmware..."))
71 hexFile = intelHex.readHex(self.filename)
72 wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
73 programmer = stk500v2.Stk500v2()
74 programmer.progressCallback = self.OnProgress
75 if self.port == 'AUTO':
76 for self.port in machineCom.serialList(True):
78 programmer.connect(self.port)
80 except ispBase.IspError:
84 programmer.connect(self.port)
85 except ispBase.IspError:
88 if programmer.isConnected():
89 wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
91 programmer.programChip(hexFile)
92 wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
93 except ispBase.IspError as e:
94 wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
97 wx.CallAfter(self.okButton.Enable)
99 wx.MessageBox(_("Failed to find machine for firmware upgrade\nIs your machine connected to the PC?"),
100 _("Firmware update"), wx.OK | wx.ICON_ERROR)
101 wx.CallAfter(self.Close)
103 def updateLabel(self, text):
104 self.progressLabel.SetLabel(text)
107 def OnProgress(self, value, max):
108 wx.CallAfter(self.progressGauge.SetRange, max)
109 wx.CallAfter(self.progressGauge.SetValue, value)
114 def OnClose(self, e):