-
Notifications
You must be signed in to change notification settings - Fork 1
/
ayab_control.py
283 lines (223 loc) · 9.79 KB
/
ayab_control.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
270
271
272
273
274
275
276
277
278
279
280
281
# -*- 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/
from ayab_communication import AyabCommunication
import time
class ayabControl(object):
def __init__(self, pCallback):
self.__callback = pCallback
self.__API_VERSION = 0x03
self.__ayabCom = AyabCommunication()
self.__formerRequest = 0
self.__lineBlock = 0
def __printDebug(self, pString):
self.__callback(self, pString, "debug")
def __printPrompt(self, pString):
self.__callback(self, pString, "prompt")
def __printStream(self, pString):
self.__callback(self, pString, "stream")
def __printError(self, pString):
self.__callback(self, pString, "error")
def __setBit(self, int_type, offset):
mask = 1 << offset
return(int_type | mask)
def __setPixel(self, bytearray, pixel):
numByte = int(pixel/8)
bytearray[numByte] = self.__setBit(int(bytearray[numByte]),pixel-(8*numByte))
return bytearray
def __checkSerial(self):
time.sleep(1) #TODO if problems in communication, tweak here
line = self.__ayabCom.read_line()
if line != '':
msgId = ord(line[0])
if msgId == 0xC1: # cnfStart
#print "> cnfStart: " + str(ord(line[1]))
return ("cnfStart", ord(line[1]))
elif msgId == 0xC3: # cnfInfo
#print "> cnfInfo: Version=" + str(ord(line[1]))
return ("cnfInfo", ord(line[1]))
elif msgId == 0x82: #reqLine
#print "> reqLine: " + str(ord(line[1]))
return ("reqLine", ord(line[1]))
else:
self.__printError("unknown message: " + line[:]) #drop crlf
return ("unknown", 0)
return("none", 0)
def __cnfLine(self, lineNumber):
imgHeight = self.__image.imgHeight()
color = 0
indexToSend = 0
sendBlankLine = False
lastLine = 0x00
# TODO optimize performance
# initialize bytearray to 0x00
bytes = bytearray(25)
for x in range(0,25):
bytes[x] = 0x00
if lineNumber < 256:
# TODO some better algorithm for block wrapping
# if the last requested line number was 255, wrap to next block of lines
if self.__formerRequest == 255 and lineNumber == 0:
self.__lineBlock += 1
# store requested line number for next request
self.__formerRequest = lineNumber
reqestedLine = lineNumber
# adjust lineNumber with current block
lineNumber = lineNumber \
+ (self.__lineBlock*256)
#########################
# decide which line to send according to machine type and amount of colors
# singlebed, 2 color
if self.__machineType == 'single' \
and self.__numColors == 2:
# color is always 0 in singlebed,
# because both colors are knitted at once
color = 0
# calculate imgRow
imgRow = lineNumber + self.__startLine
# 0 1 2 3 4 .. (imgRow)
# | | | | |
# 0 1 2 3 4 5 6 7 8 .. (imageExpanded)
indexToSend = imgRow * 2
# Check if the last line of the image was requested
if imgRow == imgHeight-1:
lastLine = 0x01
# doublebed, 2 color
elif self.__machineType == 'double' \
and self.__numColors == 2:
# calculate imgRow
imgRow = int(lineNumber/2) + self.__startLine
# 0 0 1 1 2 2 3 3 4 4 .. (imgRow)
# 0 1 2 3 4 5 6 7 8 9 .. (lineNumber)
# | | X | | X | |
# 0 1 3 2 4 5 7 6 8 9 .. (imageExpanded)
lenImgExpanded = len(self.__image.imageExpanded())
indexToSend = self.__startLine*2
# TODO more beautiful algo
if lineNumber%4 == 1 or lineNumber%4 == 2:
color = 1
else:
color = 0
if (lineNumber-2)%4 == 0:
indexToSend += lineNumber+1
elif (lineNumber-2)%4 == 1:
indexToSend += lineNumber-1
if (imgRow == imgHeight-1) \
and (indexToSend == lenImgExpanded-2):
lastLine = 0x01
else:
indexToSend += lineNumber
if (imgRow == imgHeight-1) \
and (indexToSend == lenImgExpanded-1):
lastLine = 0x01
# doublebed, multicolor
elif self.__machineType == 'double' \
and self.__numColors > 2:
# calculate imgRow
imgRow = int(lineNumber/(self.__numColors*2)) + self.__startLine
if (lineNumber % 2) == 0:
color = (lineNumber/2) % self.__numColors
indexToSend = (imgRow * self.__numColors) + color
self.__printPrompt("COLOR" + str(color))
else:
sendBlankLine = True
# TODO Check assignment
if imgRow == imgHeight-1 \
and (indexToSend == lenImgExpanded-1):
lastLine = 0x01
#########################
# assign pixeldata
imgStartNeedle = self.__image.imgStartNeedle()
imgStopNeedle = self.__image.imgStopNeedle()
# set the bitarray
for col in range(0, 200):
if color == 0 \
and self.__machineType == 'double':
if col < imgStartNeedle \
or col > imgStopNeedle:
bytes = self.__setPixel(bytes,col)
for col in range(0, self.__image.imgWidth()):
pxl = (self.__image.imageExpanded())[indexToSend][col]
# take the image offset into account
if pxl == True and sendBlankLine == False:
bytes = self.__setPixel(bytes,col+self.__image.imgStartNeedle())
# TODO implement CRC8
crc8 = 0x00
# send line to machine
self.__ayabCom.cnf_line(reqestedLine, bytes, lastLine, crc8)
# screen output
msg = str((self.__image.imageExpanded())[indexToSend])
msg += ' Image Row: ' + str(imgRow)
msg += ' (indexToSend: ' + str(indexToSend)
msg += ', reqLine: ' + str(reqestedLine)
msg += ', lineNumber: ' + str(lineNumber)
msg += ', lineBlock:' + str(self.__lineBlock) + ')'
self.__printStream(msg)
else:
self.__printError("requested lineNumber out of range")
if lastLine:
return 1 # image finished
else:
return 0 # keep knitting
def knitImage(self, pImage, pOptions):
self.__formerRequest = 0
self.__image = pImage
self.__startLine = pImage.startLine()
self.__numColors = pOptions.num_colors
self.__machineType = pOptions.machine_type
API_VERSION = self.__API_VERSION
curState = 's_init'
oldState = 'none'
if self.__ayabCom.open_serial(pOptions.portname) == False:
self.__printError("Could not open serial port")
return
while True:
# TODO catch keyboard interrupts to abort knitting
rcvMsg, rcvParam = self.__checkSerial()
if curState == 's_init':
if oldState != curState:
self.__ayabCom.req_info()
if rcvMsg == 'cnfInfo':
if rcvParam == API_VERSION:
curState = 's_start'
self.__printPrompt("Please init machine")
else:
self.__printError("wrong API version: " + str(rcvParam) \
+ (" (expected: )") + str(API_VERSION))
return
if curState == 's_start':
if oldState != curState:
self.__ayabCom.req_start(self.__image.knitStartNeedle(), \
self.__image.knitStopNeedle() )
if rcvMsg == 'cnfStart':
if rcvParam == 1:
curState = 's_operate'
self.__printPrompt("Ready to Operate")
else:
self.__printError("device not ready")
return
if curState == 's_operate':
if rcvMsg == 'reqLine':
imageFinished = self.__cnfLine(rcvParam)
if imageFinished:
curState = 's_finished'
if curState == 's_finished':
self.__printPrompt("Image finished")
return
oldState = curState
return