chiark / gitweb /
Prevent installing UM2 or UMO+ firmware on an UMO. And the other way around. Also...
[cura.git] / Cura / gui / firmwareInstall.py
1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2
3 import os
4 import wx
5 import threading
6 import sys
7 import time
8
9 from Cura.avr_isp import stk500v2
10 from Cura.avr_isp import ispBase
11 from Cura.avr_isp import intelHex
12
13 from Cura.util import machineCom
14 from Cura.util import profile
15 from Cura.util import resources
16
17 def getDefaultFirmware(machineIndex = None):
18         if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker':
19                 name = 'MarlinUltimaker'
20                 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
21                         return None
22                 if profile.getMachineSetting('has_heated_bed', machineIndex) == 'True':
23                         name += '-HBK'
24                 if sys.platform.startswith('linux'):
25                         name += '-115200'
26                 else:
27                         name += '-250000'
28                 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 1:
29                         name += '-dual'
30                 return resources.getPathForFirmware(name + '.hex')
31
32         if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker_plus':
33                 name = 'MarlinUltimaker-UMOP'
34                 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 2:
35                         return None
36                 if sys.platform.startswith('linux'):
37                         name += '-115200'
38                 else:
39                         name += '-250000'
40                 if profile.getMachineSettingFloat('extruder_amount', machineIndex) > 1:
41                         name += '-dual'
42                 return resources.getPathForFirmware(name + '.hex')
43
44         if profile.getMachineSetting('machine_type', machineIndex) == 'ultimaker2':
45                 return resources.getPathForFirmware("MarlinUltimaker2.hex")
46         if profile.getMachineSetting('machine_type', machineIndex) == 'lulzbot_mini':
47                 return resources.getPathForFirmware("marlin_mini_2014Q4.hex")
48         if profile.getMachineSetting('machine_type', machineIndex) == 'Witbox':
49                 return resources.getPathForFirmware("MarlinWitbox.hex")
50         return None
51
52 class InstallFirmware(wx.Dialog):
53         def __init__(self, filename = None, port = None, machineIndex = None):
54                 super(InstallFirmware, self).__init__(parent=None, title="Firmware install for %s" % (profile.getMachineSetting('machine_name', machineIndex).title()), size=(250, 100))
55                 if port is None:
56                         port = profile.getMachineSetting('serial_port')
57                 if filename is None:
58                         filename = getDefaultFirmware(machineIndex)
59                 if filename is None:
60                         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)
61                         self.Destroy()
62                         return
63                 self._machine_type = profile.getMachineSetting('machine_type', machineIndex)
64                 if self._machine_type == 'reprap':
65                         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)
66
67                 sizer = wx.BoxSizer(wx.VERTICAL)
68
69                 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX\nX')
70                 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
71                 self.progressGauge = wx.Gauge(self, -1)
72                 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
73                 self.okButton = wx.Button(self, -1, _("OK"))
74                 self.okButton.Disable()
75                 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
76                 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
77                 self.SetSizer(sizer)
78
79                 self.filename = filename
80                 self.port = port
81
82                 self.Layout()
83                 self.Fit()
84
85                 self.thread = threading.Thread(target=self.OnRun)
86                 self.thread.daemon = True
87                 self.thread.start()
88
89                 self.ShowModal()
90                 self.Destroy()
91                 return
92
93         def OnRun(self):
94                 wx.CallAfter(self.updateLabel, _("Reading firmware..."))
95                 hexFile = intelHex.readHex(self.filename)
96                 wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
97                 programmer = stk500v2.Stk500v2()
98                 programmer.progressCallback = self.OnProgress
99                 if self.port == 'AUTO':
100                         wx.CallAfter(self.updateLabel, _("Please connect the printer to\nyour computer with the USB cable."))
101                         while not programmer.isConnected():
102                                 for self.port in machineCom.serialList(True):
103                                         try:
104                                                 programmer.connect(self.port)
105                                                 break
106                                         except ispBase.IspError:
107                                                 pass
108                                 time.sleep(1)
109                                 if not self:
110                                         #Window destroyed
111                                         return
112                 else:
113                         try:
114                                 programmer.connect(self.port)
115                         except ispBase.IspError:
116                                 pass
117
118                 if not programmer.isConnected():
119                         wx.MessageBox(_("Failed to find machine for firmware upgrade\nIs your machine connected to the PC?"),
120                                                   _("Firmware update"), wx.OK | wx.ICON_ERROR)
121                         wx.CallAfter(self.Close)
122                         return
123
124                 if self._machine_type == 'ultimaker':
125                         if programmer.hasChecksumFunction():
126                                 wx.CallAfter(self.updateLabel, _("Failed to install firmware:\nThis firmware is not compatible with this machine.\nTrying to install UMO firmware on an UM2 or UMO+?"))
127                                 programmer.close()
128                                 wx.CallAfter(self.okButton.Enable)
129                                 return
130                 if self._machine_type == 'ultimaker_plus' or self._machine_type == 'ultimaker2':
131                         if not programmer.hasChecksumFunction():
132                                 wx.CallAfter(self.updateLabel, _("Failed to install firmware:\nThis firmware is not compatible with this machine.\nTrying to install UM2 or UMO+ firmware on an UMO?"))
133                                 programmer.close()
134                                 wx.CallAfter(self.okButton.Enable)
135                                 return
136
137                 wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
138                 try:
139                         programmer.programChip(hexFile)
140                         wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
141                 except ispBase.IspError as e:
142                         wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
143
144                 programmer.close()
145                 wx.CallAfter(self.okButton.Enable)
146
147         def updateLabel(self, text):
148                 self.progressLabel.SetLabel(text)
149                 #self.Layout()
150
151         def OnProgress(self, value, max):
152                 wx.CallAfter(self.progressGauge.SetRange, max)
153                 wx.CallAfter(self.progressGauge.SetValue, value)
154
155         def OnOk(self, e):
156                 self.Close()
157
158         def OnClose(self, e):
159                 self.Destroy()
160