printrun-src/printrun/pronsole.py

Sat, 08 Sep 2018 12:00:12 +0200

author
mdd
date
Sat, 08 Sep 2018 12:00:12 +0200
changeset 39
74801c0f2709
parent 38
700241b53fe7
child 46
cce0af6351f0
permissions
-rw-r--r--

bad idea :(

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import cmd
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import glob
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import threading
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 import shutil
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 import subprocess
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 import codecs
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 import argparse
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 import locale
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 import traceback
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 import re
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 from serial import SerialException
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 from . import printcore
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 from .utils import install_locale, run_command, get_command_output, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 format_time, format_duration, RemainingTimeEstimator, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 get_home_pos, parse_build_dimensions, parse_temperature_report, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 setup_logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 install_locale('pronterface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 from .settings import Settings, BuildDimensionsSetting
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 from .power import powerset_print_start, powerset_print_stop
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 from printrun import gcoder
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 from .rpc import ProntRPC
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 if os.name == "nt":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 import _winreg
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 READLINE = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 import readline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 readline.rl.mode.show_all_if_ambiguous = "on" # config pyreadline on windows
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 READLINE = False # neither readline module is available
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 tempreading_exp = re.compile("(^T:| T:)")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 REPORT_NONE = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 REPORT_POS = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 REPORT_TEMP = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 REPORT_MANUAL = 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 class Status(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 def __init__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 self.extruder_temp = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 self.extruder_temp_target = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 self.bed_temp = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 self.bed_temp_target = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 self.print_job = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 self.print_job_progress = 1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 def update_tempreading(self, tempstr):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 temps = parse_temperature_report(tempstr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 if "T0" in temps and temps["T0"][0]: hotend_temp = float(temps["T0"][0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 elif "T" in temps and temps["T"][0]: hotend_temp = float(temps["T"][0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 else: hotend_temp = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 if "T0" in temps and temps["T0"][1]: hotend_setpoint = float(temps["T0"][1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 elif "T" in temps and temps["T"][1]: hotend_setpoint = float(temps["T"][1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 else: hotend_setpoint = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 if hotend_temp is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 self.extruder_temp = hotend_temp
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 if hotend_setpoint is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 self.extruder_temp_target = hotend_setpoint
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 bed_temp = float(temps["B"][0]) if "B" in temps and temps["B"][0] else None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 if bed_temp is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 self.bed_temp = bed_temp
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 setpoint = temps["B"][1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 if setpoint:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 self.bed_temp_target = float(setpoint)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 def bed_enabled(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 return self.bed_temp != 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 def extruder_enabled(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 return self.extruder_temp != 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 class pronsole(cmd.Cmd):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 def __init__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 cmd.Cmd.__init__(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 if not READLINE:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 self.completekey = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 self.status = Status()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 self.dynamic_temp = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 self.compute_eta = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 self.statuscheck = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 self.status_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 self.monitor_interval = 3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 self.p = printcore.printcore()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 self.p.recvcb = self.recvcb
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 self.p.startcb = self.startcb
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 self.p.endcb = self.endcb
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 self.p.layerchangecb = self.layer_change_cb
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 self.p.process_host_command = self.process_host_command
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 self.recvlisteners = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 self.in_macro = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 self.p.onlinecb = self.online
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 self.p.errorcb = self.logError
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 self.fgcode = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 self.filename = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 self.rpc_server = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 self.curlayer = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 self.sdlisting = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 self.sdlisting_echo = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 self.sdfiles = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 self.paused = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 self.sdprinting = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 self.uploading = 0 # Unused, just for pronterface generalization
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 self.temps = {"pla": "185", "abs": "230", "off": "0"}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 self.bedtemps = {"pla": "60", "abs": "110", "off": "0"}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 self.percentdone = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 self.posreport = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 self.tempreadings = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 self.userm114 = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 self.userm105 = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 self.m105_waitcycles = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 self.macros = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 self.history_file = "~/.pronsole-history"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 self.rc_loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 self.processing_rc = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 self.processing_args = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 self.settings = Settings(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 self.settings._add(BuildDimensionsSetting("build_dimensions", "200x200x100+0+0+0+0+0+0", _("Build dimensions"), _("Dimensions of Build Platform\n & optional offset of origin\n & optional switch position\n\nExamples:\n XXXxYYY\n XXX,YYY,ZZZ\n XXXxYYYxZZZ+OffX+OffY+OffZ\nXXXxYYYxZZZ+OffX+OffY+OffZ+HomeX+HomeY+HomeZ"), "Printer"), self.update_build_dimensions)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 self.settings._port_list = self.scanserial
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 self.settings._temperature_abs_cb = self.set_temp_preset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 self.settings._temperature_pla_cb = self.set_temp_preset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 self.settings._bedtemp_abs_cb = self.set_temp_preset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 self.settings._bedtemp_pla_cb = self.set_temp_preset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 self.update_build_dimensions(None, self.settings.build_dimensions)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 self.update_tcp_streaming_mode(None, self.settings.tcp_streaming_mode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 self.monitoring = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 self.starttime = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 self.extra_print_time = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 self.silent = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 self.commandprefixes = 'MGT$'
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 self.promptstrs = {"offline": "%(bold)soffline>%(normal)s ",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 "fallback": "%(bold)sPC>%(normal)s ",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 "macro": "%(bold)s..>%(normal)s ",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 "online": "%(bold)sT:%(extruder_temp_fancy)s%(progress_fancy)s>%(normal)s "}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 # General console handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 def postloop(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 self.p.disconnect()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 cmd.Cmd.postloop(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 def preloop(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 self.log(_("Welcome to the printer console! Type \"help\" for a list of available commands."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 self.prompt = self.promptf()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 cmd.Cmd.preloop(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 # We replace this function, defined in cmd.py .
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 # It's default behavior with regards to Ctr-C
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 # and Ctr-D doesn't make much sense...
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 def cmdloop(self, intro=None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 """Repeatedly issue a prompt, accept input, parse an initial prefix
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 off the received input, and dispatch to action methods, passing them
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 the remainder of the line as argument.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 self.preloop()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 if self.use_rawinput and self.completekey:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 import readline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 self.old_completer = readline.get_completer()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 readline.set_completer(self.complete)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 readline.parse_and_bind(self.completekey + ": complete")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 history = os.path.expanduser(self.history_file)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 if os.path.exists(history):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 readline.read_history_file(history)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 except ImportError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 if intro is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 self.intro = intro
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 if self.intro:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 self.stdout.write(str(self.intro) + "\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 stop = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 while not stop:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 if self.cmdqueue:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 line = self.cmdqueue.pop(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 if self.use_rawinput:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 line = raw_input(self.prompt)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 except EOFError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 self.log("")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 self.do_exit("")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 except KeyboardInterrupt:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 self.log("")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 line = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 self.stdout.write(self.prompt)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 self.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 line = self.stdin.readline()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 if not len(line):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 line = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 line = line.rstrip('\r\n')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 line = self.precmd(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 stop = self.onecmd(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 stop = self.postcmd(stop, line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 self.postloop()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 finally:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 if self.use_rawinput and self.completekey:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 import readline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 readline.set_completer(self.old_completer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 readline.write_history_file(history)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 except ImportError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 def confirm(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 y_or_n = raw_input("y/n: ")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 if y_or_n == "y":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 elif y_or_n != "n":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 return self.confirm()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 def log(self, *msg):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 msg = u"".join(unicode(i) for i in msg)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 logging.info(msg)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 def logError(self, *msg):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 msg = u"".join(unicode(i) for i in msg)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 logging.error(msg)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 if not self.settings.error_command:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 output = get_command_output(self.settings.error_command, {"$m": msg})
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 if output:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 self.log("Error command output:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 self.log(output.rstrip())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 def promptf(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 """A function to generate prompts so that we can do dynamic prompts. """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 if self.in_macro:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 promptstr = self.promptstrs["macro"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 elif not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 promptstr = self.promptstrs["offline"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 elif self.status.extruder_enabled:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 promptstr = self.promptstrs["online"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 promptstr = self.promptstrs["fallback"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 if "%" not in promptstr:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 return promptstr
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 specials = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 specials["extruder_temp"] = str(int(self.status.extruder_temp))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 specials["extruder_temp_target"] = str(int(self.status.extruder_temp_target))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 if self.status.extruder_temp_target == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 specials["extruder_temp_fancy"] = str(int(self.status.extruder_temp))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 specials["extruder_temp_fancy"] = "%s/%s" % (str(int(self.status.extruder_temp)), str(int(self.status.extruder_temp_target)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 if self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 progress = int(1000 * float(self.p.queueindex) / len(self.p.mainqueue)) / 10
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 elif self.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 progress = self.percentdone
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 progress = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 specials["progress"] = str(progress)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 if self.p.printing or self.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 specials["progress_fancy"] = " " + str(progress) + "%"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 specials["progress_fancy"] = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 specials["bold"] = "\033[01m"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 specials["normal"] = "\033[00m"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 return promptstr % specials
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 def postcmd(self, stop, line):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 """ A hook we override to generate prompts after
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 each command is executed, for the next prompt.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 We also use it to send M105 commands so that
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 temp info gets updated for the prompt."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 if self.p.online and self.dynamic_temp:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 self.p.send_now("M105")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 self.prompt = self.promptf()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 return stop
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 def kill(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 self.statuscheck = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 if self.status_thread:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 self.status_thread.join()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 self.status_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 if self.rpc_server is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 self.rpc_server.shutdown()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 def write_prompt(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 sys.stdout.write(self.promptf())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 def help_help(self, l = ""):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324 self.do_help("")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 def do_gcodes(self, l = ""):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 self.help_gcodes()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 def help_gcodes(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 self.log("Gcodes are passed through to the printer as they are")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 def precmd(self, line):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 if line.upper().startswith("M114"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 self.userm114 += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 elif line.upper().startswith("M105"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 self.userm105 += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 return line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 def help_shell(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 self.log("Executes a python command. Example:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 self.log("! os.listdir('.')")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 def do_shell(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 exec(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 def emptyline(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 """Called when an empty line is entered - do not remove"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 def default(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 if l[0].upper() in self.commandprefixes.upper():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 if self.p and self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 if not self.p.loud:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 self.log("SENDING:" + l.upper())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 self.p.send_now(l.upper())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 self.logError(_("Printer is not online."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 elif l[0] == "@":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 if self.p and self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 if not self.p.loud:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 self.log("SENDING:" + l[1:])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 self.p.send_now(l[1:])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 self.logError(_("Printer is not online."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 cmd.Cmd.default(self, l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 def do_exit(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 if self.status.extruder_temp_target != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 self.log("Setting extruder temp to 0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 self.p.send_now("M104 S0.0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 if self.status.bed_enabled:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 if self.status.bed_temp_target != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 self.log("Setting bed temp to 0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 self.p.send_now("M140 S0.0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 self.log("Disconnecting from printer...")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 if self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 self.log(_("Are you sure you want to exit while printing?\n\
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 (this will terminate the print)."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 if not self.confirm():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 self.log(_("Exiting program. Goodbye!"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 self.p.disconnect()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 self.kill()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 sys.exit()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 def help_exit(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 self.log(_("Disconnects from the printer and exits the program."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 # Macro handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 def complete_macro(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 if (len(line.split()) == 2 and line[-1] != " ") or (len(line.split()) == 1 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 return [i for i in self.macros.keys() if i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 elif len(line.split()) == 3 or (len(line.split()) == 2 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 return [i for i in ["/D", "/S"] + self.completenames(text) if i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 return []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 def hook_macro(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 l = l.rstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 ls = l.lstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 ws = l[:len(l) - len(ls)] # just leading whitespace
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 if len(ws) == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 self.end_macro()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 # pass the unprocessed line to regular command processor to not require empty line in .pronsolerc
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 return self.onecmd(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 self.cur_macro_def += l + "\n"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 def end_macro(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 if "onecmd" in self.__dict__: del self.onecmd # remove override
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 self.in_macro = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 self.prompt = self.promptf()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 if self.cur_macro_def != "":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 self.macros[self.cur_macro_name] = self.cur_macro_def
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 macro = self.compile_macro(self.cur_macro_name, self.cur_macro_def)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 setattr(self.__class__, "do_" + self.cur_macro_name, lambda self, largs, macro = macro: macro(self, *largs.split()))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 setattr(self.__class__, "help_" + self.cur_macro_name, lambda self, macro_name = self.cur_macro_name: self.subhelp_macro(macro_name))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 if not self.processing_rc:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 self.log("Macro '" + self.cur_macro_name + "' defined")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 # save it
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 if not self.processing_args:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 macro_key = "macro " + self.cur_macro_name
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 macro_def = macro_key
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 if "\n" in self.cur_macro_def:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 macro_def += "\n"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 macro_def += " "
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 macro_def += self.cur_macro_def
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 self.save_in_rc(macro_key, macro_def)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 self.logError("Empty macro - cancelled")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437 del self.cur_macro_name, self.cur_macro_def
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 def compile_macro_line(self, line):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 line = line.rstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 ls = line.lstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 ws = line[:len(line) - len(ls)] # just leading whitespace
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 if ls == "" or ls.startswith('#'): return "" # no code
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 if ls.startswith('!'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 return ws + ls[1:] + "\n" # python mode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 ls = ls.replace('"', '\\"') # need to escape double quotes
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448 ret = ws + 'self.precmd("' + ls + '".format(*arg))\n' # parametric command mode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 return ret + ws + 'self.onecmd("' + ls + '".format(*arg))\n'
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 def compile_macro(self, macro_name, macro_def):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 if macro_def.strip() == "":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 self.logError("Empty macro - cancelled")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 macro = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 pycode = "def macro(self,*arg):\n"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457 if "\n" not in macro_def.strip():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 pycode += self.compile_macro_line(" " + macro_def.strip())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 lines = macro_def.split("\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 for l in lines:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
462 pycode += self.compile_macro_line(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 exec pycode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464 return macro
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 def start_macro(self, macro_name, prev_definition = "", suppress_instructions = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467 if not self.processing_rc and not suppress_instructions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
468 self.logError("Enter macro using indented lines, end with empty line")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469 self.cur_macro_name = macro_name
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470 self.cur_macro_def = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 self.onecmd = self.hook_macro # override onecmd temporarily
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472 self.in_macro = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473 self.prompt = self.promptf()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475 def delete_macro(self, macro_name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476 if macro_name in self.macros.keys():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 delattr(self.__class__, "do_" + macro_name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478 del self.macros[macro_name]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
479 self.log("Macro '" + macro_name + "' removed")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
480 if not self.processing_rc and not self.processing_args:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
481 self.save_in_rc("macro " + macro_name, "")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
482 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
483 self.logError("Macro '" + macro_name + "' is not defined")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
484
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
485 def do_macro(self, args):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
486 if args.strip() == "":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
487 self.print_topics("User-defined macros", map(str, self.macros.keys()), 15, 80)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
489 arglist = args.split(None, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
490 macro_name = arglist[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
491 if macro_name not in self.macros and hasattr(self.__class__, "do_" + macro_name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
492 self.logError("Name '" + macro_name + "' is being used by built-in command")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
493 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
494 if len(arglist) == 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
495 macro_def = arglist[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
496 if macro_def.lower() == "/d":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
497 self.delete_macro(macro_name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
498 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
499 if macro_def.lower() == "/s":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
500 self.subhelp_macro(macro_name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
501 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502 self.cur_macro_def = macro_def
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
503 self.cur_macro_name = macro_name
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
504 self.end_macro()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
505 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506 if macro_name in self.macros:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
507 self.start_macro(macro_name, self.macros[macro_name])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
508 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
509 self.start_macro(macro_name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
510
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
511 def help_macro(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
512 self.log("Define single-line macro: macro <name> <definition>")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
513 self.log("Define multi-line macro: macro <name>")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
514 self.log("Enter macro definition in indented lines. Use {0} .. {N} to substitute macro arguments")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
515 self.log("Enter python code, prefixed with ! Use arg[0] .. arg[N] to substitute macro arguments")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
516 self.log("Delete macro: macro <name> /d")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
517 self.log("Show macro definition: macro <name> /s")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
518 self.log("'macro' without arguments displays list of defined macros")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
519
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
520 def subhelp_macro(self, macro_name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
521 if macro_name in self.macros.keys():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
522 macro_def = self.macros[macro_name]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
523 if "\n" in macro_def:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
524 self.log("Macro '" + macro_name + "' defined as:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
525 self.log(self.macros[macro_name] + "----------------")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
526 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
527 self.log("Macro '" + macro_name + "' defined as: '" + macro_def + "'")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
528 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
529 self.logError("Macro '" + macro_name + "' is not defined")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
530
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
531 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
532 # Configuration handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
533 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
534
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
535 def set(self, var, str):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
536 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
537 t = type(getattr(self.settings, var))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
538 value = self.settings._set(var, str)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
539 if not self.processing_rc and not self.processing_args:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
540 self.save_in_rc("set " + var, "set %s %s" % (var, value))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
541 except AttributeError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
542 logging.debug(_("Unknown variable '%s'") % var)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
543 except ValueError, ve:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
544 if hasattr(ve, "from_validator"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
545 self.logError(_("Bad value %s for variable '%s': %s") % (str, var, ve.args[0]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
546 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
547 self.logError(_("Bad value for variable '%s', expecting %s (%s)") % (var, repr(t)[1:-1], ve.args[0]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
548
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
549 def do_set(self, argl):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
550 args = argl.split(None, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
551 if len(args) < 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
552 for k in [kk for kk in dir(self.settings) if not kk.startswith("_")]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
553 self.log("%s = %s" % (k, str(getattr(self.settings, k))))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
554 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
555 if len(args) < 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
556 # Try getting the default value of the setting to check whether it
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
557 # actually exists
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
558 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
559 getattr(self.settings, args[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
560 except AttributeError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
561 logging.warning("Unknown variable '%s'" % args[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
562 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
563 self.set(args[0], args[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
564
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
565 def help_set(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
566 self.log("Set variable: set <variable> <value>")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
567 self.log("Show variable: set <variable>")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
568 self.log("'set' without arguments displays all variables")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
569
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
570 def complete_set(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
571 if (len(line.split()) == 2 and line[-1] != " ") or (len(line.split()) == 1 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
572 return [i for i in dir(self.settings) if not i.startswith("_") and i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
573 elif len(line.split()) == 3 or (len(line.split()) == 2 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
574 return [i for i in self.settings._tabcomplete(line.split()[1]) if i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
575 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
576 return []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
577
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
578 def load_rc(self, rc_filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
579 self.processing_rc = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
580 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
581 rc = codecs.open(rc_filename, "r", "utf-8")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
582 self.rc_filename = os.path.abspath(rc_filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
583 for rc_cmd in rc:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
584 if not rc_cmd.lstrip().startswith("#"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
585 self.onecmd(rc_cmd)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
586 rc.close()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
587 if hasattr(self, "cur_macro_def"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
588 self.end_macro()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
589 self.rc_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
590 finally:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
591 self.processing_rc = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
592
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
593 def load_default_rc(self, rc_filename = ".pronsolerc"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
594 if rc_filename == ".pronsolerc" and hasattr(sys, "frozen") and sys.frozen in ["windows_exe", "console_exe"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
595 rc_filename = "printrunconf.ini"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
596 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
597 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
598 self.load_rc(os.path.join(os.path.expanduser("~"), rc_filename))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
599 except IOError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
600 self.load_rc(rc_filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
601 except IOError:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
602 # make sure the filename is initialized
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
603 self.rc_filename = os.path.abspath(os.path.join(os.path.expanduser("~"), rc_filename))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
604
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
605 def save_in_rc(self, key, definition):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
606 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
607 Saves or updates macro or other definitions in .pronsolerc
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
608 key is prefix that determines what is being defined/updated (e.g. 'macro foo')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
609 definition is the full definition (that is written to file). (e.g. 'macro foo move x 10')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
610 Set key as empty string to just add (and not overwrite)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
611 Set definition as empty string to remove it from .pronsolerc
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
612 To delete line from .pronsolerc, set key as the line contents, and definition as empty string
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
613 Only first definition with given key is overwritten.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
614 Updates are made in the same file position.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
615 Additions are made to the end of the file.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
616 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
617 rci, rco = None, None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
618 if definition != "" and not definition.endswith("\n"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
619 definition += "\n"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
620 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
621 written = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
622 if os.path.exists(self.rc_filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
623 shutil.copy(self.rc_filename, self.rc_filename + "~bak")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
624 rci = codecs.open(self.rc_filename + "~bak", "r", "utf-8")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
625 rco = codecs.open(self.rc_filename + "~new", "w", "utf-8")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
626 if rci is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
627 overwriting = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
628 for rc_cmd in rci:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
629 l = rc_cmd.rstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
630 ls = l.lstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
631 ws = l[:len(l) - len(ls)] # just leading whitespace
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
632 if overwriting and len(ws) == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
633 overwriting = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
634 if not written and key != "" and rc_cmd.startswith(key) and (rc_cmd + "\n")[len(key)].isspace():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
635 overwriting = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
636 written = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
637 rco.write(definition)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
638 if not overwriting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
639 rco.write(rc_cmd)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
640 if not rc_cmd.endswith("\n"): rco.write("\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
641 if not written:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
642 rco.write(definition)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
643 if rci is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
644 rci.close()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
645 rco.close()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
646 shutil.move(self.rc_filename + "~new", self.rc_filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
647 # if definition != "":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
648 # self.log("Saved '"+key+"' to '"+self.rc_filename+"'")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
649 # else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
650 # self.log("Removed '"+key+"' from '"+self.rc_filename+"'")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
651 except Exception, e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
652 self.logError("Saving failed for ", key + ":", str(e))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
653 finally:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
654 del rci, rco
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
655
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
656 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
657 # Configuration update callbacks
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
658 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
659
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
660 def update_build_dimensions(self, param, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
661 self.build_dimensions_list = parse_build_dimensions(value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
662 self.p.analyzer.home_pos = get_home_pos(self.build_dimensions_list)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
663
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
664 def update_tcp_streaming_mode(self, param, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
665 self.p.tcp_streaming_mode = self.settings.tcp_streaming_mode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
666
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
667 def update_rpc_server(self, param, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
668 if value:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
669 if self.rpc_server is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
670 self.rpc_server = ProntRPC(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
671 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
672 if self.rpc_server is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
673 self.rpc_server.shutdown()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
674 self.rpc_server = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
675
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
676 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
677 # Command line options handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
678 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
679
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
680 def add_cmdline_arguments(self, parser):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
681 parser.add_argument('-v', '--verbose', help = _("increase verbosity"), action = "store_true")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
682 parser.add_argument('-c', '--conf', '--config', help = _("load this file on startup instead of .pronsolerc ; you may chain config files, if so settings auto-save will use the last specified file"), action = "append", default = [])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
683 parser.add_argument('-e', '--execute', help = _("executes command after configuration/.pronsolerc is loaded ; macros/settings from these commands are not autosaved"), action = "append", default = [])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
684 parser.add_argument('filename', nargs='?', help = _("file to load"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
685
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
686 def process_cmdline_arguments(self, args):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
687 if args.verbose:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
688 logger = logging.getLogger()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
689 logger.setLevel(logging.DEBUG)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
690 for config in args.conf:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
691 self.load_rc(config)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
692 if not self.rc_loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
693 self.load_default_rc()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
694 self.processing_args = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
695 for command in args.execute:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
696 self.onecmd(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
697 self.processing_args = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
698 self.update_rpc_server(None, self.settings.rpc_server)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
699 if args.filename:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
700 filename = args.filename.decode(locale.getpreferredencoding())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
701 self.cmdline_filename_callback(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
702
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
703 def cmdline_filename_callback(self, filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
704 self.do_load(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
705
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
706 def parse_cmdline(self, args):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
707 parser = argparse.ArgumentParser(description = 'Printrun 3D printer interface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
708 self.add_cmdline_arguments(parser)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
709 args = [arg for arg in args if not arg.startswith("-psn")]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
710 args = parser.parse_args(args = args)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
711 self.process_cmdline_arguments(args)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
712 setup_logging(sys.stdout, self.settings.log_path, True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
713
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
714 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
715 # Printer connection handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
716 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
717
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
718 def connect_to_printer(self, port, baud, dtr):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
719 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
720 self.p.connect(port, baud, dtr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
721 except SerialException as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
722 # Currently, there is no errno, but it should be there in the future
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
723 if e.errno == 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
724 self.logError(_("Error: You are trying to connect to a non-existing port."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
725 elif e.errno == 8:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
726 self.logError(_("Error: You don't have permission to open %s.") % port)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
727 self.logError(_("You might need to add yourself to the dialout group."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
728 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
729 self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
730 # Kill the scope anyway
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
731 return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
732 except OSError as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
733 if e.errno == 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
734 self.logError(_("Error: You are trying to connect to a non-existing port."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
735 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
736 self.logError(traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
737 return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
738 self.statuscheck = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
739 self.status_thread = threading.Thread(target = self.statuschecker)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
740 self.status_thread.start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
741 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
742
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
743 def do_connect(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
744 a = l.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
745 p = self.scanserial()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
746 port = self.settings.port
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
747 if (port == "" or port not in p) and len(p) > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
748 port = p[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
749 baud = self.settings.baudrate or 115200
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
750 if len(a) > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
751 port = a[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
752 if len(a) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
753 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
754 baud = int(a[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
755 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
756 self.log("Bad baud value '" + a[1] + "' ignored")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
757 if len(p) == 0 and not port:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
758 self.log("No serial ports detected - please specify a port")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
759 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
760 if len(a) == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
761 self.log("No port specified - connecting to %s at %dbps" % (port, baud))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
762 if port != self.settings.port:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
763 self.settings.port = port
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
764 self.save_in_rc("set port", "set port %s" % port)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
765 if baud != self.settings.baudrate:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
766 self.settings.baudrate = baud
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
767 self.save_in_rc("set baudrate", "set baudrate %d" % baud)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
768 self.connect_to_printer(port, baud, self.settings.dtr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
769
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
770 def help_connect(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
771 self.log("Connect to printer")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
772 self.log("connect <port> <baudrate>")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
773 self.log("If port and baudrate are not specified, connects to first detected port at 115200bps")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
774 ports = self.scanserial()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
775 if ports:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
776 self.log("Available ports: ", " ".join(ports))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
777 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
778 self.log("No serial ports were automatically found.")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
779
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
780 def complete_connect(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
781 if (len(line.split()) == 2 and line[-1] != " ") or (len(line.split()) == 1 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
782 return [i for i in self.scanserial() if i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
783 elif len(line.split()) == 3 or (len(line.split()) == 2 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
784 return [i for i in ["2400", "9600", "19200", "38400", "57600", "115200"] if i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
785 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
786 return []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
787
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
788 def scanserial(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
789 """scan for available ports. return a list of device names."""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
790 baselist = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
791 if os.name == "nt":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
792 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
793 key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
794 i = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
795 while(1):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
796 baselist += [_winreg.EnumValue(key, i)[1]]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
797 i += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
798 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
799 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
800
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
801 for g in ['/dev/ttyUSB*', '/dev/ttyACM*', "/dev/tty.*", "/dev/cu.*", "/dev/rfcomm*"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
802 baselist += glob.glob(g)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
803 return filter(self._bluetoothSerialFilter, baselist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
804
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
805 def _bluetoothSerialFilter(self, serial):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
806 return not ("Bluetooth" in serial or "FireFly" in serial)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
807
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
808 def online(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
809 self.log("\rPrinter is now online")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
810 self.write_prompt()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
811
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
812 def do_disconnect(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
813 self.p.disconnect()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
814
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
815 def help_disconnect(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
816 self.log("Disconnects from the printer")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
817
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
818 def do_block_until_online(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
819 while not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
820 time.sleep(0.1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
821
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
822 def help_block_until_online(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
823 self.log("Blocks until printer is online")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
824 self.log("Warning: if something goes wrong, this can block pronsole forever")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
825
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
826 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
827 # Printer status monitoring
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
828 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
829
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
830 def statuschecker_inner(self, do_monitoring = True):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
831 if self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
832 if self.p.writefailures >= 4:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
833 self.logError(_("Disconnecting after 4 failed writes."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
834 self.status_thread = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
835 self.disconnect()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
836 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
837 if do_monitoring:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
838 if self.sdprinting and not self.paused:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
839 self.p.send_now("M27")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
840 if self.m105_waitcycles % 10 == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
841 self.p.send_now("M105")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
842 self.m105_waitcycles += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
843 cur_time = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
844 wait_time = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
845 while time.time() < cur_time + self.monitor_interval - 0.25:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
846 if not self.statuscheck:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
847 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
848 time.sleep(0.25)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
849 # Safeguard: if system time changes and goes back in the past,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
850 # we could get stuck almost forever
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
851 wait_time += 0.25
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
852 if wait_time > self.monitor_interval - 0.25:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
853 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
854 # Always sleep at least a bit, if something goes wrong with the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
855 # system time we'll avoid freezing the whole app this way
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
856 time.sleep(0.25)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
857
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
858 def statuschecker(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
859 while self.statuscheck:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
860 self.statuschecker_inner()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
861
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
862 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
863 # File loading handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
864 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
865
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
866 def do_load(self, filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
867 self._do_load(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
868
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
869 def _do_load(self, filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
870 if not filename:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
871 self.logError("No file name given.")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
872 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
873 self.log(_("Loading file: %s") % filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
874 if not os.path.exists(filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
875 self.logError("File not found!")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
876 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
877 self.load_gcode(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
878 self.log(_("Loaded %s, %d lines.") % (filename, len(self.fgcode)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
879 self.log(_("Estimated duration: %d layers, %s") % self.fgcode.estimate_duration())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
880
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
881 def load_gcode(self, filename, layer_callback = None, gcode = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
882 if gcode is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
883 self.fgcode = gcoder.LightGCode(deferred = True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
884 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
885 self.fgcode = gcode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
886 self.fgcode.prepare(open(filename, "rU"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
887 get_home_pos(self.build_dimensions_list),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
888 layer_callback = layer_callback)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
889 self.fgcode.estimate_duration()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
890 self.filename = filename
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
891
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
892 def complete_load(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
893 s = line.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
894 if len(s) > 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
895 return []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
896 if (len(s) == 1 and line[-1] == " ") or (len(s) == 2 and line[-1] != " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
897 if len(s) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
898 return [i[len(s[1]) - len(text):] for i in glob.glob(s[1] + "*/") + glob.glob(s[1] + "*.g*")]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
899 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
900 return glob.glob("*/") + glob.glob("*.g*")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
901
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
902 def help_load(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
903 self.log("Loads a gcode file (with tab-completion)")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
904
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
905 def do_slice(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
906 l = l.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
907 if len(l) == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
908 self.logError(_("No file name given."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
909 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
910 settings = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
911 if l[0] == "set":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
912 settings = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
913 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
914 self.log(_("Slicing file: %s") % l[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
915 if not(os.path.exists(l[0])):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
916 self.logError(_("File not found!"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
917 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
918 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
919 if settings:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
920 command = self.settings.sliceoptscommand
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
921 self.log(_("Entering slicer settings: %s") % command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
922 run_command(command, blocking = True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
923 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
924 command = self.settings.slicecommand
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
925 stl_name = l[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
926 gcode_name = stl_name.replace(".stl", "_export.gcode").replace(".STL", "_export.gcode")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
927 run_command(command,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
928 {"$s": stl_name,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
929 "$o": gcode_name},
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
930 blocking = True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
931 self.log(_("Loading sliced file."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
932 self.do_load(l[0].replace(".stl", "_export.gcode"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
933 except Exception, e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
934 self.logError(_("Slicing failed: %s") % e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
935
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
936 def complete_slice(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
937 s = line.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
938 if len(s) > 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
939 return []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
940 if (len(s) == 1 and line[-1] == " ") or (len(s) == 2 and line[-1] != " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
941 if len(s) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
942 return [i[len(s[1]) - len(text):] for i in glob.glob(s[1] + "*/") + glob.glob(s[1] + "*.stl")]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
943 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
944 return glob.glob("*/") + glob.glob("*.stl")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
945
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
946 def help_slice(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
947 self.log(_("Creates a gcode file from an stl model using the slicer (with tab-completion)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
948 self.log(_("slice filename.stl - create gcode file"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
949 self.log(_("slice filename.stl view - create gcode file and view using skeiniso (if using skeinforge)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
950 self.log(_("slice set - adjust slicer settings"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
951
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
952 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
953 # Print/upload handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
954 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
955
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
956 def do_upload(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
957 names = l.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
958 if len(names) == 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
959 filename = names[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
960 targetname = names[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
961 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
962 self.logError(_("Please enter target name in 8.3 format."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
963 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
964 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
965 self.logError(_("Not connected to printer."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
966 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
967 self._do_load(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
968 self.log(_("Uploading as %s") % targetname)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
969 self.log(_("Uploading %s") % self.filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
970 self.p.send_now("M28 " + targetname)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
971 self.log(_("Press Ctrl-C to interrupt upload."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
972 self.p.startprint(self.fgcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
973 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
974 sys.stdout.write(_("Progress: ") + "00.0%")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
975 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
976 while self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
977 time.sleep(0.5)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
978 sys.stdout.write("\b\b\b\b\b%04.1f%%" % (100 * float(self.p.queueindex) / len(self.p.mainqueue),))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
979 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
980 self.p.send_now("M29 " + targetname)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
981 time.sleep(0.2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
982 self.p.clear = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
983 self._do_ls(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
984 self.log("\b\b\b\b\b100%.")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
985 self.log(_("Upload completed. %s should now be on the card.") % targetname)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
986 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
987 except (KeyboardInterrupt, Exception) as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
988 if isinstance(e, KeyboardInterrupt):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
989 self.logError(_("...interrupted!"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
990 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
991 self.logError(_("Something wrong happened while uploading:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
992 + "\n" + traceback.format_exc())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
993 self.p.pause()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
994 self.p.send_now("M29 " + targetname)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
995 time.sleep(0.2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
996 self.p.cancelprint()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
997 self.logError(_("A partial file named %s may have been written to the sd card.") % targetname)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
998
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
999 def complete_upload(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1000 s = line.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1001 if len(s) > 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1002 return []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1003 if (len(s) == 1 and line[-1] == " ") or (len(s) == 2 and line[-1] != " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1004 if len(s) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1005 return [i[len(s[1]) - len(text):] for i in glob.glob(s[1] + "*/") + glob.glob(s[1] + "*.g*")]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1006 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1007 return glob.glob("*/") + glob.glob("*.g*")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1008
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1009 def help_upload(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1010 self.log("Uploads a gcode file to the sd card")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1011
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1012 def help_print(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1013 if not self.fgcode:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1014 self.log(_("Send a loaded gcode file to the printer. Load a file with the load command first."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1015 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1016 self.log(_("Send a loaded gcode file to the printer. You have %s loaded right now.") % self.filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1017
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1018 def do_print(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1019 if not self.fgcode:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1020 self.logError(_("No file loaded. Please use load first."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1021 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1022 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1023 self.logError(_("Not connected to printer."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1024 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1025 self.log(_("Printing %s") % self.filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1026 self.log(_("You can monitor the print with the monitor command."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1027 self.sdprinting = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1028 self.p.startprint(self.fgcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1029
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1030 def do_pause(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1031 if self.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1032 self.p.send_now("M25")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1033 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1034 if not self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1035 self.logError(_("Not printing, cannot pause."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1036 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1037 self.p.pause()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1038 self.paused = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1039
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1040 def help_pause(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1041 self.log(_("Pauses a running print"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1042
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1043 def pause(self, event = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1044 return self.do_pause(None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1045
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1046 def do_resume(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1047 if not self.paused:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1048 self.logError(_("Not paused, unable to resume. Start a print first."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1049 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1050 self.paused = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1051 if self.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1052 self.p.send_now("M24")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1053 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1054 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1055 self.p.resume()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1056
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1057 def help_resume(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1058 self.log(_("Resumes a paused print."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1059
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1060 def listfiles(self, line):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1061 if "Begin file list" in line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1062 self.sdlisting = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1063 elif "End file list" in line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1064 self.sdlisting = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1065 self.recvlisteners.remove(self.listfiles)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1066 if self.sdlisting_echo:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1067 self.log(_("Files on SD card:"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1068 self.log("\n".join(self.sdfiles))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1069 elif self.sdlisting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1070 self.sdfiles.append(line.strip().lower())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1071
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1072 def _do_ls(self, echo):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1073 # FIXME: this was 2, but I think it should rather be 0 as in do_upload
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1074 self.sdlisting = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1075 self.sdlisting_echo = echo
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1076 self.sdfiles = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1077 self.recvlisteners.append(self.listfiles)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1078 self.p.send_now("M20")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1079
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1080 def do_ls(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1081 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1082 self.logError(_("Printer is not online. Please connect to it first."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1083 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1084 self._do_ls(True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1085
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1086 def help_ls(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1087 self.log(_("Lists files on the SD card"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1088
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1089 def waitforsdresponse(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1090 if "file.open failed" in l:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1091 self.logError(_("Opening file failed."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1092 self.recvlisteners.remove(self.waitforsdresponse)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1093 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1094 if "File opened" in l:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1095 self.log(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1096 if "File selected" in l:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1097 self.log(_("Starting print"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1098 self.p.send_now("M24")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1099 self.sdprinting = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1100 # self.recvlisteners.remove(self.waitforsdresponse)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1101 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1102 if "Done printing file" in l:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1103 self.log(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1104 self.sdprinting = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1105 self.recvlisteners.remove(self.waitforsdresponse)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1106 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1107 if "SD printing byte" in l:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1108 # M27 handler
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1109 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1110 resp = l.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1111 vals = resp[-1].split("/")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1112 self.percentdone = 100.0 * int(vals[0]) / int(vals[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1113 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1114 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1115
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1116 def do_reset(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1117 self.p.reset()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1118
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1119 def help_reset(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1120 self.log(_("Resets the printer."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1121
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1122 def do_sdprint(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1123 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1124 self.log(_("Printer is not online. Please connect to it first."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1125 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1126 self._do_ls(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1127 while self.listfiles in self.recvlisteners:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1128 time.sleep(0.1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1129 if l.lower() not in self.sdfiles:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1130 self.log(_("File is not present on card. Please upload it first."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1131 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1132 self.recvlisteners.append(self.waitforsdresponse)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1133 self.p.send_now("M23 " + l.lower())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1134 self.log(_("Printing file: %s from SD card.") % l.lower())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1135 self.log(_("Requesting SD print..."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1136 time.sleep(1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1137
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1138 def help_sdprint(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1139 self.log(_("Print a file from the SD card. Tab completes with available file names."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1140 self.log(_("sdprint filename.g"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1141
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1142 def complete_sdprint(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1143 if not self.sdfiles and self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1144 self._do_ls(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1145 while self.listfiles in self.recvlisteners:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1146 time.sleep(0.1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1147 if (len(line.split()) == 2 and line[-1] != " ") or (len(line.split()) == 1 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1148 return [i for i in self.sdfiles if i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1149
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1150 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1151 # Printcore callbacks
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1152 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1153
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1154 def startcb(self, resuming = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1155 self.starttime = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1156 if resuming:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1157 self.log(_("Print resumed at: %s") % format_time(self.starttime))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1158 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1159 self.log(_("Print started at: %s") % format_time(self.starttime))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1160 if not self.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1161 self.compute_eta = RemainingTimeEstimator(self.fgcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1162 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1163 self.compute_eta = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1164
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1165 if self.settings.start_command:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1166 output = get_command_output(self.settings.start_command,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1167 {"$s": str(self.filename),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1168 "$t": format_time(time.time())})
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1169 if output:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1170 self.log("Start command output:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1171 self.log(output.rstrip())
39
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1172 try:
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1173 powerset_print_start(reason = "Preventing sleep during print")
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1174 except:
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1175 self.logError(_("Failed to set power settings:")
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1176 + "\n" + traceback.format_exc())
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1177
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1178 def endcb(self):
39
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1179 try:
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1180 powerset_print_stop()
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1181 except:
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1182 self.logError(_("Failed to set power settings:")
74801c0f2709 bad idea :(
mdd
parents: 38
diff changeset
1183 + "\n" + traceback.format_exc())
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1184 if self.p.queueindex == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1185 print_duration = int(time.time() - self.starttime + self.extra_print_time)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1186 self.log(_("Print ended at: %(end_time)s and took %(duration)s") % {"end_time": format_time(time.time()),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1187 "duration": format_duration(print_duration)})
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1188
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1189 # Update total filament length used
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1190 if self.fgcode is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1191 new_total = self.settings.total_filament_used + self.fgcode.filament_length
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1192 self.set("total_filament_used", new_total)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1193
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1194 if not self.settings.final_command:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1195 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1196 output = get_command_output(self.settings.final_command,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1197 {"$s": str(self.filename),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1198 "$t": format_duration(print_duration)})
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1199 if output:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1200 self.log("Final command output:")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1201 self.log(output.rstrip())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1202
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1203 def recvcb_report(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1204 isreport = REPORT_NONE
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1205 if "ok C:" in l or "Count" in l \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1206 or ("X:" in l and len(gcoder.m114_exp.findall(l)) == 6):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1207 self.posreport = l
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1208 isreport = REPORT_POS
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1209 if self.userm114 > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1210 self.userm114 -= 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1211 isreport |= REPORT_MANUAL
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1212 if "ok T:" in l or tempreading_exp.findall(l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1213 self.tempreadings = l
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1214 isreport = REPORT_TEMP
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1215 if self.userm105 > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1216 self.userm105 -= 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1217 isreport |= REPORT_MANUAL
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1218 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1219 self.m105_waitcycles = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1220 return isreport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1221
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1222 def recvcb_actions(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1223 if l.startswith("!!"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1224 self.do_pause(None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1225 msg = l.split(" ", 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1226 if len(msg) > 1 and self.silent is False: self.logError(msg[1].ljust(15))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1227 sys.stdout.write(self.promptf())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1228 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1229 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1230 elif l.startswith("//"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1231 command = l.split(" ", 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1232 if len(command) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1233 command = command[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1234 self.log(_("Received command %s") % command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1235 command = command.split(":")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1236 if len(command) == 2 and command[0] == "action":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1237 command = command[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1238 if command == "pause":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1239 self.do_pause(None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1240 sys.stdout.write(self.promptf())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1241 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1242 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1243 elif command == "resume":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1244 self.do_resume(None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1245 sys.stdout.write(self.promptf())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1246 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1247 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1248 elif command == "disconnect":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1249 self.do_disconnect(None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1250 sys.stdout.write(self.promptf())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1251 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1252 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1253 return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1254
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1255 def recvcb(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1256 l = l.rstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1257 for listener in self.recvlisteners:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1258 listener(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1259 if not self.recvcb_actions(l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1260 report_type = self.recvcb_report(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1261 if report_type & REPORT_TEMP:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1262 self.status.update_tempreading(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1263 if l != "ok" and not self.sdlisting \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1264 and not self.monitoring and (report_type == REPORT_NONE or report_type & REPORT_MANUAL):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1265 if l[:5] == "echo:":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1266 l = l[5:].lstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1267 if self.silent is False: self.log("\r" + l.ljust(15))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1268 sys.stdout.write(self.promptf())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1269 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1270
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1271 def layer_change_cb(self, newlayer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1272 layerz = self.fgcode.all_layers[newlayer].z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1273 if layerz is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1274 self.curlayer = layerz
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1275 if self.compute_eta:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1276 secondselapsed = int(time.time() - self.starttime + self.extra_print_time)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1277 self.compute_eta.update_layer(newlayer, secondselapsed)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1278
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1279 def get_eta(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1280 if self.sdprinting or self.uploading:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1281 if self.uploading:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1282 fractioncomplete = float(self.p.queueindex) / len(self.p.mainqueue)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1283 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1284 fractioncomplete = float(self.percentdone / 100.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1285 secondselapsed = int(time.time() - self.starttime + self.extra_print_time)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1286 # Prevent division by zero
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1287 secondsestimate = secondselapsed / max(fractioncomplete, 0.000001)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1288 secondsremain = secondsestimate - secondselapsed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1289 progress = fractioncomplete
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1290 elif self.compute_eta is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1291 secondselapsed = int(time.time() - self.starttime + self.extra_print_time)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1292 secondsremain, secondsestimate = self.compute_eta(self.p.queueindex, secondselapsed)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1293 progress = self.p.queueindex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1294 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1295 secondsremain, secondsestimate, progress = 1, 1, 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1296 return secondsremain, secondsestimate, progress
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1297
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1298 def do_eta(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1299 if not self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1300 self.logError(_("Printer is not currently printing. No ETA available."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1301 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1302 secondsremain, secondsestimate, progress = self.get_eta()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1303 eta = _("Est: %s of %s remaining") % (format_duration(secondsremain),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1304 format_duration(secondsestimate))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1305 self.log(eta.strip())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1306
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1307 def help_eta(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1308 self.log(_("Displays estimated remaining print time."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1309
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1310 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1311 # Temperature handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1312 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1313
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1314 def set_temp_preset(self, key, value):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1315 if not key.startswith("bed"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1316 self.temps["pla"] = str(self.settings.temperature_pla)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1317 self.temps["abs"] = str(self.settings.temperature_abs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1318 self.log("Hotend temperature presets updated, pla:%s, abs:%s" % (self.temps["pla"], self.temps["abs"]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1319 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1320 self.bedtemps["pla"] = str(self.settings.bedtemp_pla)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1321 self.bedtemps["abs"] = str(self.settings.bedtemp_abs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1322 self.log("Bed temperature presets updated, pla:%s, abs:%s" % (self.bedtemps["pla"], self.bedtemps["abs"]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1323
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1324 def tempcb(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1325 if "T:" in l:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1326 self.log(l.strip().replace("T", "Hotend").replace("B", "Bed").replace("ok ", ""))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1327
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1328 def do_gettemp(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1329 if "dynamic" in l:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1330 self.dynamic_temp = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1331 if self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1332 self.p.send_now("M105")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1333 time.sleep(0.75)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1334 if not self.status.bed_enabled:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1335 self.log(_("Hotend: %s/%s") % (self.status.extruder_temp, self.status.extruder_temp_target))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1336 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1337 self.log(_("Hotend: %s/%s") % (self.status.extruder_temp, self.status.extruder_temp_target))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1338 self.log(_("Bed: %s/%s") % (self.status.bed_temp, self.status.bed_temp_target))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1339
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1340 def help_gettemp(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1341 self.log(_("Read the extruder and bed temperature."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1342
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1343 def do_settemp(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1344 l = l.lower().replace(", ", ".")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1345 for i in self.temps.keys():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1346 l = l.replace(i, self.temps[i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1347 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1348 f = float(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1349 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1350 self.logError(_("You must enter a temperature."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1351 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1352
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1353 if f >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1354 if f > 250:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1355 self.log(_("%s is a high temperature to set your extruder to. Are you sure you want to do that?") % f)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1356 if not self.confirm():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1357 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1358 if self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1359 self.p.send_now("M104 S" + l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1360 self.log(_("Setting hotend temperature to %s degrees Celsius.") % f)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1361 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1362 self.logError(_("Printer is not online."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1363 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1364 self.logError(_("You cannot set negative temperatures. To turn the hotend off entirely, set its temperature to 0."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1365
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1366 def help_settemp(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1367 self.log(_("Sets the hotend temperature to the value entered."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1368 self.log(_("Enter either a temperature in celsius or one of the following keywords"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1369 self.log(", ".join([i + "(" + self.temps[i] + ")" for i in self.temps.keys()]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1370
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1371 def complete_settemp(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1372 if (len(line.split()) == 2 and line[-1] != " ") or (len(line.split()) == 1 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1373 return [i for i in self.temps.keys() if i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1374
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1375 def do_bedtemp(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1376 f = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1377 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1378 l = l.lower().replace(", ", ".")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1379 for i in self.bedtemps.keys():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1380 l = l.replace(i, self.bedtemps[i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1381 f = float(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1382 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1383 self.logError(_("You must enter a temperature."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1384 if f is not None and f >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1385 if self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1386 self.p.send_now("M140 S" + l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1387 self.log(_("Setting bed temperature to %s degrees Celsius.") % f)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1388 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1389 self.logError(_("Printer is not online."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1390 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1391 self.logError(_("You cannot set negative temperatures. To turn the bed off entirely, set its temperature to 0."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1392
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1393 def help_bedtemp(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1394 self.log(_("Sets the bed temperature to the value entered."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1395 self.log(_("Enter either a temperature in celsius or one of the following keywords"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1396 self.log(", ".join([i + "(" + self.bedtemps[i] + ")" for i in self.bedtemps.keys()]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1397
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1398 def complete_bedtemp(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1399 if (len(line.split()) == 2 and line[-1] != " ") or (len(line.split()) == 1 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1400 return [i for i in self.bedtemps.keys() if i.startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1401
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1402 def do_monitor(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1403 interval = 5
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1404 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1405 self.logError(_("Printer is not online. Please connect to it first."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1406 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1407 if not (self.p.printing or self.sdprinting):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1408 self.logError(_("Printer is not printing. Please print something before monitoring."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1409 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1410 self.log(_("Monitoring printer, use ^C to interrupt."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1411 if len(l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1412 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1413 interval = float(l)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1414 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1415 self.logError(_("Invalid period given."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1416 self.log(_("Updating values every %f seconds.") % (interval,))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1417 self.monitoring = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1418 prev_msg_len = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1419 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1420 while True:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1421 self.p.send_now("M105")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1422 if self.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1423 self.p.send_now("M27")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1424 time.sleep(interval)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1425 if self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1426 preface = _("Print progress: ")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1427 progress = 100 * float(self.p.queueindex) / len(self.p.mainqueue)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1428 elif self.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1429 preface = _("SD print progress: ")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1430 progress = self.percentdone
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1431 prev_msg = preface + "%.1f%%" % progress
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1432 if self.silent is False:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1433 sys.stdout.write("\r" + prev_msg.ljust(prev_msg_len))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1434 sys.stdout.flush()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1435 prev_msg_len = len(prev_msg)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1436 except KeyboardInterrupt:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1437 if self.silent is False: self.log(_("Done monitoring."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1438 self.monitoring = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1439
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1440 def help_monitor(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1441 self.log(_("Monitor a machine's temperatures and an SD print's status."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1442 self.log(_("monitor - Reports temperature and SD print status (if SD printing) every 5 seconds"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1443 self.log(_("monitor 2 - Reports temperature and SD print status (if SD printing) every 2 seconds"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1444
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1445 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1446 # Manual printer controls
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1447 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1448
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1449 def do_tool(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1450 tool = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1451 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1452 tool = int(l.lower().strip())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1453 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1454 self.logError(_("You must specify the tool index as an integer."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1455 if tool is not None and tool >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1456 if self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1457 self.p.send_now("T%d" % tool)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1458 self.log(_("Using tool %d.") % tool)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1459 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1460 self.logError(_("Printer is not online."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1461 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1462 self.logError(_("You cannot set negative tool numbers."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1463
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1464 def help_tool(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1465 self.log(_("Switches to the specified tool (e.g. doing tool 1 will emit a T1 G-Code)."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1466
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1467 def do_move(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1468 if len(l.split()) < 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1469 self.logError(_("No move specified."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1470 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1471 if self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1472 self.logError(_("Printer is currently printing. Please pause the print before you issue manual commands."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1473 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1474 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1475 self.logError(_("Printer is not online. Unable to move."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1476 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1477 l = l.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1478 if l[0].lower() == "x":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1479 feed = self.settings.xy_feedrate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1480 axis = "X"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1481 elif l[0].lower() == "y":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1482 feed = self.settings.xy_feedrate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1483 axis = "Y"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1484 elif l[0].lower() == "z":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1485 feed = self.settings.z_feedrate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1486 axis = "Z"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1487 elif l[0].lower() == "e":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1488 feed = self.settings.e_feedrate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1489 axis = "E"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1490 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1491 self.logError(_("Unknown axis."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1492 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1493 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1494 float(l[1]) # check if distance can be a float
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1495 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1496 self.logError(_("Invalid distance"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1497 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1498 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1499 feed = int(l[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1500 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1501 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1502 self.p.send_now("G91")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1503 self.p.send_now("G0 " + axis + str(l[1]) + " F" + str(feed))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1504 self.p.send_now("G90")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1505
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1506 def help_move(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1507 self.log(_("Move an axis. Specify the name of the axis and the amount. "))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1508 self.log(_("move X 10 will move the X axis forward by 10mm at %s mm/min (default XY speed)") % self.settings.xy_feedrate)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1509 self.log(_("move Y 10 5000 will move the Y axis forward by 10mm at 5000mm/min"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1510 self.log(_("move Z -1 will move the Z axis down by 1mm at %s mm/min (default Z speed)") % self.settings.z_feedrate)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1511 self.log(_("Common amounts are in the tabcomplete list."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1512
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1513 def complete_move(self, text, line, begidx, endidx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1514 if (len(line.split()) == 2 and line[-1] != " ") or (len(line.split()) == 1 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1515 return [i for i in ["X ", "Y ", "Z ", "E "] if i.lower().startswith(text)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1516 elif len(line.split()) == 3 or (len(line.split()) == 2 and line[-1] == " "):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1517 base = line.split()[-1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1518 rlen = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1519 if base.startswith("-"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1520 rlen = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1521 if line[-1] == " ":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1522 base = ""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1523 return [i[rlen:] for i in ["-100", "-10", "-1", "-0.1", "100", "10", "1", "0.1", "-50", "-5", "-0.5", "50", "5", "0.5", "-200", "-20", "-2", "-0.2", "200", "20", "2", "0.2"] if i.startswith(base)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1524 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1525 return []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1526
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1527 def do_extrude(self, l, override = None, overridefeed = 300):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1528 length = self.settings.default_extrusion # default extrusion length
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1529 feed = self.settings.e_feedrate # default speed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1530 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1531 self.logError("Printer is not online. Unable to extrude.")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1532 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1533 if self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1534 self.logError(_("Printer is currently printing. Please pause the print before you issue manual commands."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1535 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1536 ls = l.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1537 if len(ls):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1538 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1539 length = float(ls[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1540 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1541 self.logError(_("Invalid length given."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1542 if len(ls) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1543 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1544 feed = int(ls[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1545 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1546 self.logError(_("Invalid speed given."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1547 if override is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1548 length = override
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1549 feed = overridefeed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1550 self.do_extrude_final(length, feed)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1551
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1552 def do_extrude_final(self, length, feed):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1553 if length > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1554 self.log(_("Extruding %fmm of filament.") % (length,))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1555 elif length < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1556 self.log(_("Reversing %fmm of filament.") % (-length,))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1557 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1558 self.log(_("Length is 0, not doing anything."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1559 self.p.send_now("G91")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1560 self.p.send_now("G1 E" + str(length) + " F" + str(feed))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1561 self.p.send_now("G90")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1562
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1563 def help_extrude(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1564 self.log(_("Extrudes a length of filament, 5mm by default, or the number of mm given as a parameter"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1565 self.log(_("extrude - extrudes 5mm of filament at 300mm/min (5mm/s)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1566 self.log(_("extrude 20 - extrudes 20mm of filament at 300mm/min (5mm/s)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1567 self.log(_("extrude -5 - REVERSES 5mm of filament at 300mm/min (5mm/s)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1568 self.log(_("extrude 10 210 - extrudes 10mm of filament at 210mm/min (3.5mm/s)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1569
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1570 def do_reverse(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1571 length = self.settings.default_extrusion # default extrusion length
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1572 feed = self.settings.e_feedrate # default speed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1573 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1574 self.logError(_("Printer is not online. Unable to reverse."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1575 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1576 if self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1577 self.logError(_("Printer is currently printing. Please pause the print before you issue manual commands."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1578 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1579 ls = l.split()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1580 if len(ls):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1581 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1582 length = float(ls[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1583 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1584 self.logError(_("Invalid length given."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1585 if len(ls) > 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1586 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1587 feed = int(ls[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1588 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1589 self.logError(_("Invalid speed given."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1590 self.do_extrude("", -length, feed)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1591
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1592 def help_reverse(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1593 self.log(_("Reverses the extruder, 5mm by default, or the number of mm given as a parameter"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1594 self.log(_("reverse - reverses 5mm of filament at 300mm/min (5mm/s)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1595 self.log(_("reverse 20 - reverses 20mm of filament at 300mm/min (5mm/s)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1596 self.log(_("reverse 10 210 - extrudes 10mm of filament at 210mm/min (3.5mm/s)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1597 self.log(_("reverse -5 - EXTRUDES 5mm of filament at 300mm/min (5mm/s)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1598
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1599 def do_home(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1600 if not self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1601 self.logError(_("Printer is not online. Unable to move."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1602 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1603 if self.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1604 self.logError(_("Printer is currently printing. Please pause the print before you issue manual commands."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1605 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1606 if "x" in l.lower():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1607 self.p.send_now("G28 X0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1608 if "y" in l.lower():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1609 self.p.send_now("G28 Y0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1610 if "z" in l.lower():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1611 self.p.send_now("G28 Z0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1612 if "e" in l.lower():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1613 self.p.send_now("G92 E0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1614 if not len(l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1615 self.p.send_now("G28")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1616 self.p.send_now("G92 E0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1617
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1618 def help_home(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1619 self.log(_("Homes the printer"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1620 self.log(_("home - homes all axes and zeroes the extruder(Using G28 and G92)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1621 self.log(_("home xy - homes x and y axes (Using G28)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1622 self.log(_("home z - homes z axis only (Using G28)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1623 self.log(_("home e - set extruder position to zero (Using G92)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1624 self.log(_("home xyze - homes all axes and zeroes the extruder (Using G28 and G92)"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1625
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1626 def do_off(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1627 self.off()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1628
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1629 def off(self, ignore = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1630 if self.p.online:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1631 if self.p.printing: self.pause(None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1632 self.log(_("; Motors off"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1633 self.onecmd("M84")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1634 self.log(_("; Extruder off"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1635 self.onecmd("M104 S0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1636 self.log(_("; Heatbed off"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1637 self.onecmd("M140 S0")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1638 self.log(_("; Fan off"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1639 self.onecmd("M107")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1640 self.log(_("; Power supply off"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1641 self.onecmd("M81")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1642 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1643 self.logError(_("Printer is not online. Unable to turn it off."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1644
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1645 def help_off(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1646 self.log(_("Turns off everything on the printer"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1647
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1648 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1649 # Host commands handling
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1650 # --------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1651
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1652 def process_host_command(self, command):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1653 """Override host command handling"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1654 command = command.lstrip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1655 if command.startswith(";@"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1656 command = command[2:]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1657 self.log(_("G-Code calling host command \"%s\"") % command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1658 self.onecmd(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1659
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1660 def do_run_script(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1661 p = run_command(l, {"$s": str(self.filename)}, stdout = subprocess.PIPE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1662 for line in p.stdout.readlines():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1663 self.log("<< " + line.strip())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1664
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1665 def help_run_script(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1666 self.log(_("Runs a custom script. Current gcode filename can be given using $s token."))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1667
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1668 def do_run_gcode_script(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1669 p = run_command(l, {"$s": str(self.filename)}, stdout = subprocess.PIPE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1670 for line in p.stdout.readlines():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1671 self.onecmd(line.strip())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1672
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1673 def help_run_gcode_script(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1674 self.log(_("Runs a custom script which output gcode which will in turn be executed. Current gcode filename can be given using $s token."))

mercurial