private void SwitchKeyEvent(bool KeyDown, KeyEventArgs e)
        {
            if (client == null)
            {
                return;
            }
            int defaultDistance   = 35;
            int defaultTurnDegree = 20;

            bool flipFunctionKeyDown  = Keyboard.IsKeyDown(Key.LeftCtrl);
            bool speedFunctionKeyDown = Keyboard.IsKeyDown(Key.LeftShift);


            if (joystickMode && !joystickDataSender.IsEnabled && e.Key == Key.Up && !flipFunctionKeyDown)
            {
                joystickDataSender.Start();
            }

            FlipShift(flipFunctionKeyDown);
            SpeedShift(speedFunctionKeyDown);

            if (KeyDown && e.Key != Key.Escape)
            {
                escapeTimes = 0;
                emergency_notification.Visibility = Visibility.Collapsed;
            }

            try
            {
                switch (e.Key)
                {
                case Key.W: HandleKeyEvent(KeyDown, img_forward_gray); if (KeyDown && !joystickMode)
                    {
                        client.Forward(defaultDistance);
                    }
                    break;

                case Key.S: HandleKeyEvent(KeyDown, img_backward_gray); if (KeyDown && !joystickMode)
                    {
                        client.Backward(defaultDistance);
                    }
                    break;

                case Key.A: HandleKeyEvent(KeyDown, img_left_gray); if (KeyDown && !joystickMode)
                    {
                        client.Left(defaultDistance);
                    }
                    break;

                case Key.D: HandleKeyEvent(KeyDown, img_right_gray); if (KeyDown && !joystickMode)
                    {
                        client.Right(defaultDistance);
                    }
                    break;

                case Key.Up:
                    HandleKeyEvent(KeyDown, img_up_gray, flipFunctionKeyDown, img_flip_forward);
                    if (KeyDown && !joystickMode && !flipFunctionKeyDown)
                    {
                        client.Up(defaultDistance);
                    }
                    if (KeyDown && flipFunctionKeyDown)
                    {
                        client.Flip(FlipDirection.FORWARD);
                    }
                    break;

                case Key.Down:
                    HandleKeyEvent(KeyDown, img_down_gray, flipFunctionKeyDown, img_flip_backward);
                    if (KeyDown && !joystickMode && !flipFunctionKeyDown)
                    {
                        client.Down(defaultDistance);
                    }
                    if (KeyDown && flipFunctionKeyDown)
                    {
                        client.Flip(FlipDirection.BACKWARD);
                    }
                    break;

                case Key.Left:
                    HandleKeyEvent(KeyDown, img_ccw_gray, flipFunctionKeyDown, img_flip_left);
                    if (KeyDown && !joystickMode && !flipFunctionKeyDown)
                    {
                        client.TurnLeft(defaultTurnDegree);
                    }
                    if (KeyDown && flipFunctionKeyDown)
                    {
                        client.Flip(FlipDirection.LEFT);
                    }
                    break;

                case Key.Right:
                    HandleKeyEvent(KeyDown, img_cw_gray, flipFunctionKeyDown, img_flip_right);
                    if (KeyDown && !joystickMode && !flipFunctionKeyDown)
                    {
                        client.TurnRight(defaultTurnDegree);
                    }
                    if (KeyDown && flipFunctionKeyDown)
                    {
                        client.Flip(FlipDirection.RIGHT);
                    }
                    break;

                case Key.Space: HandleKeyEvent(KeyDown, img_land_gray); if (KeyDown)
                    {
                        client.Land();
                    }
                    joystickDataSender.Stop(); break;

                case Key.Escape: HandleKeyEvent(KeyDown, img_emergency_gray); if (KeyDown)
                    {
                        HandleEmergency();
                    }
                    break;

                case Key.Enter: HandleKeyEvent(KeyDown, img_takeoff_gray); if (KeyDown)
                    {
                        client.TakeOff();
                    }
                    break;

                case Key.R: HandleKeyEvent(KeyDown, img_start_rotors_gray); if (KeyDown)
                    {
                        client.StartRotors();
                    }
                    break;

                case Key.Q: HandleKeyEvent(KeyDown, img_dec_speed_gray); if (KeyDown)
                    {
                        client.DecreaseSpeed();
                    }
                    break;

                case Key.E: HandleKeyEvent(KeyDown, img_inc_speed_gray); if (KeyDown)
                    {
                        client.IncreaseSpeed();
                    }
                    break;

                case Key.P: SwitchStream(KeyDown); break;
                }
            }
            catch (CommandIntegerParamException ie)
            {
                txt_response.Text       = ie.Message;
                txt_response.Background = red;
            }
            catch (CommandStringParamException se)
            {
                txt_response.Text       = se.Message;
                txt_response.Background = red;
            }
            catch (DroneCurveException ce)
            {
                txt_response.Text       = ce.Message;
                txt_response.Background = red;
            }
            txt_current_speed.Content = client.CurrentSpeed;
        }
Пример #2
0
        public async Task MainLoop()
        {
            await core.Init();

            RenderConsoleLoop();
            var shouldLoop = true;

            while (shouldLoop)
            {
                try
                {
                    var key = Console.ReadKey(true);
                    logger.LogInformation($"{key.Key} pressed");
                    switch (key.Key)
                    {
                    case ConsoleKey.W:
                    case ConsoleKey.S:
                    case ConsoleKey.A:
                    case ConsoleKey.D:
                    case ConsoleKey.R:
                    case ConsoleKey.F:
                        if (gamePadEnabled == true)
                        {
                            break;
                        }
                        logger.LogInformation($"FlyDirection({MoveMappings[key.Key]}, 30)");
                        await client.FlyDirection(MoveMappings[key.Key], 30);

                        break;

                    case ConsoleKey.Q:
                    case ConsoleKey.E:
                        if (gamePadEnabled == true)
                        {
                            break;
                        }
                        logger.LogInformation($"RotateDirection({(key.Key == ConsoleKey.E ? "cw" : "ccw")}, 20)");
                        await client.RotateDirection(key.Key == ConsoleKey.E, 20);

                        break;

                    case ConsoleKey.T:
                        logger.LogInformation("TakeOff()");
                        await client.TakeOff();

                        break;

                    case ConsoleKey.L:
                        logger.LogInformation("Land()");
                        await client.Land();

                        break;

                    case ConsoleKey.Spacebar:
                        logger.LogInformation("Emergency()");
                        await client.Emergency();

                        break;

                    case ConsoleKey.J:
                        if (gamePadEnabled)
                        {
                            gamePad.Close();
                            gamePadEnabled = false;
                        }
                        else
                        {
                            gamePadEnabled = gamePad.Listen();
                        }

                        logger.LogInformation($"Gamepad mode is {(gamePadEnabled ? "ON" : "OFF")}");
                        break;

                    case ConsoleKey.Escape:
                        logger.LogInformation("Closing all of the connections");
                        core.Close();
                        shouldLoop = false;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Exception while proccessing key press");
                }
            }
            logger.LogInformation("Exiting loop");
        }