Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
Printrun
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
machinery
Printrun
Commits
86c76a19
Commit
86c76a19
authored
May 11, 2011
by
kliment
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added documentation and patched up a couple functions
parent
64137be9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
10 deletions
+42
-10
printcore.py
printcore.py
+42
-10
No files found.
printcore.py
View file @
86c76a19
...
@@ -4,6 +4,8 @@ import time
...
@@ -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():
...
@@ -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():
...
@@ -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():
...
@@ -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():
...
@@ -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():
...
@@ -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():
...
@@ -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
"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment