-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqrGen.py
202 lines (181 loc) · 8.03 KB
/
qrGen.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
import pyqrcode
outputFileName = "qrCode.nc"
# Text the QR Code will be
qrText = 'Hello World'.upper()
# Error correction level (L, M, Q or H, corresdponds to 7%, 15%, 25%, 30%):
errorLevel = "L"
# width of quiet zone (standard = 4):
quietWidth = 4
# Scale of qrCode and Box
scale = 0.8
# Invert dark and bright areas? (Good for stamps or etching for example)
invert = False
#Distance between raster Lines
spacing = 0.1
# starting Position:
# Empty = lower left (default), c = center, ul = upper left corner, ur = upper right corner, lr = lower right corner
startPos = ""
# Draw lines on the Borders of squares
sideBorders = True
# Retract Laser from corner before disabling
cornerRetract = True
# Burn dot at machine Zero?
zeroMarker = False
# Burn dots in all corners of quiet Zone?
quietMarkers = False
# Draw Box around quiet Zone?
quietBox = True
# Draw box around code?
codeBox = True
# G-Code to enable Laser
laserEnable = ";Enabling Laser\nM400\nG4 P100\nM106\nM400\n"
# G-Code to disable Laser
laserDisable = ";Disabling Laser\nM400\nM107\nM400\nG4 P250\n"
# G-Code to burn dot
laserDot = laserEnable + "G4 P200\n" + laserDisable
# Speed to use while Laser is disabled
travelSpeed = 3000
# Speed to use while Laser is enabled
cutSpeed = 1200
# show outline before beginning cutting?
outlineDemo = True
# enable Laser while showing outline? (useful if you can reduce laser power)
laserDemo = True
# G-Code to pause after outlineDemo before beginning cutting
machinePause = "M3070\n"
if not laserEnable.endswith("\n"):
laserEnable = laserEnable + "\n"
if not laserDisable.endswith("\n"):
laserDisable = laserDisable + "\n"
if not laserDot.endswith("\n"):
laserDot = laserDot + "\n"
if not machinePause.endswith("\n"):
machinePause = machinePause + "\n"
def floatTrim(f, p):
if p > 0:
floatString = ("{:10." + str(p) + "f}").format(f)
floatString = floatString.strip()
else:
floatString = int(f)
return floatString
def addSquare(gCode, xStart, yStart, xEnd, yEnd, dir, spacing, retract=True, sideBorders=True):
xMov = xEnd - xStart
yMov = yEnd - yStart
if spacing > 0:
if sideBorders:
gCode += "\nG1 X" + floatTrim(xStart, 3) + " Y" + floatTrim(yEnd, 3) + "\n"
gCode += laserEnable
gCode += "\nG0 X" + floatTrim(xStart, 3) + " Y" + floatTrim(yStart, 3) + "\n"
if not sideBorders:
gCode += laserEnable
if dir == 0:
lineN = int(xMov / spacing)
for line in range(lineN):
gCode += "G1 X" + floatTrim(xStart + line*spacing, 3) + " Y" + floatTrim(yStart, 3) + "\n"
gCode += "G1 X" + floatTrim(xStart + line*spacing, 3) + " Y" + floatTrim(yEnd, 3) + "\n"
else:
lineN = int(yMov / spacing)
for line in range(lineN + 1):
gCode += "G1 X" + floatTrim(xStart, 3) + " Y" + floatTrim(yStart + line*spacing, 3) + "\n"
gCode += "G1 X" + floatTrim(xEnd, 3) + " Y" + floatTrim(yStart + line*spacing, 3) + "\n"
gCode += "G1 X" + floatTrim(xStart, 3) + " Y" + floatTrim(yEnd, 3) + "\n"
gCode += "G1 X" + floatTrim(xEnd, 3) + " Y" + floatTrim(yEnd, 3) + "\n"
if sideBorders:
gCode += "\nG1 X" + floatTrim(xEnd, 3) + " Y" + floatTrim(yStart, 3) + "\n"
else:
gCode += "\nG0 X" + floatTrim(xEnd, 3) + " Y" + floatTrim(yStart, 3) + "\n"
gCode += laserEnable
gCode += "\nG0 X" + floatTrim(xEnd, 3) + " Y" + floatTrim(yEnd, 3) + "\n"
gCode += "\nG0 X" + floatTrim(xStart, 3) + " Y" + floatTrim(yEnd, 3) + "\n"
gCode += "\nG0 X" + floatTrim(xStart, 3) + " Y" + floatTrim(yStart, 3) + "\n"
gCode += "\nG0 X" + floatTrim(xEnd, 3) + " Y" + floatTrim(yStart, 3) + "\n"
if retract:
gCode += "G1 X" + floatTrim(xEnd - 0.5 * (yMov/abs(yMov)) * scale, 3) + " Y" + floatTrim(yEnd - 0.5 * (yMov/abs(yMov)) * scale, 3) + "\n"
gCode += laserDisable
return gCode
qrCode = pyqrcode.create(qrText, error=errorLevel)
qrCode = qrCode.text(quiet_zone=quietWidth)
if invert:
qrCode = qrCode.replace("0", "X")
qrCode = qrCode.replace("1", "0")
qrCode = qrCode.replace("X", "1")
qrCode = qrCode.strip().split("\n")
qrWidth = len(qrCode)
startPos = startPos.lower()
xOffset = 0
yOffset = 0
startPos = "lower left"
if startPos == "c" or startPos == "center":
xOffset = qrWidth*scale / -2
yOffset = qrWidth*scale / -2
startPos = "center"
if startPos == "ul" or startPos == "upperleft":
yOffset = -qrWidth*scale
startPos = "upper left"
if startPos == "ur" or startPos == "upperright":
xOffset = -qrWidth*scale
yOffset = -qrWidth*scale
startPos = "upper right"
if startPos == "lr" or startPos == "lowerright":
xOffset = -qrWidth*scale
startPos = "lower right"
gCode = ";G-Code Generated by qrGen.py\n;START POS: " + startPos + "\n"
gCode += ";DIMENSIONS: " + str(qrWidth) + "*" + str(qrWidth) + " Modules, quietZone width=" + str(quietWidth) + " Modules\n"
gCode += ";DIMENSIONS: " + floatTrim(qrWidth*scale, 1) + "*" + floatTrim(qrWidth*scale, 1) + " mm, quietZone width=" + floatTrim(quietWidth*scale, 1) + " mm\n"
gCode += "G90\nG0 F" + str(travelSpeed) + "\nG1 F" + str(cutSpeed) + "\n"
if outlineDemo:
gCode += "G0 X" + floatTrim(xOffset, 3) + " Y" + floatTrim(yOffset, 3) + " Z0\n"
if laserDemo:
gCode += laserEnable
gCode += "G1 X" + floatTrim(xOffset, 3) + " Y" + floatTrim(qrWidth*scale + yOffset, 3) + "\n"
gCode += "G1 X" + floatTrim(qrWidth*scale + xOffset, 3) + " Y" + floatTrim(qrWidth*scale + yOffset, 3) + "\n"
gCode += "G1 X" + floatTrim(qrWidth*scale + xOffset, 3) + " Y" + floatTrim(yOffset, 3) + "\n"
gCode += "G1 X" + floatTrim(xOffset, 3) + " Y" + floatTrim(yOffset, 3) + "\n"
if laserDemo:
gCode += laserDisable
gCode += machinePause
if zeroMarker:
gCode += laserDot
elif quietBox or quietMarkers:
gCode += "G0 X" + floatTrim(xOffset, 3) + " Y" + floatTrim(yOffset, 3) + " Z0\n"
if quietBox:
gCode += laserEnable
if quietMarkers:
gCode += laserDot
gCode += "G1 X" + floatTrim(xOffset, 3) + " Y" + floatTrim(qrWidth*scale + yOffset, 3) + "\n"
if quietMarkers:
gCode += laserDot
gCode += "G1 X" + floatTrim(qrWidth*scale + xOffset, 3) + " Y" + floatTrim(qrWidth*scale + yOffset, 3) + "\n"
if quietMarkers:
gCode += laserDot
gCode += "G1 X" + floatTrim(qrWidth*scale + xOffset, 3) + " Y" + floatTrim(yOffset, 3) + "\n"
if quietMarkers:
gCode += laserDot
gCode += "G1 X" + floatTrim(xOffset, 3) + " Y" + floatTrim(yOffset, 3) + "\n"
if quietBox:
gCode += laserDisable
if codeBox:
gCode += "G0 X" + floatTrim(quietWidth * scale + xOffset, 3) + " Y" + floatTrim(quietWidth * scale + yOffset, 3) + "\n"
gCode += laserEnable
gCode += "G1 X" + floatTrim(quietWidth * scale + xOffset, 3) + " Y" + floatTrim((qrWidth-quietWidth)*scale + yOffset, 3) + "\n"
gCode += "G1 X" + floatTrim((qrWidth-quietWidth) * scale + xOffset, 3) + " Y" + floatTrim((qrWidth-quietWidth)*scale + yOffset, 3) + "\n"
gCode += "G1 X" + floatTrim((qrWidth-quietWidth) * scale + xOffset, 3) + " Y" + floatTrim(quietWidth*scale + yOffset, 3) + "\n"
gCode += "G1 X" + floatTrim(quietWidth * scale + xOffset, 3) + " Y" + floatTrim(quietWidth * scale + yOffset, 3) + "\n"
gCode += laserDisable
for lineN in range(len(qrCode[::-1])):
line = qrCode[lineN]
inLine = False
for point in range(len(line)):
if line[point] == "1" and not inLine:
inLine = True
lineStart = point
if (line[point] == "0" or point==len(line)-1) and inLine:
inLine = False
if point==len(line)-1:
point += 1
gCode = addSquare(gCode, lineStart*scale + xOffset, lineN*scale + yOffset, point*scale + xOffset, (lineN+1)*scale + yOffset, 1, spacing, retract=cornerRetract, sideBorders=sideBorders)
gCode += "G0 X0 Y0\n"
gCode += "G4 P500\n" # wait before shutting down steppers to make sure everything is stationary
f = open(outputFileName, "w")
f.write(gCode)
f.close()