chiark / gitweb /
2dc4ef74b303e1b10faac6de240c250b0ec21465
[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.getPreference('machine_type') == 'ultimaker':
16                 if profile.getPreference('has_heated_bed') == 'True':
17                         return None
18                 if profile.getPreferenceFloat('extruder_amount') > 2:
19                         return None
20                 if profile.getPreferenceFloat('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         return None
30
31 class InstallFirmware(wx.Dialog):
32         def __init__(self, filename = None, port = None):
33                 super(InstallFirmware, self).__init__(parent=None, title="Firmware install", size=(250, 100))
34                 if port is None:
35                         port = profile.getPreference('serial_port')
36                 if filename is None:
37                         filename = getDefaultFirmware()
38                 if filename is None:
39                         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)
40                         self.Destroy()
41                         return
42
43                 sizer = wx.BoxSizer(wx.VERTICAL)
44
45                 self.progressLabel = wx.StaticText(self, -1, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nX')
46                 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER)
47                 self.progressGauge = wx.Gauge(self, -1)
48                 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
49                 self.okButton = wx.Button(self, -1, _("OK"))
50                 self.okButton.Disable()
51                 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
52                 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER)
53                 self.SetSizer(sizer)
54
55                 self.filename = filename
56                 self.port = port
57
58                 self.Layout()
59                 self.Fit()
60
61                 threading.Thread(target=self.OnRun).start()
62
63                 self.ShowModal()
64                 self.Destroy()
65                 return
66
67         def OnRun(self):
68                 wx.CallAfter(self.updateLabel, _("Reading firmware..."))
69                 hexFile = intelHex.readHex(self.filename)
70                 wx.CallAfter(self.updateLabel, _("Connecting to machine..."))
71                 programmer = stk500v2.Stk500v2()
72                 programmer.progressCallback = self.OnProgress
73                 if self.port == 'AUTO':
74                         for self.port in machineCom.serialList(True):
75                                 try:
76                                         programmer.connect(self.port)
77                                         break
78                                 except ispBase.IspError:
79                                         pass
80                 else:
81                         try:
82                                 programmer.connect(self.port)
83                         except ispBase.IspError:
84                                 pass
85
86                 if programmer.isConnected():
87                         wx.CallAfter(self.updateLabel, _("Uploading firmware..."))
88                         try:
89                                 programmer.programChip(hexFile)
90                                 wx.CallAfter(self.updateLabel, _("Done!\nInstalled firmware: %s") % (os.path.basename(self.filename)))
91                         except ispBase.IspError as e:
92                                 wx.CallAfter(self.updateLabel, _("Failed to write firmware.\n") + str(e))
93
94                         programmer.close()
95                         wx.CallAfter(self.okButton.Enable)
96                         return
97                 wx.MessageBox(_("Failed to find machine for firmware upgrade\nIs your machine connected to the PC?"),
98                                           _("Firmware update"), wx.OK | wx.ICON_ERROR)
99                 wx.CallAfter(self.Close)
100
101         def updateLabel(self, text):
102                 self.progressLabel.SetLabel(text)
103                 #self.Layout()
104
105         def OnProgress(self, value, max):
106                 wx.CallAfter(self.progressGauge.SetRange, max)
107                 wx.CallAfter(self.progressGauge.SetValue, value)
108
109         def OnOk(self, e):
110                 self.Close()
111
112         def OnClose(self, e):
113                 self.Destroy()
114