chiark / gitweb /
Copyright message needs to be after the __future__ imports.
[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, 'Reading firmware...')
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                 threading.Thread(target=self.OnRun).start()
59                 
60                 self.ShowModal()
61                 self.Destroy()
62                 return
63
64         def OnRun(self):
65                 hexFile = intelHex.readHex(self.filename)
66                 wx.CallAfter(self.updateLabel, "Connecting to machine...")
67                 programmer = stk500v2.Stk500v2()
68                 programmer.progressCallback = self.OnProgress
69                 if self.port == 'AUTO':
70                         for self.port in machineCom.serialList(True):
71                                 try:
72                                         programmer.connect(self.port)
73                                         break
74                                 except ispBase.IspError:
75                                         pass
76                 else:
77                         try:
78                                 programmer.connect(self.port)
79                         except ispBase.IspError:
80                                 pass
81                                 
82                 if programmer.isConnected():
83                         wx.CallAfter(self.updateLabel, "Uploading firmware...")
84                         try:
85                                 programmer.programChip(hexFile)
86                                 wx.CallAfter(self.updateLabel, "Done!\nInstalled firmware: %s" % (os.path.basename(self.filename)))
87                         except ispBase.IspError as e:
88                                 wx.CallAfter(self.updateLabel, "Failed to write firmware.\n" + str(e))
89                                 
90                         programmer.close()
91                         wx.CallAfter(self.okButton.Enable)
92                         return
93                 wx.MessageBox('Failed to find machine for firmware upgrade\nIs your machine connected to the PC?', 'Firmware update', wx.OK | wx.ICON_ERROR)
94                 wx.CallAfter(self.Close)
95         
96         def updateLabel(self, text):
97                 self.progressLabel.SetLabel(text)
98                 self.Layout()
99         
100         def OnProgress(self, value, max):
101                 wx.CallAfter(self.progressGauge.SetRange, max)
102                 wx.CallAfter(self.progressGauge.SetValue, value)
103
104         def OnOk(self, e):
105                 self.Close()
106
107         def OnClose(self, e):
108                 self.Destroy()
109