Skip to content

Commit e9a50e2

Browse files
committed
Remove the code enum (#229)
1 parent b7fffdc commit e9a50e2

File tree

3 files changed

+45
-101
lines changed

3 files changed

+45
-101
lines changed

src/__tests__/interpreter.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ test('.G0 continues the path if the job has one', () => {
112112
job.state.z = 5;
113113
interpreter.execute([command1], job);
114114

115-
interpreter.G0(command2, job);
115+
interpreter.g0(command2, job);
116116

117117
expect(job.paths.length).toEqual(0);
118118
expect(job.inprogressPath?.vertices.length).toEqual(9);
@@ -126,7 +126,7 @@ test(".G0 assigns the travel type if there's no extrusion", () => {
126126
const interpreter = new Interpreter();
127127
const job = new Job();
128128

129-
interpreter.G0(command, job);
129+
interpreter.g0(command, job);
130130

131131
expect(job.paths.length).toEqual(0);
132132
expect(job.inprogressPath?.travelType).toEqual(PathType.Travel);
@@ -137,7 +137,7 @@ test(".G0 assigns the extrusion type if there's extrusion", () => {
137137
const interpreter = new Interpreter();
138138
const job = new Job();
139139

140-
interpreter.G0(command, job);
140+
interpreter.g0(command, job);
141141

142142
expect(job.paths.length).toEqual(0);
143143
expect(job.inprogressPath?.travelType).toEqual('Extrusion');
@@ -148,7 +148,7 @@ test('.G0 assigns the travel type if the extrusion is a retraction', () => {
148148
const interpreter = new Interpreter();
149149
const job = new Job();
150150

151-
interpreter.G0(command, job);
151+
interpreter.g0(command, job);
152152

153153
expect(job.paths.length).toEqual(0);
154154
expect(job.inprogressPath?.travelType).toEqual('Travel');
@@ -159,7 +159,7 @@ test('.G0 assigns the travel type if the extrusion is a retraction', () => {
159159
const interpreter = new Interpreter();
160160
const job = new Job();
161161

162-
interpreter.G0(command, job);
162+
interpreter.g0(command, job);
163163

164164
expect(job.paths.length).toEqual(0);
165165
expect(job.inprogressPath?.travelType).toEqual('Travel');
@@ -172,7 +172,7 @@ test('.G0 starts a new path if the travel type changes from Travel to Extrusion'
172172
const job = new Job();
173173
interpreter.execute([command1], job);
174174

175-
interpreter.G0(command2, job);
175+
interpreter.g0(command2, job);
176176

177177
expect(job.paths.length).toEqual(1);
178178
expect(job.inprogressPath?.travelType).toEqual('Extrusion');
@@ -185,7 +185,7 @@ test('.G0 starts a new path if the travel type changes from Extrusion to Travel'
185185
const job = new Job();
186186
interpreter.execute([command1], job);
187187

188-
interpreter.G0(command2, job);
188+
interpreter.g0(command2, job);
189189

190190
expect(job.paths.length).toEqual(1);
191191
expect(job.inprogressPath?.travelType).toEqual('Travel');
@@ -194,15 +194,15 @@ test('.G0 starts a new path if the travel type changes from Extrusion to Travel'
194194
test('.G1 is an alias to .G0', () => {
195195
const interpreter = new Interpreter();
196196

197-
expect(interpreter.G1).toEqual(interpreter.G0);
197+
expect(interpreter.g1).toEqual(interpreter.g0);
198198
});
199199

200200
test('.G20 sets the units to inches', () => {
201201
const command = new GCodeCommand('G20', 'g20', {});
202202
const interpreter = new Interpreter();
203203
const job = new Job();
204204

205-
interpreter.G20(command, job);
205+
interpreter.g20(command, job);
206206

207207
expect(job.state.units).toEqual('in');
208208
});
@@ -212,7 +212,7 @@ test('.G21 sets the units to millimeters', () => {
212212
const interpreter = new Interpreter();
213213
const job = new Job();
214214

215-
interpreter.G21(command, job);
215+
interpreter.g21(command, job);
216216

217217
expect(job.state.units).toEqual('mm');
218218
});
@@ -224,7 +224,7 @@ test('.g28 moves the state to the origin', () => {
224224
job.state.x = 3;
225225
job.state.y = 4;
226226

227-
interpreter.G28(command, job);
227+
interpreter.g28(command, job);
228228

229229
expect(job.state.x).toEqual(0);
230230
expect(job.state.y).toEqual(0);
@@ -237,7 +237,7 @@ test('.t0 sets the tool to 0', () => {
237237
const job = new Job();
238238
job.state.tool = 3;
239239

240-
interpreter.T0(command, job);
240+
interpreter.t0(command, job);
241241

242242
expect(job.state.tool).toEqual(0);
243243
});
@@ -248,7 +248,7 @@ test('.t1 sets the tool to 1', () => {
248248
const job = new Job();
249249
job.state.tool = 3;
250250

251-
interpreter.T1(command, job);
251+
interpreter.t1(command, job);
252252

253253
expect(job.state.tool).toEqual(1);
254254
});
@@ -259,7 +259,7 @@ test('.t2 sets the tool to 2', () => {
259259
const job = new Job();
260260
job.state.tool = 3;
261261

262-
interpreter.T2(command, job);
262+
interpreter.t2(command, job);
263263

264264
expect(job.state.tool).toEqual(2);
265265
});
@@ -270,7 +270,7 @@ test('.t3 sets the tool to 3', () => {
270270
const job = new Job();
271271
job.state.tool = 3;
272272

273-
interpreter.T3(command, job);
273+
interpreter.t3(command, job);
274274

275275
expect(job.state.tool).toEqual(3);
276276
});
@@ -281,7 +281,7 @@ test('.t4 sets the tool to 4', () => {
281281
const job = new Job();
282282
job.state.tool = 3;
283283

284-
interpreter.T4(command, job);
284+
interpreter.t4(command, job);
285285

286286
expect(job.state.tool).toEqual(4);
287287
});
@@ -292,7 +292,7 @@ test('.t5 sets the tool to 5', () => {
292292
const job = new Job();
293293
job.state.tool = 3;
294294

295-
interpreter.T5(command, job);
295+
interpreter.t5(command, job);
296296

297297
expect(job.state.tool).toEqual(5);
298298
});
@@ -303,7 +303,7 @@ test('.t6 sets the tool to 6', () => {
303303
const job = new Job();
304304
job.state.tool = 3;
305305

306-
interpreter.T6(command, job);
306+
interpreter.t6(command, job);
307307

308308
expect(job.state.tool).toEqual(6);
309309
});
@@ -314,7 +314,7 @@ test('.t7 sets the tool to 7', () => {
314314
const job = new Job();
315315
job.state.tool = 3;
316316

317-
interpreter.T7(command, job);
317+
interpreter.t7(command, job);
318318

319319
expect(job.state.tool).toEqual(7);
320320
});

src/gcode-parser.ts

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -56,74 +56,13 @@ type singleLetter =
5656
| 'Z';
5757
type CommandParams = { [key in singleLetter]?: number };
5858

59-
export enum Code {
60-
G0 = 'G0',
61-
G1 = 'G1',
62-
G2 = 'G2',
63-
G3 = 'G3',
64-
G20 = 'G20',
65-
G21 = 'G21',
66-
G28 = 'G28',
67-
T0 = 'T0',
68-
T1 = 'T1',
69-
T2 = 'T2',
70-
T3 = 'T3',
71-
T4 = 'T4',
72-
T5 = 'T5',
73-
T6 = 'T6',
74-
T7 = 'T7'
75-
}
7659
export class GCodeCommand {
77-
public code?: Code;
7860
constructor(
7961
public src: string,
8062
public gcode: string,
8163
public params: CommandParams,
8264
public comment?: string
83-
) {
84-
this.code = this.match(gcode);
85-
}
86-
87-
match(gcode: string): Code {
88-
switch (gcode) {
89-
case 'g0':
90-
case 'g00':
91-
return Code.G0;
92-
case 'g1':
93-
case 'g01':
94-
return Code.G1;
95-
case 'g2':
96-
case 'g02':
97-
return Code.G2;
98-
case 'g3':
99-
case 'g03':
100-
return Code.G3;
101-
case 'g20':
102-
return Code.G20;
103-
case 'g21':
104-
return Code.G21;
105-
case 'g28':
106-
return Code.G28;
107-
case 't0':
108-
return Code.T0;
109-
case 't1':
110-
return Code.T1;
111-
case 't2':
112-
return Code.T2;
113-
case 't3':
114-
return Code.T3;
115-
case 't4':
116-
return Code.T4;
117-
case 't5':
118-
return Code.T5;
119-
case 't6':
120-
return Code.T6;
121-
case 't7':
122-
return Code.T7;
123-
default:
124-
return undefined;
125-
}
126-
}
65+
) {}
12766
}
12867

12968
type Metadata = { thumbnails: Record<string, Thumbnail> };
@@ -163,7 +102,7 @@ export class Parser {
163102
.slice(1)
164103
.map((s) => s.trim());
165104

166-
const gcode = !parts.length ? '' : `${parts[0]?.toLowerCase()}${parts[1]}`;
105+
const gcode = !parts.length ? '' : `${parts[0]?.toLowerCase()}${Number(parts[1])}`;
167106
const params = this.parseParams(parts.slice(2));
168107
return new GCodeCommand(line, gcode, params, comment);
169108
}

src/interpreter.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { Path, PathType } from './path';
2-
import { Code, GCodeCommand } from './gcode-parser';
2+
import { GCodeCommand } from './gcode-parser';
33
import { Job } from './job';
44

55
export class Interpreter {
6+
// eslint-disable-next-line no-unused-vars
7+
[key: string]: (...args: unknown[]) => unknown;
68
execute(commands: GCodeCommand[], job = new Job()): Job {
79
commands.forEach((command) => {
8-
if (command.code !== undefined) {
9-
this[command.code](command, job);
10+
if (command.gcode !== undefined) {
11+
if (this[command.gcode] === undefined) {
12+
return;
13+
}
14+
this[command.gcode](command, job);
1015
}
1116
});
1217

1318
return job;
1419
}
1520

16-
G0(command: GCodeCommand, job: Job): void {
21+
g0(command: GCodeCommand, job: Job): void {
1722
const { x, y, z, e } = command.params;
1823
const { state } = job;
1924

@@ -31,14 +36,14 @@ export class Interpreter {
3136
currentPath.addPoint(state.x, state.y, state.z);
3237
}
3338

34-
G1 = this.G0;
39+
g1 = this.g0;
3540

36-
G2(command: GCodeCommand, job: Job): void {
41+
g2(command: GCodeCommand, job: Job): void {
3742
const { x, y, z, e } = command.params;
3843
let { i, j, r } = command.params;
3944
const { state } = job;
4045

41-
const cw = command.code === Code.G2;
46+
const cw = command.gcode === 'g2';
4247
let currentPath = job.inprogressPath;
4348
const pathType = e ? PathType.Extrusion : PathType.Travel;
4449

@@ -128,44 +133,44 @@ export class Interpreter {
128133
currentPath.addPoint(state.x, state.y, state.z);
129134
}
130135

131-
G3 = this.G2;
136+
g3 = this.g2;
132137

133-
G20(command: GCodeCommand, job: Job): void {
138+
g20(command: GCodeCommand, job: Job): void {
134139
job.state.units = 'in';
135140
}
136141

137-
G21(command: GCodeCommand, job: Job): void {
142+
g21(command: GCodeCommand, job: Job): void {
138143
job.state.units = 'mm';
139144
}
140145

141-
G28(command: GCodeCommand, job: Job): void {
146+
g28(command: GCodeCommand, job: Job): void {
142147
job.state.x = 0;
143148
job.state.y = 0;
144149
job.state.z = 0;
145150
}
146151

147-
T0(command: GCodeCommand, job: Job): void {
152+
t0(command: GCodeCommand, job: Job): void {
148153
job.state.tool = 0;
149154
}
150-
T1(command: GCodeCommand, job: Job): void {
155+
t1(command: GCodeCommand, job: Job): void {
151156
job.state.tool = 1;
152157
}
153-
T2(command: GCodeCommand, job: Job): void {
158+
t2(command: GCodeCommand, job: Job): void {
154159
job.state.tool = 2;
155160
}
156-
T3(command: GCodeCommand, job: Job): void {
161+
t3(command: GCodeCommand, job: Job): void {
157162
job.state.tool = 3;
158163
}
159-
T4(command: GCodeCommand, job: Job): void {
164+
t4(command: GCodeCommand, job: Job): void {
160165
job.state.tool = 4;
161166
}
162-
T5(command: GCodeCommand, job: Job): void {
167+
t5(command: GCodeCommand, job: Job): void {
163168
job.state.tool = 5;
164169
}
165-
T6(command: GCodeCommand, job: Job): void {
170+
t6(command: GCodeCommand, job: Job): void {
166171
job.state.tool = 6;
167172
}
168-
T7(command: GCodeCommand, job: Job): void {
173+
t7(command: GCodeCommand, job: Job): void {
169174
job.state.tool = 7;
170175
}
171176

0 commit comments

Comments
 (0)