summaryrefslogtreecommitdiff
path: root/trunk/users/hoeken/arduino/GCode_Host/GCode_Host.pde
blob: 6d8f4360f7d8957d0cd78ba5405d901257c74835 (plain)
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
import processing.serial.*;


// The serial port
Serial myPort;
String serialLine = "";

boolean started = false;
boolean finished = false;
boolean commandComplete = true;
int commandCount = 0;

String temperature = "?";

String gcode[] = {
};
int gcodeIndex = 0;

PFont font;

void setup()
{
  //init stuff
  size(1024, 600);
  //smooth();
           
  // List all the available serial ports
  println(Serial.list());
  //open the first port...
  myPort = new Serial(this, Serial.list()[0], 19200);
 

  //load our gcode lines
  
  gcode = loadStrings("job.gcode");

  font = loadFont("ArialMT-48.vlw"); 
}

int queueLength = 2;
int commandsInQueue = 0;



void draw()
{
  background(20, 20, 20);
  

  while (myPort.available() > 0)
  {
    int inByte;
    
    inByte = myPort.read();

    if (inByte == '\n')
    {
      println("Got: " + serialLine);

      String m[] = match(serialLine, "^T:([0-9]+)");
      ;
      if (m != null)
        temperature = m[0];

      if (match(serialLine, "^start") != null)
      {
        started = true;
      }
      if (match(serialLine, "^ok") != null)
      {
        commandsInQueue--;

        if (gcodeIndex == gcode.length)
          println("Job's done!");
      }

      serialLine = "";
    }
    else if (inByte > 0)
    {
      serialLine += (char)inByte;
    }
  }

  if (started)
  {
    if (commandsInQueue < queueLength)
    {
      String cmd = getNextCommand();
      print("next command: ");
      println(cmd);
      
      print("queue length:  ");
      println(commandsInQueue);

      if (gcodeIndex == gcode.length)
      {
        finished = true;
      }
      
      if (cmd != null)
      {
        commandsInQueue++;
        println("Sent: " +cmd);
        myPort.write(cmd);
        myPort.write("\n");
      }
    }
  }
  
  if (started && finished)
  {
    println("Job's done!");
    started = false;
    finished = false;
  }

  
  textFont(font, 16);
  String temp = "Temperature: " + temperature + "C";
  text(temp, 10, 20);
}

String getNextCommand()
{
  if (gcodeIndex < gcode.length)
  {

    String c = gcode[gcodeIndex];
    gcodeIndex++;

    /*
  if (match(c, "^...") != null)
     {
     String comment = "Comment: " + c;
     println(comment); 
     }
     */

    if (match(c, "^[A-Z]{1}") == null)
      c = getNextCommand();

    return c;
  }
  else
    return null;
}