-
Notifications
You must be signed in to change notification settings - Fork 1
/
ayab_commandline.py
executable file
·269 lines (227 loc) · 7.34 KB
/
ayab_commandline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python
# -*- coding: utf-8 -*-
#This file is part of AYAB.
#
# AYAB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AYAB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AYAB. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2013 Christian Obersteiner, Andreas Müller
# https://bitbucket.org/chris007de/ayab-apparat/
import os
import sys
import serial
import serial.tools.list_ports
from optparse import OptionParser
import ayab_image
import ayab_control
VERSION = "1.2"
def getSerialPorts():
"""
Returns a generator for all available serial ports
"""
if os.name == 'nt':
# windows
for i in range(256):
try:
s = serial.Serial(i)
s.close()
yield 'COM' + str(i + 1)
except serial.SerialException:
pass
else:
# unix
for port in serial.tools.list_ports.comports():
yield port[0]
def showImage(image):
"""
show the image in ASCII
"""
for row in range(0, image.imgHeight()):
msg = ''
for col in range(0, image.imgWidth()):
# TODO Mapping of color numbers to
# fancy ascii representation
msg += str((image.imageIntern())[row][col])
print msg
raw_input("press Enter")
def showImagePosition(image):
"""
show the current positioning of the image
"""
imgStartNeedle = image.imgStartNeedle()
imgStopNeedle = image.imgStopNeedle()
knitStartNeedle = image.knitStartNeedle()
knitStopNeedle = image.knitStopNeedle()
print "Image Start: ", imgStartNeedle
print "Image Stop : ", imgStopNeedle
print ""
# print markers for active area and knitted image
msg = '|'
for i in range(0,200):
if i >= knitStartNeedle and i <= knitStopNeedle:
if i >= imgStartNeedle and i <= imgStopNeedle:
msg += 'x'
else:
msg += '-'
else:
msg += '_'
msg += '|'
print msg
# print markers at multiples of 10
msg = '|'
for i in range(0,200):
if i == 100:
msg += '|'
else:
if (i % 10) == 0:
msg += '^'
else:
msg += ' '
msg += '|'
print msg
raw_input("press Enter")
def resizeImage(image):
image.resizeImage(int(raw_input("New Width (pixel): ")))
def setKnitNeedles(image):
# TODO Change to Green/Orange 100-0-100 range
startNeedle = int(raw_input("Start Needle (0 <= x <= 198): "))
stopNeedle = int(raw_input("Stop Needle (1 <= x <= 199): "))
image.setKnitNeedles(startNeedle,stopNeedle)
def setImagePosition(image):
print "Allowed options:"
print ""
print "center"
print "left"
print "right"
print "<position from left>"
print ""
image.setImagePosition(raw_input("Image Position: "))
def setStartLine(image):
image.setStartLine(int(raw_input("Start Line: ")))
def mainCallback(pSource, pString, pType):
if pType == "stream":
print pString
return
if pType == "error":
print "E: " + pString
elif pType == "debug":
print "D: " + pString
elif pType == "prompt":
print pString
raw_input("Press Enter")
return
def print_main_menu(image):
"""Print the main menu"""
print "======================"
print "= AYAB CONTROL ="
print "======================"
print "Version: " + VERSION
print "Distributed under GPL"
print ""
print "IMAGE TOOLS"
print " 1 - show"
print " 2 - invert"
print " 3 - resize"
print " 4 - rotate"
print ""
print "KNITTING"
print " 5 - set start and stop needle"
print " 6 - set image position"
print " 7 - set start line"
print " 8 - show image position"
print ""
print " 9 - knit image with current settings"
print ""
print " 0 - Exit"
print ""
print "INFORMATION"
print "Machine Type : ", options.machine_type
print "Colors : ", options.num_colors
print "Filename : ", image.filename()
print "Width : ", image.imgWidth()
print "Height : ", image.imgHeight()
print ""
print "Start Needle : ", image.knitStartNeedle()
print "Stop Needle : ", image.knitStopNeedle()
print "Start Line : ", image.startLine()
print "Image position: ", image.imgPosition()
def no_such_action():
print "Please make a valid selection"
def mainFunction(options):
"""main"""
if options.machine_type != 'single' \
and options.machine_type != 'double':
return "E: invalid machine type"
if options.machine_type == 'single' \
and options.num_colors != 2:
print "E: singlebed only supports 2 color knitting"
return
image = ayab_image.ayabImage(options.filename, \
options.num_colors)
ayabControl = ayab_control.ayabControl(mainCallback)
actions = {"1": "showImage(image)",
"2": "image.invertImage()",
"3": "resizeImage(image)",
"4": "image.rotateImage()",
"5": "setKnitNeedles(image)",
"6": "setImagePosition(image)",
"7": "setStartLine(image)",
"8": "showImagePosition(image)",
"9": "ayabControl.knitImage(image, options)"
}
while True:
os.system('cls' if os.name=='nt' else 'clear')
print_main_menu(image)
print ""
selection = raw_input("Your selection: ")
print ""
if "0" == selection:
exit()
return
toDo = actions.get(selection, "no_such_action")
eval(toDo)
return
if __name__ == '__main__':
# Parse command line options
parser = OptionParser("%prog [filename] [options]", \
description = "AYAB Control Commandline Version")
parser.add_option("-p", "--port", \
dest = "portname", \
metavar = "PORT", \
default = "/dev/ttyACM0", \
help = "Serial Port used for communication" \
" with the machine [default: %default]")
parser.add_option("-c", "--colors", \
dest = "num_colors", \
metavar = "COLORS", \
type = "int", \
default = 2, \
help = "Number of Colors of your image [default: %default]")
parser.add_option("-t", "--type", \
dest = "machine_type", \
metavar = "TYPE", \
default = "single", \
help = "Set the type of the machine (single/double bed) [default: %default]")
parser.add_option("-l", "--list", \
dest = "list", \
action ="store_true", \
default = False, \
help = "List all available serial ports and exit [default: %default]")
(options, args) = parser.parse_args()
if options.list:
print str(list(getSerialPorts()))
sys.exit(0)
if len(args):
options.filename = args[0]
mainFunction(options)
sys.exit(0)