forked from AllYarnsAreBeautiful/ayab-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialtest.py
213 lines (174 loc) · 5.32 KB
/
serialtest.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
# -*- 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, Christian Gerbrandt
# https://github.com/AllYarnsAreBeautiful/ayab-desktop
import time
import serial
import sys
import Image
#
# HELPER FUNCTIONS
#
def setBit(int_type, offset):
mask = 1 << offset
return(int_type | mask)
def setPixel(bytearray,pixel):
_numByte = int(pixel/8)
bytearray[_numByte] = setBit(int(bytearray[_numByte]),pixel-(8*_numByte))
return
#
# ACTIONS
#
def a_reqInfo():
print("< reqInfo")
ser.write(chr(0x03) + '\n\r')
def a_reqStart( ):
startNeedle = eval(input("Start Needle: "))
stopNeedle = eval(input("Stop Needle : "))
msg = chr(0x01) #msg id
msg += chr(int(startNeedle))
msg += chr(int(stopNeedle))
print("< reqStart")
ser.write(msg + '\n\r')
def a_cnfLine():
lineNumber = eval(input("Line Number : "))
lineNumber = int(lineNumber)
lineData = ''
while len(lineData) != 25:
lineData = eval(input("Line Data : "))
lastLine = eval(input("Last Line (0/1): "))
flags = int(lastLine)
crc8 = 0x00
print((type(lineData)))
cnfLine(lineNumber, lineData, flags, crc8)
def a_printImage():
msg = chr(0x01) #msg id
msg += chr(int(0))
msg += chr(int(199))
print("< reqStart")
ser.write(msg + '\n\r')
while True:
checkSerial( True )
def a_showImage():
for y in range(0, imageH):
msg = ''
for x in range(0, imageW):
pxl = image.getpixel((x, y))
if pxl == 255:
msg += "#"
else:
msg += '-'
print(msg)
def sendLine(lineNumber):
bytes = bytearray(25)
if lineNumber < imageH:
msg = ''
for x in range(0, imageW):
pxl = image.getpixel((x, lineNumber))
if pxl == 255:
msg += "#"
setPixel(bytes,x)
else:
msg += '-'
print((msg + str(lineNumber)))
if lineNumber == imageH-1:
lastLine = 0x01
else:
lastLine = 0x00
cnfLine(lineNumber, bytes, lastLine, 0x00)
def cnfLine(lineNumber, lineData, flags, crc8):
msg = chr(0x42) # msg id
msg += chr(lineNumber) # line number
msg += lineData # line data
msg += chr(flags) # flags
msg += chr(crc8) # crc8
print("< cnfLine")
ser.write(msg + '\n\r')
def checkSerial( printMode ):
time.sleep(1)
out = ''
while ser.inWaiting() > 0:
line = ser.readline()
msgId = ord(line[0])
if msgId == 0xC1: # cnfStart
msg = "> cnfStart: "
if(ord(line[1])):
msg += "success"
else:
msg += "failed"
print(msg)
elif msgId == 0xC3: # cnfInfo
msg = "> cnfInfo: Version="
msg += str(ord(line[1]))
print(msg)
elif msgId == 0x82: #reqLine
msg = "> reqLine: "
msg += str(ord(line[1]))
print(msg)
if printMode:
sendLine(ord(line[1]))
else:
print((line[:-2])) #drop crlf
def no_such_action():
print("Please make a valid selection")
def print_menu():
print("===================")
print("= AYAB TEST =")
print("===================")
print("= andz & chris007 =")
print("= v1 =")
print("===================")
print(("Image: " + filename))
print((img.format, img.size, img.mode))
print("")
print("1 - reqInfo")
print("2 - reqStart")
print("3 - cnfLine")
print("")
print("4 - show image")
print("5 - print image")
print("")
print("0 - Exit")
def mainFunction():
actions = {"1": a_reqInfo, "2": a_reqStart, "3": a_cnfLine, "4": a_showImage, "5": a_printImage}
print_menu()
while True:
print("")
selection = eval(input("Your selection: "))
print("")
if "0" == selection:
ser.close()
exit()
return
toDo = actions.get(selection, no_such_action)
toDo()
checkSerial( False )
filename = sys.argv[1]
if filename != '':
img = Image.open(filename)
image = img.convert('1')
imageW = image.size[0]
imageH = image.size[1]
maxWidth = 200
if imageW > maxWidth:
wpercent = (maxWidth/float(imageW))
hsize = int((float(imageH)*float(wpercent)))
image = image.resize((maxWidth,hsize), Image.ANTIALIAS)
imageW = image.size[0]
imageH = image.size[1]
ser = serial.Serial('/dev/ttyACM0', 115200)
mainFunction()