Search in sources :

Example 1 with EnabledPins

use of com.willwinder.universalgcodesender.listeners.ControllerStatus.EnabledPins in project Universal-G-Code-Sender by winder.

the class GrblUtils method getStatusFromStatusString.

/**
 * Parses a GRBL status string in the legacy format or v1.x format:
 * legacy: <status,WPos:1,2,3,MPos:1,2,3>
 * 1.x: <status|WPos:1,2,3|Bf:0,0|WCO:0,0,0>
 * @param lastStatus required for the 1.x version which requires WCO coords
 *                   and override status from previous status updates.
 * @param status the raw status string
 * @param version capabilities flags
 * @param reportingUnits units
 * @return
 */
protected static ControllerStatus getStatusFromStatusString(ControllerStatus lastStatus, final String status, final Capabilities version, Units reportingUnits) {
    // Legacy status.
    if (!version.hasCapability(GrblCapabilitiesConstants.V1_FORMAT)) {
        return new ControllerStatus(getStateFromStatusString(status, version), getMachinePositionFromStatusString(status, version, reportingUnits), getWorkPositionFromStatusString(status, version, reportingUnits));
    } else {
        String state = "";
        Position MPos = null;
        Position WPos = null;
        Position WCO = null;
        OverridePercents overrides = null;
        EnabledPins pins = null;
        AccessoryStates accessoryStates = null;
        Double feedSpeed = null;
        Double spindleSpeed = null;
        boolean isOverrideReport = false;
        // Parse out the status messages.
        for (String part : status.substring(0, status.length() - 1).split("\\|")) {
            if (part.startsWith("<")) {
                int idx = part.indexOf(':');
                if (idx == -1)
                    state = part.substring(1);
                else
                    state = part.substring(1, idx);
            } else if (part.startsWith("MPos:")) {
                MPos = GrblUtils.getPositionFromStatusString(status, machinePattern, reportingUnits);
            } else if (part.startsWith("WPos:")) {
                WPos = GrblUtils.getPositionFromStatusString(status, workPattern, reportingUnits);
            } else if (part.startsWith("WCO:")) {
                WCO = GrblUtils.getPositionFromStatusString(status, wcoPattern, reportingUnits);
            } else if (part.startsWith("Ov:")) {
                isOverrideReport = true;
                String[] overrideParts = part.substring(3).trim().split(",");
                if (overrideParts.length == 3) {
                    overrides = new OverridePercents(Integer.parseInt(overrideParts[0]), Integer.parseInt(overrideParts[1]), Integer.parseInt(overrideParts[2]));
                }
            } else if (part.startsWith("F:")) {
                feedSpeed = Double.parseDouble(part.substring(2));
            } else if (part.startsWith("FS:")) {
                String[] parts = part.substring(3).split(",");
                feedSpeed = Double.parseDouble(parts[0]);
                spindleSpeed = Double.parseDouble(parts[1]);
            } else if (part.startsWith("Pn:")) {
                String value = part.substring(part.indexOf(':') + 1);
                pins = new EnabledPins(value);
            } else if (part.startsWith("A:")) {
                String value = part.substring(part.indexOf(':') + 1);
                accessoryStates = new AccessoryStates(value);
            }
        }
        // Grab WCO from state information if necessary.
        if (WCO == null) {
            // Grab the work coordinate offset.
            if (lastStatus != null && lastStatus.getWorkCoordinateOffset() != null) {
                WCO = lastStatus.getWorkCoordinateOffset();
            } else {
                WCO = new Position(0, 0, 0, reportingUnits);
            }
        }
        // Calculate missing coordinate with WCO
        if (WPos == null) {
            WPos = new Position(MPos.x - WCO.x, MPos.y - WCO.y, MPos.z - WCO.z, reportingUnits);
        }
        if (MPos == null) {
            MPos = new Position(WPos.x + WCO.x, WPos.y + WCO.y, WPos.z + WCO.z, reportingUnits);
        }
        if (!isOverrideReport && lastStatus != null) {
            overrides = lastStatus.getOverrides();
            pins = lastStatus.getEnabledPins();
            accessoryStates = lastStatus.getAccessoryStates();
        } else if (isOverrideReport) {
            // set all pins to a disabled state.
            if (pins == null) {
                pins = new EnabledPins("");
            }
            // Likewise for accessory states.
            if (accessoryStates == null) {
                accessoryStates = new AccessoryStates("");
            }
        }
        return new ControllerStatus(state, MPos, WPos, feedSpeed, spindleSpeed, overrides, WCO, pins, accessoryStates);
    }
}
Also used : ControllerStatus(com.willwinder.universalgcodesender.listeners.ControllerStatus) Position(com.willwinder.universalgcodesender.model.Position) AccessoryStates(com.willwinder.universalgcodesender.listeners.ControllerStatus.AccessoryStates) EnabledPins(com.willwinder.universalgcodesender.listeners.ControllerStatus.EnabledPins) OverridePercents(com.willwinder.universalgcodesender.listeners.ControllerStatus.OverridePercents)

Example 2 with EnabledPins

use of com.willwinder.universalgcodesender.listeners.ControllerStatus.EnabledPins in project Universal-G-Code-Sender by winder.

the class MachineStatusPanel method onControllerStatusReceived.

private void onControllerStatusReceived(ControllerStatus status) {
    this.updateStatePanel(status.getState());
    resetStatePinComponents();
    if (status.getEnabledPins() != null) {
        EnabledPins ep = status.getEnabledPins();
        List<String> enabled = new ArrayList<>();
        if (ep.X)
            enabled.add(PIN_X);
        if (ep.Y)
            enabled.add(PIN_Y);
        if (ep.Z)
            enabled.add(PIN_Z);
        if (ep.Probe)
            enabled.add(PIN_PROBE);
        if (ep.Door)
            enabled.add(PIN_DOOR);
        if (ep.Hold)
            enabled.add(PIN_HOLD);
        if (ep.SoftReset)
            enabled.add(PIN_SOFT_RESET);
        if (ep.CycleStart)
            enabled.add(PIN_CYCLE_STARY);
        if (!enabled.isEmpty()) {
            enabled.add(0, ALARM + ":");
            pinStatesLabel.setText(String.join(" ", enabled));
            pinStatesLabel.setForeground(ThemeColors.RED);
            pinStatePanel.setForeground(ThemeColors.RED);
        }
    }
    if (status.getMachineCoord() != null) {
        this.setUnits(status.getMachineCoord().getUnits());
        this.setPositionValueColor(this.machinePositionXValue, status.getMachineCoord().x);
        this.machinePositionXValue.setText(decimalFormatter.format(status.getMachineCoord().x));
        this.setPositionValueColor(this.machinePositionYValue, status.getMachineCoord().y);
        this.machinePositionYValue.setText(decimalFormatter.format(status.getMachineCoord().y));
        this.setPositionValueColor(this.machinePositionZValue, status.getMachineCoord().z);
        this.machinePositionZValue.setText(decimalFormatter.format(status.getMachineCoord().z));
    }
    if (status.getWorkCoord() != null) {
        this.setUnits(status.getWorkCoord().getUnits());
        this.setPositionValueColor(this.workPositionXValue, status.getWorkCoord().x);
        this.workPositionXValue.setText(decimalFormatter.format(status.getWorkCoord().x));
        this.setPositionValueColor(this.workPositionYValue, status.getWorkCoord().y);
        this.workPositionYValue.setText(decimalFormatter.format(status.getWorkCoord().y));
        this.setPositionValueColor(this.workPositionZValue, status.getWorkCoord().z);
        this.workPositionZValue.setText(decimalFormatter.format(status.getWorkCoord().z));
    }
    // Use real-time values if available, otherwise show the target values.
    int feedSpeed = status.getFeedSpeed() != null ? status.getFeedSpeed().intValue() : (int) this.backend.getGcodeState().speed;
    this.feedValue.setText(Integer.toString(feedSpeed));
    int spindleSpeed = status.getSpindleSpeed() != null ? status.getSpindleSpeed().intValue() : (int) this.backend.getGcodeState().spindleSpeed;
    this.spindleSpeedValue.setText(Integer.toString(spindleSpeed));
}
Also used : ArrayList(java.util.ArrayList) EnabledPins(com.willwinder.universalgcodesender.listeners.ControllerStatus.EnabledPins)

Aggregations

EnabledPins (com.willwinder.universalgcodesender.listeners.ControllerStatus.EnabledPins)2 ControllerStatus (com.willwinder.universalgcodesender.listeners.ControllerStatus)1 AccessoryStates (com.willwinder.universalgcodesender.listeners.ControllerStatus.AccessoryStates)1 OverridePercents (com.willwinder.universalgcodesender.listeners.ControllerStatus.OverridePercents)1 Position (com.willwinder.universalgcodesender.model.Position)1 ArrayList (java.util.ArrayList)1