コード例 #1
0
 public void ParseHomedStatus(string data)
 {
     string[] values = data.TrimEnd(']').Split(':');
     if (values.Length == 3)
     {
         HomePosition.Parse(values[1]);
         AxisHomed.Value = (AxisFlags)int.Parse(values[2]);
         for (int i = 0; i < GrblInfo.NumAxes; i++)
         {
             if (!AxisHomed.Value.HasFlag(GrblInfo.AxisIndexToFlag(i)))
             {
                 HomePosition.Values[i] = double.NaN;
             }
         }
     }
 }
コード例 #2
0
        public bool ProcessKeypress(KeyEventArgs e, bool allowJog, UserControl context = null)
        {
            bool isJogging = IsJogging, jogkeyPressed = false;

            double[] dist = new double[4] {
                0d, 0d, 0d, 0d
            };

            if (e.IsUp && isJogging)
            {
                bool cancel = !allowJog;

                isJogging = false;

                for (int i = 0; i < N_AXIS; i++)
                {
                    if (axisjog[i] == e.Key)
                    {
                        axisjog[i] = Key.None;
                        cancel     = true;
                    }
                    else
                    {
                        isJogging = isJogging || (axisjog[i] != Key.None);
                    }
                }

                isJogging &= allowJog;

                if (cancel && !isJogging && jogMode != JogMode.Step)
                {
                    JogCancel();
                }
            }

            if (!isJogging && allowJog && Comms.com.OutCount != 0)
            {
                return(true);
            }

            this.allowJog = allowJog;

            if (IsJoggingEnabled && e.IsDown && CanJog)
            {
                // Do not respond to autorepeats!
                if (e.IsRepeat)
                {
                    return(true);
                }

                N_AXIS = GrblInfo.AxisFlags.HasFlag(AxisFlags.A) ? 4 : 3;

                switch (e.Key)
                {
                case Key.PageUp:
                    isJogging = axisjog[GrblConstants.Z_AXIS] != Key.PageUp;
                    axisjog[GrblConstants.Z_AXIS] = Key.PageUp;
                    break;

                case Key.PageDown:
                    isJogging = axisjog[GrblConstants.Z_AXIS] != Key.PageDown;
                    axisjog[GrblConstants.Z_AXIS] = Key.PageDown;
                    break;

                case Key.Left:
                    isJogging = axisjog[GrblConstants.X_AXIS] != Key.Left;
                    axisjog[GrblConstants.X_AXIS] = Key.Left;
                    break;

                case Key.Up:
                    isJogging = axisjog[GrblConstants.Y_AXIS] != Key.Up;
                    axisjog[GrblConstants.Y_AXIS] = Key.Up;
                    break;

                case Key.Right:
                    isJogging = axisjog[GrblConstants.X_AXIS] != Key.Right;
                    axisjog[GrblConstants.X_AXIS] = Key.Right;
                    break;

                case Key.Down:
                    isJogging = axisjog[GrblConstants.Y_AXIS] != Key.Down;
                    axisjog[GrblConstants.Y_AXIS] = Key.Down;
                    break;

                case Key.Home:
                    if (N_AXIS == 4)
                    {
                        isJogging = axisjog[GrblConstants.A_AXIS] != Key.Home;
                        axisjog[GrblConstants.A_AXIS] = Key.Home;
                    }
                    break;

                case Key.End:
                    if (N_AXIS == 4)
                    {
                        isJogging = axisjog[GrblConstants.A_AXIS] != Key.End;
                        axisjog[GrblConstants.A_AXIS] = Key.End;
                    }
                    break;
                }
            }
            else
            {
                jogkeyPressed = !(Keyboard.FocusedElement is System.Windows.Controls.TextBox) && (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.PageUp || e.Key == Key.PageDown);
            }

            if (isJogging)
            {
                string command = string.Empty;

                if (GrblInfo.LatheModeEnabled)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.Left:
                            dist[GrblConstants.Z_AXIS] = -1d;
                            command += "Z-{3}";
                            break;

                        case Key.Up:
                            dist[GrblConstants.X_AXIS] = -1d;
                            command += "X-{1}";
                            break;

                        case Key.Right:
                            dist[GrblConstants.Z_AXIS] = 1d;
                            command += "Z{3}";
                            break;

                        case Key.Down:
                            dist[GrblConstants.X_AXIS] = 1d;
                            command += "X{1}";
                            break;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < N_AXIS; i++)
                    {
                        switch (axisjog[i])
                        {
                        case Key.PageUp:
                            dist[GrblConstants.Z_AXIS] = 1d;
                            command += "Z{3}";
                            break;

                        case Key.PageDown:
                            dist[GrblConstants.Z_AXIS] = -1d;
                            command += "Z-{3}";
                            break;

                        case Key.Left:
                            dist[GrblConstants.X_AXIS] = -1d;
                            command += "X-{1}";
                            break;

                        case Key.Up:
                            dist[GrblConstants.Y_AXIS] = 1d;
                            command += "Y{2}";
                            break;

                        case Key.Right:
                            dist[GrblConstants.X_AXIS] = 1d;
                            command += "X{1}";
                            break;

                        case Key.Down:
                            dist[GrblConstants.Y_AXIS] = -1d;
                            command += "Y-{2}";
                            break;

                        case Key.Home:
                            dist[GrblConstants.A_AXIS] = 1d;
                            command += "A{4}";
                            break;

                        case Key.End:
                            dist[GrblConstants.A_AXIS] = -1d;
                            command += "A-{4}";
                            break;
                        }
                    }
                }

                if ((isJogging = command != string.Empty))
                {
                    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        for (int i = 0; i < N_AXIS; i++)
                        {
                            axisjog[i] = Key.None;
                        }
                        preCancel = !(jogMode == JogMode.Step || jogMode == JogMode.None);
                        jogMode   = JogMode.Step;
                        JogDistances[(int)jogMode] = grbl.JogStep;
                    }
                    else if (IsContinuousJoggingEnabled)
                    {
                        preCancel = true;
                        if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                        {
                            jogMode = JogMode.Fast;
                        }
                        else
                        {
                            jogMode = JogMode.Slow;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < N_AXIS; i++)
                        {
                            axisjog[i] = Key.None;
                        }
                        jogMode = JogMode.None;
                    }

                    if (jogMode != JogMode.None)
                    {
                        if (GrblInfo.IsGrblHAL || !SoftLimits)
                        {
                            var distance = JogDistances[(int)jogMode].ToInvariantString();
                            SendJogCommand("$J=G91G21" + string.Format(command + "F{0}",
                                                                       JogFeedrates[(int)jogMode].ToInvariantString(),
                                                                       distance, distance, distance, distance));
                        }
                        else
                        {
                            for (int i = 0; i < N_AXIS; i++)
                            {
                                if (dist[i] != 0d)
                                {
                                    dist[i] = grbl.MachinePosition.Values[i] + JogDistances[(int)jogMode] * dist[i];

                                    if (i == GrblConstants.A_AXIS && GrblInfo.MaxTravel.Values[GrblConstants.A_AXIS] == 0d)
                                    {
                                        continue;
                                    }

                                    if (GrblInfo.ForceSetOrigin)
                                    {
                                        if (!GrblInfo.HomingDirection.HasFlag(GrblInfo.AxisIndexToFlag(i)))
                                        {
                                            if (dist[i] > 0)
                                            {
                                                dist[i] = 0;
                                            }
                                            else if (dist[i] < (-GrblInfo.MaxTravel.Values[i] + LimitSwitchesClearance))
                                            {
                                                dist[i] = (-GrblInfo.MaxTravel.Values[i] + LimitSwitchesClearance);
                                            }
                                        }
                                        else
                                        {
                                            if (dist[i] < 0d)
                                            {
                                                dist[i] = 0d;
                                            }
                                            else if (dist[i] > (GrblInfo.MaxTravel.Values[i] - LimitSwitchesClearance))
                                            {
                                                dist[i] = GrblInfo.MaxTravel.Values[i] - LimitSwitchesClearance;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (dist[i] > -LimitSwitchesClearance)
                                        {
                                            dist[i] = -LimitSwitchesClearance;
                                        }
                                        else if (dist[i] < -(GrblInfo.MaxTravel.Values[i] - LimitSwitchesClearance))
                                        {
                                            dist[i] = -(GrblInfo.MaxTravel.Values[i] - LimitSwitchesClearance);
                                        }
                                    }
                                }
                            }

                            SendJogCommand("$J=G53G21" + string.Format(command.Replace('-', ' ') + "F{0}",
                                                                       JogFeedrates[(int)jogMode].ToInvariantString(),
                                                                       dist[GrblConstants.X_AXIS].ToInvariantString(),
                                                                       dist[GrblConstants.Y_AXIS].ToInvariantString(),
                                                                       dist[GrblConstants.Z_AXIS].ToInvariantString(),
                                                                       dist[GrblConstants.A_AXIS].ToInvariantString()));
                        }
                    }

                    return(jogMode != JogMode.None);
                }
            }

            IsRepeating = e.IsRepeat;

            if (Keyboard.Modifiers == ModifierKeys.Alt)
            {
                var handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.SystemKey && k.onUp == e.IsUp && k.context == context).FirstOrDefault();
                if (handler != null)
                {
                    return(handler.Call(e.SystemKey));
                }
                else
                {
                    handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.SystemKey && k.onUp == e.IsUp && k.context == null).FirstOrDefault();
                    if (handler != null)
                    {
                        return(handler.Call(e.SystemKey));
                    }
                }
            }
            else if (Keyboard.Modifiers == ModifierKeys.None || Keyboard.Modifiers == ModifierKeys.Control || Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
            {
                var handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.Key && k.onUp == e.IsUp && k.context == context).FirstOrDefault();
                if (handler != null)
                {
                    return(handler.Call(e.Key));
                }
                else
                {
                    handler = handlers.Where(k => k.modifiers == Keyboard.Modifiers && k.key == e.Key && k.onUp == e.IsUp && k.context == null).FirstOrDefault();
                    if (handler != null)
                    {
                        return(handler.Call(e.Key));
                    }
                }
            }

            return(jogkeyPressed);
        }