chiark / gitweb /
Merge branch 'master' into SteamEngine
[cura.git] / Cura / gui / firmwareInstall.py
1 from __future__ import absolute_import
2
3 import os, wx, threading, sys
4
5 from Cura.avr_isp import stk500v2
6 from Cura.avr_isp import ispBase
7 from Cura.avr_isp import intelHex
8
9 from Cura.util import machineCom
10 from Cura.util import profile
11 from Cura.util import resources
12
13 def getDefaultFirmware():
14         if profile.getPreference('machine_type') == 'ultimaker':
15                 if profile.getPreference('has_heated_bed') == 'True':
16                         return None
17                 if profile.getPreferenceFloat('extruder_amount') > 2:
18                         return None
19                 if profile.getPreferenceFloat('extruder_amount') > 1:
20                         if sys.platform.startswith('linux'):
21                                 return resources.getPathForFirmware("MarlinUltimaker-115200-dual.hex")
22                         else:
23                                 return resources.getPathForFirmware("MarlinUltimaker-250000-dual.hex")
24                 if sys.platform.startswith('linux'):
25                         return resources.getPathForFirmware("MarlinUltimaker-115200.hex")
26                 else:
27                         return resources.getPathForFirmware("MarlinUltimaker-250000.hex")
28         return None
29
30 class InstallFirmware(wx.Dialog):
31         def __init__(self, filename = None, port = None):
32                 super(InstallFirmware, self).__init__(parent=None, title="Firmware install", size=(250, 100))
33                 if port is None:
34                         port = profile.getPreference('serial_port')
35                 if filename is None:
36                         filename = getDefaultFirmware()
37                 if filename is None:
38                         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)
39                         self.Destroy()
40                         return
41
42                 sizer = wx.BoxSizer(wx.VERTICAL)
43                 
44                 self.progressLabel = wx.StaticText(self, -1, 'Reading firmware...')
45                 sizer.Add(self.progressLabel, 0, flag=wx.ALIGN_CENTER)
46                 self.progressGauge = wx.Gauge(self, -1)
47                 sizer.Add(self.progressGauge, 0, flag=wx.EXPAND)
48                 self.okButton = wx.Button(self, -1, 'Ok')
49                 self.okButton.Disable()
50                 self.okButton.Bind(wx.EVT_BUTTON, self.OnOk)
51                 sizer.Add(self.okButton, 0, flag=wx.ALIGN_CENTER)
52                 self.SetSizer(sizer)
53                 
54                 self.filename = filename
55                 self.port = port
56                 
57                 threading.Thread(target=self.OnRun).start()
58                 
59                 self.ShowModal()
60                 self.Destroy()
61                 return
62
63         def OnRun(self):
64                 hexFile = intelHex.readHex(self.filename)
65                 wx.CallAfter(self.updateLabel, "Connecting to machine...")
66                 programmer = stk500v2.Stk500v2()
67                 programmer.progressCallback = self.OnProgress
68                 if self.port == 'AUTO':
69                         for self.port in machineCom.serialList(True):
70                                 try:
71                                         programmer.connect(self.port)
72                                         break
73                                 except ispBase.IspError:
74                                         pass
75                 else:
76                         try:
77                                 programmer.connect(self.port)
78                         except ispBase.IspError:
79                                 pass
80                                 
81                 if programmer.isConnected():
82                         wx.CallAfter(self.updateLabel, "Uploading firmware...")
83                         try:
84                                 programmer.programChip(hexFile)
85                                 wx.CallAfter(self.updateLabel, "Done!\nInstalled firmware: %s" % (os.path.basename(self.filename)))
86                         except ispBase.IspError as e:
87                                 wx.CallAfter(self.updateLabel, "Failed to write firmware.\n" + str(e))
88                                 
89                         programmer.close()
90                         wx.CallAfter(self.okButton.Enable)
91                         return
92                 wx.MessageBox('Failed to find machine for firmware upgrade\nIs your machine connected to the PC?', 'Firmware update', wx.OK | wx.ICON_ERROR)
93                 wx.CallAfter(self.Close)
94         
95         def updateLabel(self, text):
96                 self.progressLabel.SetLabel(text)
97                 self.Layout()
98         
99         def OnProgress(self, value, max):
100                 wx.CallAfter(self.progressGauge.SetRange, max)
101                 wx.CallAfter(self.progressGauge.SetValue, value)
102
103         def OnOk(self, e):
104                 self.Close()
105
106         def OnClose(self, e):
107                 self.Destroy()
108