chiark / gitweb /
Add UM2 firmware version 13.10.
[cura.git] / Cura / gui / firmwareInstall.py
1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
3
4 import os, wx, threading, sys
5
6 from Cura.avr_isp import stk500v2
7 from Cura.avr_isp import ispBase
8 from Cura.avr_isp import intelHex
9
10 from Cura.util import machineCom
11 from Cura.util import profile
12 from Cura.util import resources
13
14 def getDefaultFirmware():
15         if profile.getMachineSetting('machine_type') == 'ultimaker':
16                 if profile.getMachineSetting('has_heated_bed') == 'True':
17                         return None
18                 if profile.getMachineSettingFloat('extruder_amount') > 2:
19                         return None
20                 if profile.getMachineSettingFloat('extruder_amount') > 1:
21                         if sys.platform.startswith('linux'):
22                                 return resources.getPathForFirmware("MarlinUltimaker-115200-dual.hex")
23                         else:
24                                 return resources.getPathForFirmware("MarlinUltimaker-250000-dual.hex")
25                 if sys.platform.startswith('linux'):
26                         return resources.getPathForFirmware("MarlinUltimaker-115200.hex")
27                 else:
28                         return resources.getPathForFirmware("MarlinUltimaker-250000.hex")
29         if profile.getMachineSetting('machine_type') == 'ultimaker2':
30                 return resources.getPathForFirmware("MarlinUltimaker2.hex")
31         return None
32
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))
36                 if port is None:
37                         port = profile.getMachineSetting('serial_port')
38                 if filename is None:
39                         filename = getDefaultFirmware()
40                 if filename is None:
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)
42                         self.Destroy()
43                         return
44
45                 sizer = wx.BoxSizer(wx.VERTICAL)
46
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)
55                 self.SetSizer(sizer)
56
57                 self.filename = filename
58                 self.port = port
59
60                 self.Layout()
61                 self.Fit()
62
63                 threading.Thread(target=self.OnRun).start()
64
65                 self.ShowModal()
66                 self.Destroy()
67                 return
68
69         def OnRun(self):
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):
77                                 try:
78                                         programmer.connect(self.port)
79                                         break
80                                 except ispBase.IspError:
81                                         pass
82                 else:
83                         try:
84                                 programmer.connect(self.port)
85                         except ispBase.IspError:
86                                 pass
87
88                 if programmer.isConnected():
89                         wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
90                         try:
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))
95
96                         programmer.close()
97                         wx.CallAfter(self.okButton.Enable)
98                         return
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)
102
103         def updateLabel(self, text):
104                 self.progressLabel.SetLabel(text)
105                 #self.Layout()
106
107         def OnProgress(self, value, max):
108                 wx.CallAfter(self.progressGauge.SetRange, max)
109                 wx.CallAfter(self.progressGauge.SetValue, value)
110
111         def OnOk(self, e):
112                 self.Close()
113
114         def OnClose(self, e):
115                 self.Destroy()
116