This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCommandClass.pde
162 lines (130 loc) · 4.14 KB
/
GCommandClass.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
By Chris Eugene Mills
*/
static final int[] supportedGCommands = new int[]{0, 1, 20, 21, 28, 90, 91};
static final int[] supportedMCommands = new int[]{2, 30};
class GCommand {
// VARIABLES /////////////////////////////////////////////////////////////////
public char set = '?';
public int command = -1;
public int lineNumber = -1;
public int feedrate = -1;
public int duration = -1;
public float x = Float.NaN;
public float y = Float.NaN;
public float z = Float.NaN;
public boolean supported = false;
// CONSTRUCTOR ///////////////////////////////////////////////////////////////
GCommand( char _set, int _command ) {
set = Character.toUpperCase( _set );
command = _command;
supported = checkValidity( _set, _command );
}
GCommand( int line, char _set, int _command ) {
lineNumber = line;
set = Character.toUpperCase( _set );
command = _command;
supported = checkValidity( _set, _command );
}
GCommand( char _set, int _command, float _x, float _y) {
x = _x;
y = _y;
set = Character.toUpperCase( _set );
command = _command;
supported = checkValidity( _set, _command );
}
GCommand( int line, char _set, int _command, float _x, float _y) {
lineNumber = line;
x = _x;
y = _y;
set = Character.toUpperCase( _set );
command = _command;
supported = checkValidity( _set, _command );
}
GCommand( char _set, int _command, float _x, float _y, float _z) {
x = _x;
y = _y;
z = _z;
set = Character.toUpperCase( _set );
command = _command;
supported = checkValidity( _set, _command );
}
GCommand( int line, char _set, int _command, float _x, float _y, float _z) {
lineNumber = line;
x = _x;
y = _y;
z = _z;
set = Character.toUpperCase( _set );
command = _command;
supported = checkValidity( _set, _command );
}
GCommand( char _set, int _command, float _x, float _y, float _z, int _feedrate) {
feedrate = _feedrate;
x = _x;
y = _y;
z = _z;
set = Character.toUpperCase( _set );
command = _command;
supported = checkValidity( _set, _command );
}
GCommand( int line, char _set, int _command, float _x, float _y, float _z, int _feedrate) {
lineNumber = line;
feedrate = _feedrate;
x = _x;
y = _y;
z = _z;
set = Character.toUpperCase( _set );
command = _command;
supported = checkValidity( _set, _command );
}
// ///////////////////////////////////////////////////////////////////////////
private boolean checkValidity( char _set, int _command ) {
//TODO LOOSE!! Only detects command code, not validity of data
switch ( _set ) {
case 'G':
return matchIntArray(supportedGCommands, _command);
case 'M':
return matchIntArray(supportedMCommands, _command);
case 'T':
return false;
default:
return false;
}
}
private boolean matchIntArray(int[] arr, int targetValue) {
for(int s: arr){
if(s == targetValue) return true;
}
return false;
}
public JSONObject getJSON() {
JSONObject json = new JSONObject();
if (lineNumber != -1) json.setInt("lineNumber", lineNumber);
json.setString("commandSet", str(set));
json.setInt("commandCode", command);
json.setBoolean("supported", supported);
JSONObject data = new JSONObject();
if (!Float.isNaN(x)) data.setFloat("x", x);
if (!Float.isNaN(y)) data.setFloat("y", y);
if (!Float.isNaN(z)) data.setFloat("z", z);
if (feedrate != -1) data.setFloat("feedrate", feedrate);
if (duration != -1) data.setFloat("duration", duration);
json.setJSONObject("data", data);
return json;
}
public String getGText() {
String line = "";
if(supported) {
line += str(set);
line += str(command) + " ";
if (!Float.isNaN(x)) line += "X" + str(x) + " ";
if (!Float.isNaN(y)) line += "Y" + str(y) + " ";
if (!Float.isNaN(z)) line += "Z" + str(z) + " ";
if (feedrate != -1) line += "F" + str(feedrate) + " ";
if (duration != -1) line += "P" + str(duration) + " ";
} else {
line += "( unsupported command: "+str(set)+str(command)+" )";
}
return line;
}
}