2017-10-10 17:12:15 +02:00
|
|
|
# Virtual sdcard support (print files directly from a host g-code file)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import os, logging
|
|
|
|
|
|
|
|
class VirtualSD:
|
|
|
|
def __init__(self, config):
|
|
|
|
printer = config.get_printer()
|
2019-01-08 15:15:40 +01:00
|
|
|
printer.register_event_handler("klippy:shutdown", self.handle_shutdown)
|
2017-10-10 17:12:15 +02:00
|
|
|
# sdcard state
|
|
|
|
sd = config.get('path')
|
|
|
|
self.sdcard_dirname = os.path.normpath(os.path.expanduser(sd))
|
|
|
|
self.current_file = None
|
|
|
|
self.file_position = self.file_size = 0
|
|
|
|
# Work timer
|
|
|
|
self.reactor = printer.get_reactor()
|
|
|
|
self.must_pause_work = False
|
|
|
|
self.work_timer = None
|
|
|
|
# Register commands
|
|
|
|
self.gcode = printer.lookup_object('gcode')
|
2018-05-20 19:04:52 +02:00
|
|
|
self.gcode.register_command('M21', None)
|
2017-10-10 17:12:15 +02:00
|
|
|
for cmd in ['M20', 'M21', 'M23', 'M24', 'M25', 'M26', 'M27']:
|
|
|
|
self.gcode.register_command(cmd, getattr(self, 'cmd_' + cmd))
|
|
|
|
for cmd in ['M28', 'M29', 'M30']:
|
|
|
|
self.gcode.register_command(cmd, self.cmd_error)
|
2019-01-08 15:15:40 +01:00
|
|
|
def handle_shutdown(self):
|
|
|
|
if self.work_timer is not None:
|
2017-10-10 17:12:15 +02:00
|
|
|
self.must_pause_work = True
|
2018-04-24 00:24:58 +02:00
|
|
|
try:
|
|
|
|
readpos = max(self.file_position - 1024, 0)
|
|
|
|
readcount = self.file_position - readpos
|
|
|
|
self.current_file.seek(readpos)
|
|
|
|
data = self.current_file.read(readcount + 128)
|
|
|
|
except:
|
|
|
|
logging.exception("virtual_sdcard shutdown read")
|
|
|
|
return
|
|
|
|
logging.info("Virtual sdcard (%d): %s\nUpcoming (%d): %s",
|
|
|
|
readpos, repr(data[:readcount]),
|
|
|
|
self.file_position, repr(data[readcount:]))
|
|
|
|
def stats(self, eventtime):
|
|
|
|
if self.work_timer is None:
|
|
|
|
return False, ""
|
|
|
|
return True, "sd_pos=%d" % (self.file_position,)
|
2017-10-10 17:12:15 +02:00
|
|
|
def get_file_list(self):
|
|
|
|
dname = self.sdcard_dirname
|
|
|
|
try:
|
|
|
|
filenames = os.listdir(self.sdcard_dirname)
|
|
|
|
return [(fname, os.path.getsize(os.path.join(dname, fname)))
|
2019-02-08 17:48:53 +01:00
|
|
|
for fname in filenames
|
|
|
|
if not fname.startswith('.')]
|
2017-10-10 17:12:15 +02:00
|
|
|
except:
|
|
|
|
logging.exception("virtual_sdcard get_file_list")
|
|
|
|
raise self.gcode.error("Unable to get file list")
|
2018-02-21 02:50:06 +01:00
|
|
|
def get_status(self, eventtime):
|
|
|
|
progress = 0.
|
|
|
|
if self.work_timer is not None and self.file_size:
|
|
|
|
progress = float(self.file_position) / self.file_size
|
|
|
|
return {'progress': progress}
|
2019-01-21 18:41:42 +01:00
|
|
|
def is_active(self):
|
|
|
|
return self.work_timer is not None
|
2017-10-10 17:12:15 +02:00
|
|
|
# G-Code commands
|
|
|
|
def cmd_error(self, params):
|
|
|
|
raise self.gcode.error("SD write not supported")
|
|
|
|
def cmd_M20(self, params):
|
|
|
|
# List SD card
|
|
|
|
files = self.get_file_list()
|
|
|
|
self.gcode.respond("Begin file list")
|
|
|
|
for fname, fsize in files:
|
|
|
|
self.gcode.respond("%s %d" % (fname, fsize))
|
|
|
|
self.gcode.respond("End file list")
|
|
|
|
def cmd_M21(self, params):
|
|
|
|
# Initialize SD card
|
|
|
|
self.gcode.respond("SD card ok")
|
|
|
|
def cmd_M23(self, params):
|
|
|
|
# Select SD file
|
|
|
|
if self.work_timer is not None:
|
|
|
|
raise self.gcode.error("SD busy")
|
|
|
|
if self.current_file is not None:
|
|
|
|
self.current_file.close()
|
|
|
|
self.current_file = None
|
|
|
|
self.file_position = self.file_size = 0
|
|
|
|
try:
|
|
|
|
orig = params['#original']
|
|
|
|
filename = orig[orig.find("M23") + 4:].split()[0].strip()
|
2018-03-02 17:58:00 +01:00
|
|
|
if '*' in filename:
|
|
|
|
filename = filename[:filename.find('*')].strip()
|
2017-10-10 17:12:15 +02:00
|
|
|
except:
|
|
|
|
raise self.gcode.error("Unable to extract filename")
|
|
|
|
if filename.startswith('/'):
|
|
|
|
filename = filename[1:]
|
|
|
|
files = self.get_file_list()
|
|
|
|
files_by_lower = { fname.lower(): fname for fname, fsize in files }
|
|
|
|
try:
|
|
|
|
fname = files_by_lower[filename.lower()]
|
|
|
|
fname = os.path.join(self.sdcard_dirname, fname)
|
|
|
|
f = open(fname, 'rb')
|
|
|
|
f.seek(0, os.SEEK_END)
|
|
|
|
fsize = f.tell()
|
|
|
|
f.seek(0)
|
|
|
|
except:
|
|
|
|
logging.exception("virtual_sdcard file open")
|
|
|
|
raise self.gcode.error("Unable to open file")
|
|
|
|
self.gcode.respond("File opened:%s Size:%d" % (filename, fsize))
|
|
|
|
self.gcode.respond("File selected")
|
|
|
|
self.current_file = f
|
|
|
|
self.file_position = 0
|
|
|
|
self.file_size = fsize
|
|
|
|
def cmd_M24(self, params):
|
|
|
|
# Start/resume SD print
|
|
|
|
if self.work_timer is not None:
|
|
|
|
raise self.gcode.error("SD busy")
|
|
|
|
self.must_pause_work = False
|
|
|
|
self.work_timer = self.reactor.register_timer(
|
|
|
|
self.work_handler, self.reactor.NOW)
|
|
|
|
def cmd_M25(self, params):
|
|
|
|
# Pause SD print
|
|
|
|
if self.work_timer is not None:
|
|
|
|
self.must_pause_work = True
|
|
|
|
def cmd_M26(self, params):
|
|
|
|
# Set SD position
|
|
|
|
if self.work_timer is not None:
|
|
|
|
raise self.gcode.error("SD busy")
|
2018-04-21 03:41:13 +02:00
|
|
|
pos = self.gcode.get_int('S', params, minval=0)
|
2017-10-10 17:12:15 +02:00
|
|
|
self.file_position = pos
|
|
|
|
def cmd_M27(self, params):
|
|
|
|
# Report SD print status
|
2019-01-21 18:41:42 +01:00
|
|
|
if self.current_file is None:
|
2017-10-10 17:12:15 +02:00
|
|
|
self.gcode.respond("Not SD printing.")
|
|
|
|
return
|
|
|
|
self.gcode.respond("SD printing byte %d/%d" % (
|
|
|
|
self.file_position, self.file_size))
|
|
|
|
# Background work timer
|
|
|
|
def work_handler(self, eventtime):
|
2018-04-24 00:24:58 +02:00
|
|
|
logging.info("Starting SD card print (position %d)", self.file_position)
|
2017-10-10 17:12:15 +02:00
|
|
|
self.reactor.unregister_timer(self.work_timer)
|
|
|
|
try:
|
|
|
|
self.current_file.seek(self.file_position)
|
|
|
|
except:
|
|
|
|
logging.exception("virtual_sdcard seek")
|
2018-03-13 03:36:39 +01:00
|
|
|
self.gcode.respond_error("Unable to seek file")
|
2017-10-10 17:12:15 +02:00
|
|
|
self.work_timer = None
|
|
|
|
return self.reactor.NEVER
|
|
|
|
partial_input = ""
|
|
|
|
lines = []
|
|
|
|
while not self.must_pause_work:
|
|
|
|
if not lines:
|
|
|
|
# Read more data
|
|
|
|
try:
|
|
|
|
data = self.current_file.read(8192)
|
|
|
|
except:
|
|
|
|
logging.exception("virtual_sdcard read")
|
|
|
|
self.gcode.respond_error("Error on virtual sdcard read")
|
|
|
|
break
|
|
|
|
if not data:
|
|
|
|
# End of file
|
|
|
|
self.current_file.close()
|
|
|
|
self.current_file = None
|
2018-04-24 00:24:58 +02:00
|
|
|
logging.info("Finished SD card print")
|
2017-10-10 17:12:15 +02:00
|
|
|
self.gcode.respond("Done printing file")
|
|
|
|
break
|
|
|
|
lines = data.split('\n')
|
|
|
|
lines[0] = partial_input + lines[0]
|
|
|
|
partial_input = lines.pop()
|
|
|
|
lines.reverse()
|
2018-08-03 02:27:34 +02:00
|
|
|
self.reactor.pause(self.reactor.NOW)
|
2017-10-10 17:12:15 +02:00
|
|
|
continue
|
|
|
|
# Dispatch command
|
|
|
|
try:
|
2018-10-18 17:51:35 +02:00
|
|
|
res = self.gcode.process_batch([lines[-1]])
|
2017-10-10 17:12:15 +02:00
|
|
|
if not res:
|
|
|
|
self.reactor.pause(self.reactor.monotonic() + 0.100)
|
|
|
|
continue
|
|
|
|
except self.gcode.error as e:
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
logging.exception("virtual_sdcard dispatch")
|
|
|
|
break
|
|
|
|
self.file_position += len(lines.pop()) + 1
|
2018-04-24 00:24:58 +02:00
|
|
|
logging.info("Exiting SD card print (position %d)", self.file_position)
|
2017-10-10 17:12:15 +02:00
|
|
|
self.work_timer = None
|
|
|
|
return self.reactor.NEVER
|
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return VirtualSD(config)
|