|
| 1 | +import processing.net.*; |
| 2 | + |
| 3 | +/* |
| 4 | + Very simple example of how to use the Line-us API. The most important thing to |
| 5 | + remember is to read the response after sending a command. The TCP buffer on |
| 6 | + Line-us is small and it does not cope well with having to buffer more than |
| 7 | + one message. |
| 8 | +
|
| 9 | + Good Luck! |
| 10 | +*/ |
| 11 | + |
| 12 | +LineUs myLineUs; |
| 13 | + |
| 14 | +void setup() { |
| 15 | + myLineUs = new LineUs(this); |
| 16 | +} |
| 17 | + |
| 18 | +void draw() { |
| 19 | + |
| 20 | + delay(1000); |
| 21 | + |
| 22 | + myLineUs.g01(900, 300, 0); |
| 23 | + myLineUs.g01(900, -300, 0); |
| 24 | + myLineUs.g01(900, -300, 1000); |
| 25 | + |
| 26 | + myLineUs.g01(1200, 300, 0); |
| 27 | + myLineUs.g01(1200, -300, 0); |
| 28 | + myLineUs.g01(1200, -300, 1000); |
| 29 | + |
| 30 | + myLineUs.g01(900, 0, 0); |
| 31 | + myLineUs.g01(1200, 0, 0); |
| 32 | + myLineUs.g01(1200, 0, 1000); |
| 33 | + |
| 34 | + myLineUs.g01(1500, 150, 0); |
| 35 | + myLineUs.g01(1500, -300, 0); |
| 36 | + myLineUs.g01(1500, -300, 1000); |
| 37 | + |
| 38 | + myLineUs.g01(1500, 250, 0); |
| 39 | + myLineUs.g01(1500, 300, 0); |
| 40 | + myLineUs.g01(1500, 300, 1000); |
| 41 | + |
| 42 | + delay(1000); |
| 43 | + |
| 44 | + exit(); |
| 45 | +} |
| 46 | + |
| 47 | +//An example class to show how to use the Line-us API |
| 48 | + |
| 49 | +class LineUs { |
| 50 | + |
| 51 | + Client lineUs; |
| 52 | + Boolean connected = false; |
| 53 | + String helloMessage; |
| 54 | + |
| 55 | + LineUs(PApplet papp) { |
| 56 | + lineUs = new Client(papp, "line-us.local", 1337); |
| 57 | + connected = true; |
| 58 | + helloMessage = readResponse(); |
| 59 | + } |
| 60 | + |
| 61 | + String getHelloString() { |
| 62 | + if(connected) { |
| 63 | + return helloMessage; |
| 64 | + } else { |
| 65 | + return("Not connected"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + //Close the connection to the Line-us |
| 70 | + void disconnect() { |
| 71 | + lineUs.stop(); |
| 72 | + connected = false; |
| 73 | + } |
| 74 | + |
| 75 | + //Send a G01 (interpolated move), and wait for the response before returning |
| 76 | + void g01(int x, int y, int z) { |
| 77 | + String cmd = "G01 X"; |
| 78 | + cmd += str(x); |
| 79 | + cmd += " Y"; |
| 80 | + cmd += str(y); |
| 81 | + cmd += " Z"; |
| 82 | + cmd += str(z); |
| 83 | + sendCommand(cmd); |
| 84 | + readResponse(); |
| 85 | + } |
| 86 | + |
| 87 | + //Read from the socket one byte at a time until we get a null |
| 88 | + String readResponse() { |
| 89 | + String line = ""; |
| 90 | + int c; |
| 91 | + while(true) { |
| 92 | + c = lineUs.read(); |
| 93 | + if(c != 0 && c != -1) { |
| 94 | + line += (char) c; |
| 95 | + } else if(c == 0) { |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + return line; |
| 100 | + } |
| 101 | + |
| 102 | + //Send the command to Line-us |
| 103 | + void sendCommand(String command) { |
| 104 | + command += "\0"; |
| 105 | + lineUs.write(command); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments