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