use of com.willwinder.universalgcodesender.gcode.util.Code.ModalGroup.Motion in project Universal-G-Code-Sender by winder.
the class GcodeParser method processCommand.
/**
* Process commend given an initial state. This method will not modify its
* input parameters.
*
* @param includeNonMotionStates Create gcode meta responses even if there is no motion, for example "F100" will not
* return a GcodeMeta entry unless this flag is set to true.
*/
public static List<GcodeMeta> processCommand(String command, int line, final GcodeState inputState, boolean includeNonMotionStates) throws GcodeParserException {
List<String> args = GcodePreprocessorUtils.splitCommand(command);
if (args.isEmpty())
return null;
// Initialize with original state
GcodeState state = inputState.copy();
state.commandNumber = line;
// handle M codes.
// codes = GcodePreprocessorUtils.parseCodes(args, 'M');
// handleMCode(for each codes);
List<String> fCodes = GcodePreprocessorUtils.parseCodes(args, 'F');
if (!fCodes.isEmpty()) {
try {
state.speed = Double.parseDouble(Iterables.getOnlyElement(fCodes));
} catch (IllegalArgumentException e) {
throw new GcodeParserException("Multiple F-codes on one line.");
}
}
List<String> sCodes = GcodePreprocessorUtils.parseCodes(args, 'S');
if (!sCodes.isEmpty()) {
try {
state.spindleSpeed = Double.parseDouble(Iterables.getOnlyElement(sCodes));
} catch (IllegalArgumentException e) {
throw new GcodeParserException("Multiple S-codes on one line.");
}
}
// Gather G codes.
Set<Code> gCodes = GcodePreprocessorUtils.getGCodes(args);
boolean hasAxisWords = GcodePreprocessorUtils.hasAxisWords(args);
// Error to mix group 1 (Motion) and certain group 0 (NonModal) codes (G10, G28, G30, G92)
Collection<Code> motionCodes = gCodes.stream().filter(c -> c.consumesMotion()).collect(Collectors.toList());
// 1 motion code per line.
if (motionCodes.size() > 1) {
throw new GcodeParserException(Localization.getString("parser.gcode.multiple-axis-commands") + ": " + StringUtils.join(motionCodes, ", "));
}
// If there are axis words and nothing to use them, add the currentMotionMode.
if (hasAxisWords && motionCodes.isEmpty() && state.currentMotionMode != null) {
gCodes.add(state.currentMotionMode);
}
// Apply each code to the state.
List<GcodeMeta> results = new ArrayList<>();
for (Code i : gCodes) {
if (i == UNKNOWN) {
logger.warning("An unknown gcode command was detected in: " + command);
} else {
GcodeMeta meta = handleGCode(i, args, line, state, hasAxisWords);
meta.command = command;
// Commands like 'G21' don't return a point segment.
if (meta.point != null) {
meta.point.setSpeed(state.speed);
}
results.add(meta);
}
}
// Return updated state / command.
if (results.isEmpty() && includeNonMotionStates) {
GcodeMeta meta = new GcodeMeta();
meta.state = state;
meta.command = command;
meta.code = state.currentMotionMode;
return Collections.singletonList(meta);
}
return results;
}
Aggregations