-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletterform.pde
374 lines (343 loc) · 13 KB
/
letterform.pde
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import processing.serial.*;
Serial myPort; // Create object from Serial class
Plotter plotter; // Create a plotter object
int serialPort=2; // select the port
int val; // Data received from the serial port
int lf = 10; // ASCII linefeed
PFont font;
//Enable plotting?
final boolean PLOTTING_ENABLED = false;
//Label
String label1= "BYE BYE\nWHAT IS?";
String label5 = "IL Y A\nUNE ERREUR\nDANS\nLE SYSTEM?";
String label2= "IL FAUT\nDETRUIRE\nLA SYNTAXE";
String label6= "JE\nM'EXCUSE\nPOUR\nL'ERREUR\nPRECEDENTE";
String label4= "DIFFERENT PREDATORS. DIFFERENT WORDS AND WHEELS. BUT THE SAME SKY. THAT'S THE DARK AGE WE STILL LIVE IN TODAY.";
String label7= "YOU CAN\nDESCRIBE YOUR\nOWN LANGUAGE IN\nYOUR OWN\nLANGUAGE:\nBUT NOT QUITE.\nYOU CAN\nINVESTIGAE YOUR\nOWN BRAIN BY\nMEANS OF YOUR\nOWN BRAIN:\nBUT NOT QUITE.";
String label= "";
boolean ambigFlag = false;
boolean poesieLoaded = false;
boolean directionChange = false;
String ambigousLetters = "EORB?T";
String SpecialCharacter = "";
JSONArray poesieJSON;
JSONArray poesieTextJSON;
JSONObject poesieObject;
JSONObject textLine;
String ambigousLabel = "";
int poesieNumber = 9; // select inital poesie in json file
int poesieLines = 1;
int ambigousPen = 3;
float ambigousSpeed = 9;
float fontSize = 1; // text size in cm
float plotSize = 0;
float writeSpeed = 9;
float paperHeight = 402; // in milimeters A3 -> for A4 change to
float paperWidth = 275; // in milimeters A3 -> for A4 change to
//Plotter starting
int xPos_mm = 25;
int yPos_mm = 1;
int horizontalDirection = 0;
int verticalDirection = 1;
//Plotter initial pen number
int penNumber = 2;
//Plotter speed
float pSpeed = writeSpeed;
int vsOld; // last speed
//Let's set this up
void setup(){
fullScreen(); // to use the whole screen, projector etc
//size(1080, 750);
background(200);
//smooth();
//textSize(26);
font = loadFont("HelveticaNeue-Light-48.vlw");
fill(0);
textFont(font,36);
// JSON import
poesieJSON = loadJSONArray("poesie.json");
loadPoesie(0); // not actually, to allow application to start without plotting
poesieLoaded = false;
//Select a serial port
println(Serial.list()); //Print all serial ports to the console
int portNumber = Serial.list().length;
println("Number of ports: " + portNumber + ", port selected: " + serialPort);
if (serialPort>=portNumber){
serialPort = portNumber-1;
println("Selected port not available, changing to last port...");
}
//String portName = Serial.list()[serialPort]; //make sure you pick the right one
String portName = "/dev/ttys007"; // for Moxa Device
println("Plotting to port: " + portName);
//Open the port
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil(lf);
//Associate with a plotter object
plotter = new Plotter(myPort);
//Initialize plotter
plotter.write("IN;");
//this was calculated
plotTextSize(fontSize,fontSize*2);
plotDirection(horizontalDirection, verticalDirection);
plotSpeed(pSpeed);
delay(4000);
// plotSpacing(0,-0.5); not supported
//Wait 0.5 second per character while printing label
/* if (PLOTTING_ENABLED) {
delay(label.length() * 500);
}
*/
}
void draw(){
if (poesieLoaded) { // this gets loaded after number key pressed, see dowmwards
for (int l=0; l<poesieLines; l=l+1){ // getting the text lines from JSON poesie
textLine = poesieTextJSON.getJSONObject(l);
penNumber = textLine.getInt("pen_number");
writeSpeed = textLine.getFloat("write_speed");
label = textLine.getString("line");
plotPenselect(penNumber);
plotSpeed(writeSpeed);
plotTextSize(fontSize,fontSize*2);
plotPosition(xPos_mm,yPos_mm);
println("*** now starting to plot ***"); // take the label and individually sent the letters to the plotter
println("Plotting text " + l+1 + "/" + poesieLines + ": " + label);
println("Number of characters :" + label.length());
for (int i=0; i < label.length(); i = i+1){
char c = label.charAt(i);
char cnew = evaluateLetter(c); // send to evaluation
if (cnew == '\n'){
println("Now making a linefeed");
plotNewline();
plotLetterPosition(0,0.2); // reduce linefeed distance
//plotLabel(str(cnew));
} else if (cnew == '\r') {
println("Now making a carriage return");
plotLabel(str(cnew));
}
else if (cnew == '\t') {
println("Now making a turn");
plotSwitchDirection();
} else if (cnew == '\b') {
println("Now plotting a special character");
plotter.write(SpecialCharacter);
}
else {
print();
print(" " + cnew);
plotLabel(str(cnew));
}
}
horizontalDirection = 0;
verticalDirection = 1;
plotDirection(horizontalDirection, verticalDirection);
plotPosition(0,0); // show the paper
plotPenselect(0);
delay(5000 * int(10 / writeSpeed));
}
if (ambigFlag == true) { // if there was an ambigous letter then...
if (ambigousLabel != null){ // if there is an ambigous text specified in the json, take it
label = ambigousLabel;
}
plotPenselect(ambigousPen);
plotSpeed(ambigousSpeed);
println("Overwriting ambigous letters now...");
//plotPenselect(3); // draw now in red
plotPosition(xPos_mm,yPos_mm); // go to initial position (multiline)
//plotLetterPosition(-label.length(), 0); // go back to the latest place
for (int i=0; i < label.length(); i = i+1){
char c = label.charAt(i);
char cnew = evaluateAmbigLetter(c); // send to evaluation
print("" + cnew);
}
}
plotPosition(0,0);
plotPenselect(0);
println("Finished plotting. Waiting for next poetry.");
horizontalDirection = 0;
verticalDirection = 1;
plotDirection(horizontalDirection, verticalDirection);
directionChange = false; // resetting values for next plot
poesieLoaded = false;
}
}
// functions :
void plotLabel(String text){
//Draw a label at the end
//println(text);
if (text.equals(" ")) {
plotter.write("LB" + text + char(3),400);
//println("Delay" + 300);
} else {
plotter.write("LB" + text + char(3), int(1200 *10 / writeSpeed)); //Draw label, char(3)= terminator
}
}
void plotPenselect(int penNumber){
//Send pen selection to plotter
println("Pen slection: " + penNumber);
plotter.write("SP" + penNumber + ";");
delay(3000); // let the plotter change the pen
}
/* void plotSpacing(float spacing, float lining){
//Send spacing and linefeed to plotter
println("Spacing / Linefeed: " + spacing + "/ " + lining);
plotter.write("ES" + spacing + "," + lining + ";");
} */ // not supported in HPGL-1
void plotLetterPosition(float letterposX, float letterposY){
// move cursor to x and y places of letters
println("Move by " + letterposX + " places horizontally, " + letterposY + " vertically");
plotter.write("CP" + letterposX + "," + letterposY + ";",1000);
}
void plotNewline(){
// make a carriage return and a new line
//println("New line...");
plotter.write("CP;",750);
}
void plotPosition(int xPos, int yPos){ // now in mm
float xUnits = xPos / 0.025;
float yUnits = yPos / 0.025; // calculation for plotter units / resolution
plotter.write("PU"+xUnits+"," + yUnits + ";",1000); // position pen
}
void plotSpeed(float speed){
println("Plotter speed: " + speed + " cm/s");
plotter.write("VS" + speed + ";");
}
void plotTextSize(float sizeL, float sizeH){
plotter.write("SI" + sizeL + "," + sizeH + ";");
}
void plotDirection(int directCourse, int directElevation){
plotter.write("DI" + directCourse + "," + directElevation + ";");
}
void plotSwitchDirection(){
String direction = str(horizontalDirection) + str(verticalDirection);
println("HPGL direction is " + direction);
switch (direction){
case "01":
horizontalDirection = 1;
verticalDirection = 0;
println("switching direction to 90°");
break;
case "10":
horizontalDirection = 0;
verticalDirection = -1;
println("switching direction to 135°");
break;
case "0-1":
horizontalDirection = -1;
verticalDirection = 0;
println("switching direction to 180°");
break;
case "-10":
horizontalDirection = 0;
verticalDirection = 1;
println("switching direction to 0°");
break;
}
plotDirection(horizontalDirection, verticalDirection);
}
void loadPoesie(int poesieNr){ // loading from JSON file
// displaying the names of poetry
text("/// PLAIN IFXI 2.0 ///", 100, 50);
for (int p=0; p<poesieJSON.size(); p=p+1){
poesieObject = poesieJSON.getJSONObject(p);
if (p == poesieNr){
fill(180, 50, 50);
} else { fill(0);}
text(p + " / " + poesieObject.getString("name") + " [" + poesieObject.getString("author") + "]",100,125+50*p);
println(poesieObject.getString("name"));
}
println("Number of poesie found: " + poesieJSON.size());
println("Looking for poesie number " + poesieNr);
poesieObject = poesieJSON.getJSONObject(poesieNr);
ambigousLabel = poesieObject.getString("ambigous line");
if (poesieObject.isNull("pen_number") == false){
ambigousPen = poesieObject.getInt("pen_number");
println("Ambigous pen number is set to:" + ambigousPen);}
if (poesieObject.isNull("write_speed") == false){
ambigousSpeed = poesieObject.getFloat("write_speed");}
println("Ambigous is " + ambigousLabel);
println("Plotting poesie: " + poesieObject);
String poesieName = poesieObject.getString("name");
println ("Poesie selected: " + poesieName);
ambigousLetters = poesieObject.getString("ambigous");
println ("Ambigous letters used: " + ambigousLetters);
fontSize = poesieObject.getFloat("font size");
println ("Setting font size of " + fontSize + "cm.");
if (poesieObject.isNull("direction change") == false){
directionChange = poesieObject.getBoolean("direction change");
println("Change direction activated. " + directionChange);
}
poesieTextJSON = poesieObject.getJSONArray("text lines");
poesieLines = poesieObject.getJSONArray("text lines").size();
println(poesieLines + " Text(s) in this poesie.");
textLine = poesieTextJSON.getJSONObject(0);
String labelLines = textLine.getString("line");
int lineCounter = 0;
int characterCounter = 0;
int maxCharacterCounter = 0;
float lineLength = 0;
for (int k=0; k<labelLines.length(); k=k+1){
char d = labelLines.charAt(k);
characterCounter++;
if (d == '\n' || d =='\t'){
lineCounter++;
if (characterCounter > maxCharacterCounter){
maxCharacterCounter = characterCounter;
characterCounter = 0;
}
}
}
if (characterCounter > maxCharacterCounter){
maxCharacterCounter = characterCounter;
characterCounter = 0;
}
plotSize = (lineCounter)*(4*fontSize-4*fontSize*0.2)+fontSize*2; // total size = lines-1 * 2* fontsize * 2 height + 2* fontsize
lineLength = maxCharacterCounter * fontSize;
println("Plotting poesie will take " + lineCounter + " lines and this will be : " + plotSize +"cm.");
println("Maximal line length is " + lineLength + "cm for " + maxCharacterCounter + " characters at " + fontSize + "cm font width.");
if (lineLength * 10 > paperWidth) {
fontSize = (paperWidth - fontSize*20) / (maxCharacterCounter * 10); // from centimeters to milimeters
println("*** At least one line has too many characters and plotting will be partial. ***");
println("*** So reducing font size to plot everything. ***");
plotSize = (lineCounter)*(4*fontSize-4*fontSize*0.2)+fontSize*2; // total size = lines-1 * 2* fontsize * 2 height + 2* fontsize
lineLength = maxCharacterCounter * fontSize;
println("*** New font size " + fontSize + "cm. New line length " + lineLength + "cm. New plotSize " + plotSize + "cm. ***");
}
println("[JSON] Poesie loaded.");
println();
// calculate poetry position on paper
xPos_mm = int((paperHeight / 2) - plotSize*10/2); // A3 plotter area is 401 mm long >>> Golden / middle position. A3 420 * 297 mm
if (directionChange == true){
yPos_mm = int((paperWidth / 2 ) - (lineLength * 10 / 2 - fontSize * 10));
println("Plot tries to be centered at " + yPos_mm + "mm from left bord");
} else {
yPos_mm = 10;
}
poesieLoaded = true;
}
void keyReleased() {
if (key == 'P' || key == 'p') {
plotTextSize(fontSize,fontSize*2);
plotDirection(1,0);
plotPosition(500,400);
plotSpeed(pSpeed);
plotPenselect(4);
plotLabel(label);
plotLetterPosition(-label.length(), 0);
//plotPosition(500,600);
plotLabel(label2);
/*plotPosition(500,400);
plotLabel(label3);
plotPosition(500,200);
plotLabel(label4); */
delay(5000);
exit();
} else if (key == 'A' || key == 'a') {
} else if (key == 'Q' || key == 'q') {
exit();
} else if (key == '0' || key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8'|| key == '9') {
int selectKey = int(str(key));
println("Key pressed is " + selectKey);
loadPoesie(selectKey);
delay(2000);
}
}