-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHelloWorld.pde
110 lines (86 loc) · 2.18 KB
/
HelloWorld.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
import processing.net.*;
/*
Very simple example of how to use the Line-us API. The most important thing to
remember is to read the response after sending a command. The TCP buffer on
Line-us is small and it does not cope well with having to buffer more than
one message.
To run this example just copy and paste the code into your Processing window.
Good Luck!
*/
LineUs myLineUs;
void setup() {
myLineUs = new LineUs(this);
}
void draw() {
delay(1000);
myLineUs.g01(900, 300, 0);
myLineUs.g01(900, -300, 0);
myLineUs.g01(900, -300, 1000);
myLineUs.g01(1200, 300, 0);
myLineUs.g01(1200, -300, 0);
myLineUs.g01(1200, -300, 1000);
myLineUs.g01(900, 0, 0);
myLineUs.g01(1200, 0, 0);
myLineUs.g01(1200, 0, 1000);
myLineUs.g01(1500, 150, 0);
myLineUs.g01(1500, -300, 0);
myLineUs.g01(1500, -300, 1000);
myLineUs.g01(1500, 250, 0);
myLineUs.g01(1500, 300, 0);
myLineUs.g01(1500, 300, 1000);
delay(1000);
exit();
}
//An example class to show how to use the Line-us API
class LineUs {
Client lineUs;
Boolean connected = false;
String helloMessage;
LineUs(PApplet papp) {
lineUs = new Client(papp, "line-us.local", 1337);
connected = true;
helloMessage = readResponse();
}
String getHelloString() {
if(connected) {
return helloMessage;
} else {
return("Not connected");
}
}
//Close the connection to the Line-us
void disconnect() {
lineUs.stop();
connected = false;
}
//Send a G01 (interpolated move), and wait for the response before returning
void g01(int x, int y, int z) {
String cmd = "G01 X";
cmd += str(x);
cmd += " Y";
cmd += str(y);
cmd += " Z";
cmd += str(z);
sendCommand(cmd);
readResponse();
}
//Read from the socket one byte at a time until we get a null
String readResponse() {
String line = "";
int c;
while(true) {
c = lineUs.read();
if(c != 0 && c != -1) {
line += (char) c;
} else if(c == 0) {
break;
}
}
return line;
}
//Send the command to Line-us
void sendCommand(String command) {
command += "\0";
lineUs.write(command);
}
}