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.getPreference('machine_type') == 'ultimaker':
16 if profile.getPreference('has_heated_bed') == 'True':
18 if profile.getPreferenceFloat('extruder_amount') > 2:
20 if profile.getPreferenceFloat('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")
31 class InstallFirmware(wx.Dialog):
32 def __init__(self, filename = None, port = None):
33 super(InstallFirmware, self).__init__(parent=None, title="Firmware install", size=(250, 100))
35 port = profile.getPreference('serial_port')
37 filename = getDefaultFirmware()
39 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)
43 sizer = wx.BoxSizer(wx.VERTICAL)
45 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX')
46 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER)
47 self.progressGauge = wx.Gauge(self, -1)
48 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
49 self.okButton = wx.Button(self, -1, 'Ok')
50 self.okButton.Disable()
51 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
52 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER)
55 self.filename = filename
61 threading.Thread(target=self.OnRun).start()
68 wx.CallAfter(self.updateLabel, "Reading firmware...")
69 hexFile = intelHex.readHex(self.filename)
70 wx.CallAfter(self.updateLabel, "Connecting to machine...")
71 programmer = stk500v2.Stk500v2()
72 programmer.progressCallback = self.OnProgress
73 if self.port == 'AUTO':
74 for self.port in machineCom.serialList(True):
76 programmer.connect(self.port)
78 except ispBase.IspError:
82 programmer.connect(self.port)
83 except ispBase.IspError:
86 if programmer.isConnected():
87 wx.CallAfter(self.updateLabel, "Uploading firmware...")
89 programmer.programChip(hexFile)
90 wx.CallAfter(self.updateLabel, "Done!\nInstalled firmware: %s" % (os.path.basename(self.filename)))
91 except ispBase.IspError as e:
92 wx.CallAfter(self.updateLabel, "Failed to write firmware.\n" + str(e))
95 wx.CallAfter(self.okButton.Enable)
97 wx.MessageBox('Failed to find machine for firmware upgrade\nIs your machine connected to the PC?', 'Firmware update', wx.OK | wx.ICON_ERROR)
98 wx.CallAfter(self.Close)
100 def updateLabel(self, text):
101 self.progressLabel.SetLabel(text)
104 def OnProgress(self, value, max):
105 wx.CallAfter(self.progressGauge.SetRange, max)
106 wx.CallAfter(self.progressGauge.SetValue, value)
111 def OnClose(self, e):