Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- // The serial port
- Serial myPort;
- String serialLine = "";
- String c = "";
- boolean started = false;
- boolean finished = false;
- boolean commandComplete = true;
- int commandCount = 0;
- int ypos = 20;
- String temperature = "?";
- String gcode[] = {
- };
- int gcodeIndex = 0;
- void setup()
- {
- //init stuff
- size(500, 600);
- //smooth();
- // List all the available serial ports
- println(Serial.list());
- //open the first port...
- myPort = new Serial(this, Serial.list()[1], 38400);
- //load our gcode lines
- gcode = loadStrings("test.gcode");
- // println(gcode);
- background(20, 20, 20);
- }
- void draw()
- {
- PFont font;
- font = loadFont("ArialMT-48.vlw");
- textFont(font, 16);
- ypos =20 + gcodeIndex*20;
- if(ypos>500)
- {
- background(20, 20, 20);
- ypos = 20;
- }
- fill(250, 250, 250);
- String code = "[" +gcodeIndex +"] : "+ c;
- text(code,10,ypos);
- while (myPort.available() > 0)
- {
- int inByte = myPort.read();
- if (inByte == '\n')
- {
- println("Got: " + serialLine);
- if (match(serialLine, "^start") != null)
- started = true;
- if (match(serialLine, "^ok") != null)
- {
- commandComplete = true;
- fill(0, 240, 10);
- text("ok",400,ypos);
- if (gcodeIndex == gcode.length)
- println("Job's done!");
- }
- serialLine = "";
- }
- else if (inByte > 0)
- {
- serialLine += (char)inByte;
- }
- }
- if (started)
- {
- if (commandComplete)
- {
- String cmd = getNextCommand();
- commandComplete = false;
- if (gcodeIndex == gcode.length)
- {
- finished = true;
- fill(0, 240, 10);
- ypos+=50;
- text("DONE",10,ypos);
- }
- if (cmd != null)
- {
- println("Sent: " +cmd);
- myPort.write(cmd);
- }
- }
- }
- if (started && finished)
- {
- println("Job's done!");
- started = false;
- finished = false;
- }
- }
- String getNextCommand()
- {
- if (gcodeIndex < gcode.length)
- {
- c = gcode[gcodeIndex];
- gcodeIndex++;
- if (match(c, "^[A-Z]{1}") == null)
- c = getNextCommand();
- return c;
- }
- else
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement