Added documentation and patched up a couple functions

master
kliment 2011-05-11 17:28:35 +02:00
parent 64137be9a1
commit 86c76a199c
1 changed files with 42 additions and 10 deletions

View File

@ -4,6 +4,8 @@ import time
class printcore(): class printcore():
def __init__(self,port=None,baud=None): def __init__(self,port=None,baud=None):
"""Initializes a printcore instance. Pass the port and baud rate to connect immediately
"""
self.baud=None self.baud=None
self.port=None self.port=None
self.printer=None self.printer=None
@ -11,9 +13,9 @@ class printcore():
self.mainqueue=[] self.mainqueue=[]
self.priqueue=[] self.priqueue=[]
if port is not None and baud is not None: if port is not None and baud is not None:
print port, baud #print port, baud
self.connect(port, baud) self.connect(port, baud)
print "connected\n" #print "connected\n"
self.readthread=None self.readthread=None
self.queueindex=0 self.queueindex=0
self.lineno=0 self.lineno=0
@ -23,6 +25,8 @@ class printcore():
self.sentlines={} self.sentlines={}
def disconnect(self): def disconnect(self):
"""Disconnects from printer and pauses the print
"""
if(self.printer): if(self.printer):
self.printer.close() self.printer.close()
self.printer=None self.printer=None
@ -30,6 +34,8 @@ class printcore():
self.printing=False self.printing=False
def connect(self,port=None,baud=None): def connect(self,port=None,baud=None):
"""Set port and baudrate if given, then connect to printer
"""
if(self.printer): if(self.printer):
self.disconnect() self.disconnect()
if port is not None: if port is not None:
@ -44,10 +50,8 @@ class printcore():
"""This function acts on messages from the firmware """This function acts on messages from the firmware
""" """
time.sleep(1) time.sleep(1)
print "listening:"
while(True): while(True):
if(not self.printer or not self.printer.isOpen): if(not self.printer or not self.printer.isOpen):
print "port not open"
break break
line=self.printer.readline() line=self.printer.readline()
print "RECV:",line print "RECV:",line
@ -61,41 +65,69 @@ class printcore():
elif(line.startswith('Error')): elif(line.startswith('Error')):
pass pass
if "Resend" in line or "rs" in line: if "Resend" in line or "rs" in line:
toresend=int(line.split(':')[1]) toresend=int(line.replace(":"," ").split()[-1])
self.resendfrom=toresend self.resendfrom=toresend
self.clear=True self.clear=True
print "done listening"
def checksum(self,command): def _checksum(self,command):
return reduce(lambda x,y:x^y, map(ord,command)) return reduce(lambda x,y:x^y, map(ord,command))
def startprint(self,data): def startprint(self,data):
"""Start a print, data is an array of gcode commands.
returns True on success, False if already printing.
The print queue will be replaced with the contents of the data array, the next line will be set to 0 and the firmware notified.
Printing will then start in a parallel thread.
"""
if(self.printing): if(self.printing):
return False return False
self.printing=True self.printing=True
self.mainqueue=[]+data self.mainqueue=[]+data
self.lineno=0
self.queueindex=0
self.resendfrom=-1
self._send("M110",-1, True) self._send("M110",-1, True)
Thread(target=self._print).start() Thread(target=self._print).start()
return True return True
def pause(self): def pause(self):
"""Pauses the print, saving the current position.
"""
self.printing=False self.printing=False
def resume(self): def resume(self):
"""Resumes a paused print.
"""
self.printing=True self.printing=True
threading.Thread(target=self._print).start() threading.Thread(target=self._print).start()
def send(self,command):
"""Adds a command to the checksummed main command queue if printing, or sends the command immediately if not printing
"""
if(self.printing):
self.mainqueue+=[command]
else:
while not self.clear:
time.sleep(0.001)
self._send(command,self.lineno,True)
self.lineno+=1
def send_now(self,command): def send_now(self,command):
"""Sends a command to the printer ahead of the command queue, without a checksum
"""
if(self.printing): if(self.printing):
self.priqueue+=[command] self.priqueue+=[command]
else: else:
while not self.clear:
time.sleep(0.001)
self._send(command) self._send(command)
def _print(self): def _print(self):
while(self.printing and self.printer): while(self.printing and self.printer):
self.sendnext() self._sendnext()
def sendnext(self): def _sendnext(self):
if(not self.printer): if(not self.printer):
return return
if(self.resendfrom>-1): if(self.resendfrom>-1):
@ -130,7 +162,7 @@ class printcore():
def _send(self, command, lineno=0, calcchecksum=False): def _send(self, command, lineno=0, calcchecksum=False):
if(calcchecksum): if(calcchecksum):
prefix="N"+str(lineno)+" "+command prefix="N"+str(lineno)+" "+command
command=prefix+"*"+str(self.checksum(prefix)) command=prefix+"*"+str(self._checksum(prefix))
self.sentlines[lineno]=command self.sentlines[lineno]=command
if(self.printer): if(self.printer):
print "sending: "+command+"\n" print "sending: "+command+"\n"