chiark / gitweb /
Firmware can now be updated on the LulzBot Mini through the Install Default Firmware...
[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                 if profile.getMachineSetting('machine_type', machineIndex) == 'reprap':
64                         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)
65
66                 sizer = wx.BoxSizer(wx.VERTICAL)
67
68                 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX')
69                 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
70                 self.progressGauge = wx.Gauge(self, -1)
71                 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
72                 self.okButton = wx.Button(self, -1, _("OK"))
73                 self.okButton.Disable()
74                 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
75                 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER|wx.ALL, border=5)
76                 self.SetSizer(sizer)
77
78                 self.filename = filename
79                 self.port = port
80
81                 self.Layout()
82                 self.Fit()
83
84                 self.thread = threading.Thread(target=self.OnRun)
85                 self.thread.daemon = True
86                 self.thread.start()
87
88                 self.ShowModal()
89                 self.Destroy()
90                 return
91
92         def OnRun(self):
93                 wx.CallAfter(self.updateLabel, _("Reading firmware..."))
94                 hexFile = intelHex.readHex(self.filename)
95                 wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
96                 programmer = stk500v2.Stk500v2()
97                 programmer.progressCallback = self.OnProgress
98                 if self.port == 'AUTO':
99                         wx.CallAfter(self.updateLabel, _("Please connect the printer to\nyour computer with the USB cable."))
100                         while not programmer.isConnected():
101                                 for self.port in machineCom.serialList(True):
102                                         try:
103                                                 programmer.connect(self.port)
104                                                 break
105                                         except ispBase.IspError:
106                                                 pass
107                                 time.sleep(1)
108                                 if not self:
109                                         #Window destroyed
110                                         return
111                 else:
112                         try:
113                                 programmer.connect(self.port)
114                         except ispBase.IspError:
115                                 pass
116
117                 if not programmer.isConnected():
118                         wx.MessageBox(_("Failed to find machine for firmware upgrade\nIs your machine connected to the PC?"),
119                                                   _("Firmware update"), wx.OK | wx.ICON_ERROR)
120                         wx.CallAfter(self.Close)
121                         return
122
123                 wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
124                 try:
125                         programmer.programChip(hexFile)
126                         wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
127                 except ispBase.IspError as e:
128                         wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
129
130                 programmer.close()
131                 wx.CallAfter(self.okButton.Enable)
132
133         def updateLabel(self, text):
134                 self.progressLabel.SetLabel(text)
135                 #self.Layout()
136
137         def OnProgress(self, value, max):
138                 wx.CallAfter(self.progressGauge.SetRange, max)
139                 wx.CallAfter(self.progressGauge.SetValue, value)
140
141         def OnOk(self, e):
142                 self.Close()
143
144         def OnClose(self, e):
145                 self.Destroy()
146