printrun-src/printrun/pronsole.py

Wed, 20 Jan 2021 10:15:13 +0100

author
mdd
date
Wed, 20 Jan 2021 10:15:13 +0100
changeset 46
cce0af6351f0
parent 39
74801c0f2709
permissions
-rw-r--r--

updated and added new files for printrun

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

mercurial