plus4的klipper版本

This commit is contained in:
whb0514
2024-09-02 13:37:34 +08:00
parent 653d7a8f6e
commit b90736975b
1006 changed files with 1195894 additions and 11114 deletions

View File

@@ -1,14 +1,18 @@
# Virtual sdcard support (print files directly from a host g-code file)
#
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2018-2024 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import os, sys, logging
reload(sys)
sys.setdefaultencoding('utf-8')
import os, sys, logging, io
VALID_GCODE_EXTS = ['gcode', 'g', 'gco']
DEFAULT_ERROR_GCODE = """
{% if 'heaters' in printer %}
TURN_OFF_HEATERS
{% endif %}
"""
class VirtualSD:
def __init__(self, config):
self.printer = config.get_printer()
@@ -29,7 +33,11 @@ class VirtualSD:
# Error handling
gcode_macro = self.printer.load_object(config, 'gcode_macro')
self.on_error_gcode = gcode_macro.load_template(
config, 'on_error_gcode', '')
config, 'on_error_gcode', DEFAULT_ERROR_GCODE)
# power lose resume
self.lines = 0
self.save_every_n_lines = 50
self.plr_enabled = config.getboolean('plr_enabled', True)
# Register commands
self.gcode = self.printer.lookup_object('gcode')
for cmd in ['M20', 'M21', 'M23', 'M24', 'M25', 'M26', 'M27']:
@@ -120,8 +128,9 @@ class VirtualSD:
self.do_pause()
self.current_file.close()
self.current_file = None
self.lines = 0
self.print_stats.note_cancel()
self.file_position = self.file_size = 0.
self.file_position = self.file_size = 0
# G-Code commands
def cmd_error(self, gcmd):
raise gcmd.error("SD write not supported")
@@ -130,7 +139,7 @@ class VirtualSD:
self.do_pause()
self.current_file.close()
self.current_file = None
self.file_position = self.file_size = 0.
self.file_position = self.file_size = 0
self.print_stats.reset()
self.printer.send_event("virtual_sdcard:reset_file")
cmd_SDCARD_RESET_FILE_help = "Clears a loaded SD File. Stops the print "\
@@ -179,7 +188,7 @@ class VirtualSD:
if fname not in flist:
fname = files_by_lower[fname.lower()]
fname = os.path.join(self.sdcard_dirname, fname)
f = open(fname, 'r')
f = io.open(fname, 'r', newline='')
f.seek(0, os.SEEK_END)
fsize = f.tell()
f.seek(0)
@@ -232,16 +241,27 @@ class VirtualSD:
partial_input = ""
lines = []
error_message = None
# Recreate the file to balance the wear on the eMMC
if self.plr_enabled:
file_path = "/home/mks/scripts/plr/plr_record"
if os.path.exists(file_path):
os.remove(file_path)
plr_file = open(file_path, 'w', buffering=1)
while not self.must_pause_work:
if not lines:
# Read more data
try:
data = self.current_file.read(8192)
except:
if self.plr_enabled:
plr_file.close()
logging.exception("virtual_sdcard read")
break
if not data:
# End of file
self.lines = 0
if self.plr_enabled:
plr_file.close()
self.current_file.close()
self.current_file = None
logging.info("Finished SD card print")
@@ -260,9 +280,17 @@ class VirtualSD:
# Dispatch command
self.cmd_from_sd = True
line = lines.pop()
next_file_position = self.file_position + len(line) + 1
if sys.version_info.major >= 3:
next_file_position = self.file_position + len(line.encode()) + 1
else:
next_file_position = self.file_position + len(line) + 1
self.next_file_position = next_file_position
try:
self.lines += 1
if self.lines % self.save_every_n_lines == 0 and self.plr_enabled:
plr_file.seek(0)
plr_file.write(str(self.lines))
plr_file.truncate()
self.gcode.run_script(line)
except self.gcode.error as e:
error_message = str(e)
@@ -287,6 +315,8 @@ class VirtualSD:
lines = []
partial_input = ""
logging.info("Exiting SD card print (position %d)", self.file_position)
if self.plr_enabled:
plr_file.close()
self.work_timer = None
self.cmd_from_sd = False
if error_message is not None: