chiark / gitweb /
Add new firmware update for UM2. Show which firmware you are installing in the firmwa...
[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
5 import wx
6 import threading
7 import sys
8 import time
9
10 from Cura.avr_isp import stk500v2
11 from Cura.avr_isp import ispBase
12 from Cura.avr_isp import intelHex
13
14 from Cura.util import machineCom
15 from Cura.util import profile
16 from Cura.util import resources
17
18 def getDefaultFirmware():
19         if profile.getMachineSetting('machine_type') == 'ultimaker':
20                 if profile.getMachineSetting('has_heated_bed') == 'True':
21                         return None
22                 if profile.getMachineSettingFloat('extruder_amount') > 2:
23                         return None
24                 if profile.getMachineSettingFloat('extruder_amount') > 1:
25                         if sys.platform.startswith('linux'):
26                                 return resources.getPathForFirmware("MarlinUltimaker-115200-dual.hex")
27                         else:
28                                 return resources.getPathForFirmware("MarlinUltimaker-250000-dual.hex")
29                 if sys.platform.startswith('linux'):
30                         return resources.getPathForFirmware("MarlinUltimaker-115200.hex")
31                 else:
32                         return resources.getPathForFirmware("MarlinUltimaker-250000.hex")
33         if profile.getMachineSetting('machine_type') == 'ultimaker2':
34                 return resources.getPathForFirmware("MarlinUltimaker2.hex")
35         return None
36
37 class InstallFirmware(wx.Dialog):
38         def __init__(self, filename = None, port = None):
39                 super(InstallFirmware, self).__init__(parent=None, title="Firmware install for %s" % (profile.getMachineSetting('machine_name').title()), size=(250, 100))
40                 if port is None:
41                         port = profile.getMachineSetting('serial_port')
42                 if filename is None:
43                         filename = getDefaultFirmware()
44                 if filename is None:
45                         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)
46                         self.Destroy()
47                         return
48                 if profile.getMachineSetting('machine_type') == 'reprap':
49                         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)
50
51                 sizer = wx.BoxSizer(wx.VERTICAL)
52
53                 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX')
54                 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
55                 self.progressGauge = wx.Gauge(self, -1)
56                 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
57                 self.okButton = wx.Button(self, -1, _("OK"))
58                 self.okButton.Disable()
59                 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
60                 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
61                 self.SetSizer(sizer)
62
63                 self.filename = filename
64                 self.port = port
65
66                 self.Layout()
67                 self.Fit()
68
69                 self.thread = threading.Thread(target=self.OnRun)
70                 self.thread.daemon = True
71                 self.thread.start()
72
73                 self.ShowModal()
74                 self.Destroy()
75                 return
76
77         def OnRun(self):
78                 wx.CallAfter(self.updateLabel, _("Reading firmware..."))
79                 hexFile = intelHex.readHex(self.filename)
80                 wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
81                 programmer = stk500v2.Stk500v2()
82                 programmer.progressCallback = self.OnProgress
83                 if self.port == 'AUTO':
84                         wx.CallAfter(self.updateLabel, _("Please connect the printer to\nyour computer with the USB cable."))
85                         while not programmer.isConnected():
86                                 for self.port in machineCom.serialList(True):
87                                         try:
88                                                 programmer.connect(self.port)
89                                                 break
90                                         except ispBase.IspError:
91                                                 pass
92                                 time.sleep(1)
93                                 if not self:
94                                         #Window destroyed
95                                         return
96                 else:
97                         try:
98                                 programmer.connect(self.port)
99                         except ispBase.IspError:
100                                 pass
101
102                 if not programmer.isConnected():
103                         wx.MessageBox(_("Failed to find machine for firmware upgrade\nIs your machine connected to the PC?"),
104                                                   _("Firmware update"), wx.OK | wx.ICON_ERROR)
105                         wx.CallAfter(self.Close)
106                         return
107
108                 wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
109                 try:
110                         programmer.programChip(hexFile)
111                         wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
112                 except ispBase.IspError as e:
113                         wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
114
115                 programmer.close()
116                 wx.CallAfter(self.okButton.Enable)
117
118         def updateLabel(self, text):
119                 self.progressLabel.SetLabel(text)
120                 #self.Layout()
121
122         def OnProgress(self, value, max):
123                 wx.CallAfter(self.progressGauge.SetRange, max)
124                 wx.CallAfter(self.progressGauge.SetValue, value)
125
126         def OnOk(self, e):
127                 self.Close()
128
129         def OnClose(self, e):
130                 self.Destroy()
131