-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtableInput.pde
106 lines (84 loc) · 3.19 KB
/
tableInput.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
// Principally, this script ensures that a string is "caught" from Colortizer
// via UDP and coded into principal inputs of:
// - tablePieceInput[][] or tablePieceInput[][][2] (rotation)
// - UMax, VMax
int portIN = 6152;
import hypermedia.net.*;
UDP udp; // define the UDP object
boolean busyImporting = false;
boolean changeDetected = false;
boolean outputReady = false;
int inputUMax = 18;
int inputVMax = 22;
int IDMax = 15;
int legoPerPiece = 4;
int displayV = inputVMax*legoPerPiece; // Height of Lego Table
int displayU = inputUMax*legoPerPiece; // Width of Lego Table
// Arrays that holds ID information of rectilinear tile arrangement.
int tablePieceInput[][][] = new int[displayU/4][displayV/4][2];
void initUDP() {
udp = new UDP( this, portIN );
//udp.log( true ); // <-- printout the connection activity
udp.listen( true );
}
void ImportData(String inputStr[]) {
if (inputStr[0].equals("COLORTIZER")) {
parseColortizerStrings(inputStr);
}
busyImporting = false;
}
void parseColortizerStrings(String data[]) {
for (int i=0 ; i<data.length;i++) {
String[] split = split(data[i], "\t");
// Checks maximum possible ID value
if (split.length == 2 && split[0].equals("IDMax")) {
IDMax = int(split[1]);
}
// Checks if row format is compatible with piece recognition. 3 columns for ID, U, V; 4 columns for ID, U, V, rotation
if (split.length == 3 || split.length == 4) {
//Finds UV values of Lego Grid:
int u_temp = int(split[1]);
//int v_temp = tablePieceInput.length - int(split[2]) - 1;
int v_temp = int(split[2]);
if (split.length == 3 && !split[0].equals("gridExtents")) { // If 3 columns
// detects if different from previous value
if ( v_temp < tablePieceInput.length && u_temp < tablePieceInput[0].length ) {
if ( tablePieceInput[v_temp][u_temp][0] != int(split[0]) ) {
// Sets ID
tablePieceInput[v_temp][u_temp][0] = int(split[0]);
changeDetected = true;
}
}
} else if (split.length == 4) { // If 4 columns
// detects if different from previous value
if ( v_temp < tablePieceInput.length && u_temp < tablePieceInput[0].length ) {
if ( tablePieceInput[v_temp][u_temp][0] != int(split[0]) || tablePieceInput[v_temp][u_temp][1] != int(split[3])/90 ) {
// Sets ID
tablePieceInput[v_temp][u_temp][0] = int(split[0]);
//Identifies rotation vector of piece [WARNING: Colortizer supplies rotation in degrees (0, 90, 180, and 270)]
tablePieceInput[v_temp][u_temp][1] = int(split[3])/90;
changeDetected = true;
}
}
}
}
}
}
void receive( byte[] data, String ip, int port ) { // <-- extended handler
// get the "real" message =
String message = new String( data );
//println("catch!");
//println(message);
//saveStrings("data.txt", split(message, "\n"));
String[] split = split(message, "\n");
if (!busyImporting) {
busyImporting = true;
ImportData(split);
}
useMouse = false;
}
void sendCommand(String command, int port) {
String dataToSend = "";
dataToSend += command;
udp.send( dataToSend, "localhost", port );
}