1 from __future__ import absolute_import
3 import os, wx, threading, sys
5 from Cura.avr_isp import stk500v2
6 from Cura.avr_isp import ispBase
7 from Cura.avr_isp import intelHex
9 from Cura.util import machineCom
10 from Cura.util import profile
11 from Cura.util import resources
13 def getDefaultFirmware():
14 if profile.getPreference('machine_type') == 'ultimaker':
15 if profile.getPreference('has_heated_bed') == 'True':
17 if profile.getPreferenceFloat('extruder_amount') > 2:
19 if profile.getPreferenceFloat('extruder_amount') > 1:
20 if sys.platform.startswith('linux'):
21 return resources.getPathForFirmware("MarlinUltimaker-115200-dual.hex")
23 return resources.getPathForFirmware("MarlinUltimaker-250000-dual.hex")
24 if sys.platform.startswith('linux'):
25 return resources.getPathForFirmware("MarlinUltimaker-115200.hex")
27 return resources.getPathForFirmware("MarlinUltimaker-250000.hex")
30 class InstallFirmware(wx.Dialog):
31 def __init__(self, filename = None, port = None):
32 super(InstallFirmware, self).__init__(parent=None, title="Firmware install", size=(250, 100))
34 port = profile.getPreference('serial_port')
36 filename = getDefaultFirmware()
38 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)
42 sizer = wx.BoxSizer(wx.VERTICAL)
44 self.progressLabel = wx.StaticText(self, -1, 'Reading firmware...')
45 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER)
46 self.progressGauge = wx.Gauge(self, -1)
47 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
48 self.okButton = wx.Button(self, -1, 'Ok')
49 self.okButton.Disable()
50 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
51 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER)
54 self.filename = filename
57 threading.Thread(target=self.OnRun).start()
64 hexFile = intelHex.readHex(self.filename)
65 wx.CallAfter(self.updateLabel, "Connecting to machine...")
66 programmer = stk500v2.Stk500v2()
67 programmer.progressCallback = self.OnProgress
68 if self.port == 'AUTO':
69 for self.port in machineCom.serialList(True):
71 programmer.connect(self.port)
73 except ispBase.IspError:
77 programmer.connect(self.port)
78 except ispBase.IspError:
81 if programmer.isConnected():
82 wx.CallAfter(self.updateLabel, "Uploading firmware...")
84 programmer.programChip(hexFile)
85 wx.CallAfter(self.updateLabel, "Done!\nInstalled firmware: %s" % (os.path.basename(self.filename)))
86 except ispBase.IspError as e:
87 wx.CallAfter(self.updateLabel, "Failed to write firmware.\n" + str(e))
90 wx.CallAfter(self.okButton.Enable)
92 wx.MessageBox('Failed to find machine for firmware upgrade\nIs your machine connected to the PC?', 'Firmware update', wx.OK | wx.ICON_ERROR)
93 wx.CallAfter(self.Close)
95 def updateLabel(self, text):
96 self.progressLabel.SetLabel(text)
99 def OnProgress(self, value, max):
100 wx.CallAfter(self.progressGauge.SetRange, max)
101 wx.CallAfter(self.progressGauge.SetValue, value)
106 def OnClose(self, e):