printrun-src/printcore.py

changeset 15
0bbb006204fc
child 45
c82943fb205f
equal deleted inserted replaced
14:51bf56ba3c10 15:0bbb006204fc
1 #!/usr/bin/env python
2
3 # This file is part of the Printrun suite.
4 #
5 # Printrun is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Printrun is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
17
18 import time
19 import getopt
20 import sys
21 import getopt
22
23 from printrun.printcore import printcore
24 from printrun.utils import setup_logging
25 from printrun import gcoder
26
27 if __name__ == '__main__':
28 setup_logging(sys.stderr)
29 baud = 115200
30 loud = False
31 statusreport = False
32
33 from printrun.printcore import __version__ as printcore_version
34
35 usage = "Usage:\n"+\
36 " printcore [OPTIONS] PORT FILE\n\n"+\
37 "Options:\n"+\
38 " -b, --baud=BAUD_RATE"+\
39 "\t\tSet baud rate value. Default value is 115200\n"+\
40 " -s, --statusreport\t\tPrint progress as percentage\n"+\
41 " -v, --verbose\t\t\tPrint additional progress information\n"+\
42 " -V, --version\t\t\tPrint program's version number and exit\n"+\
43 " -h, --help\t\t\tPrint this help message and exit\n"
44
45 try:
46 opts, args = getopt.getopt(sys.argv[1:], "b:svVh",
47 ["baud=", "statusreport", "verbose", "version", "help"])
48 except getopt.GetoptError, err:
49 print str(err)
50 print usage
51 sys.exit(2)
52 for o, a in opts:
53 if o in ('-h', '--help'):
54 print usage
55 sys.exit(0)
56 elif o in ('-V','--version'):
57 print "printrun "+printcore_version
58 sys.exit(0)
59 elif o in ('-b','--baud'):
60 try:
61 baud = int(a)
62 except ValueError:
63 print "ValueError:"
64 print "\tInvalid BAUD_RATE value '%s'" % a
65 print "\tBAUD_RATE must be an integer\n"
66 # FIXME: This should output a more apropiate error message when
67 # not a good baud rate is passed as an argument
68 # i.e: when baud <= 1000 or > 225000
69 print usage
70 sys.exit(2)
71 elif o in ('-v', '--verbose'):
72 loud = True
73 elif o in ('-s', '--statusreport'):
74 statusreport = True
75
76 if len(args) <= 1:
77 print "Error: Port or gcode file were not specified.\n"
78 print usage
79 sys.exit(2)
80 elif len(args) > 1:
81 port = args[-2]
82 filename = args[-1]
83 print "Printing: %s on %s with baudrate %d" % (filename, port, baud)
84
85 p = printcore(port, baud)
86 p.loud = loud
87 time.sleep(2)
88 gcode = [i.strip() for i in open(filename)]
89 gcode = gcoder.LightGCode(gcode)
90 p.startprint(gcode)
91
92 try:
93 if statusreport:
94 p.loud = False
95 sys.stdout.write("Progress: 00.0%\r")
96 sys.stdout.flush()
97 while p.printing:
98 time.sleep(1)
99 if statusreport:
100 progress = 100 * float(p.queueindex) / len(p.mainqueue)
101 sys.stdout.write("Progress: %02.1f%%\r" % progress)
102 sys.stdout.flush()
103 p.disconnect()
104 sys.exit(0)
105 except:
106 p.disconnect()

mercurial