//public static void Main(string[] args)
        //{
        //    Configure();
        //    Task t = Execute();
        //    t.Wait();
        //    System.Console.ReadKey();
        //}

        private async Task Execute()
        {
            _brick = new Brick(new BluetoothCommunication("EV3"));
            _brick.BrickChanged += _brick_BrickChanged;

            System.Console.WriteLine("Connecting...");
            await _brick.ConnectAsync();

            System.Console.WriteLine("Connected... Waiting for Commands...");
            await _brick.DirectCommand.PlayToneAsync(0x50, 5000, 500);

            //await _brick.SystemCommand.CopyFileAsync("test.rsf", "../prjs/Program/test.rsf");

            //await _brick.DirectCommand.PlaySoundAsync(100, "../prjs/Program/test");


            while (true)
            {
                Ev3Command command = PollForQueueMessage();


                if (command != null)
                {
                    await ProcessCommandAsync(command);
                }
            }
        }
        private async Task GoSlower(Ev3Command command)
        {
            Console.WriteLine("Moving slower...");

            uint distance = 30;

            if (!string.IsNullOrWhiteSpace(command.Value))
            {
                uint.TryParse(command.Value, out distance);
            }

            if (speed == 10)
            {
                distance *= 10;
                _brick.BatchCommand.Initialize(CommandType.DirectNoReply);
                _brick.BatchCommand.SetMotorPolarity(OutputPort.A, Polarity.Forward);
                _brick.BatchCommand.SetMotorPolarity(OutputPort.D, Polarity.Forward);
                _brick.BatchCommand.StepMotorAtPower(OutputPort.A, 10, distance, false);
                _brick.BatchCommand.StepMotorAtPower(OutputPort.D, 10, distance, false);
                await _brick.BatchCommand.SendCommandAsync();
            }

            speed    -= 5;
            distance *= 1000;
            _brick.BatchCommand.Initialize(CommandType.DirectNoReply);
            //_brick.BatchCommand.SetMotorPolarity(OutputPort.A, Polarity.Forward);
            //_brick.BatchCommand.SetMotorPolarity(OutputPort.D, Polarity.Backward);
            _brick.BatchCommand.StepMotorAtSpeed(OutputPort.A, speed, distance, false);
            _brick.BatchCommand.StepMotorAtSpeed(OutputPort.D, speed, distance, false);
            await _brick.BatchCommand.SendCommandAsync();
        }
        private async Task FireGun(Ev3Command command)
        {
            Console.WriteLine("Firing Gun...");

            uint distance = 30;

            if (!string.IsNullOrWhiteSpace(command.Value))
            {
                uint.TryParse(command.Value, out distance);
            }

            distance *= 500;

            //do
            //{

            System.Console.WriteLine(_distanceMeasured);
            _brick.BatchCommand.Initialize(CommandType.DirectNoReply);
            _brick.BatchCommand.SetMotorPolarity(OutputPort.B, Polarity.Forward);
            //_brick.BatchCommand.SetMotorPolarity(OutputPort.C, Polarity.Backward);
            _brick.BatchCommand.StepMotorAtSpeed(OutputPort.B, 50, distance, false);
            //_brick.BatchCommand.StepMotorAtPower(OutputPort.C, 100, 180, false);
            await _brick.BatchCommand.SendCommandAsync();


            //}

            //while (_distanceMeasured >= 30);
        }
        private async Task ReturnToCenter(Ev3Command command)
        {
            Console.WriteLine("Return to center...");

            _brick.BatchCommand.Initialize(CommandType.DirectNoReply);
            _brick.BatchCommand.SetMotorPolarity(OutputPort.C, Polarity.Backward);
            //_brick.BatchCommand.SetMotorPolarity(OutputPort.C, Polarity.Backward);
            //_brick.BatchCommand.StepMotorAtSpeed(OutputPort.C, 70, 50, false);
            _brick.BatchCommand.StepMotorAtPower(OutputPort.C, 100, 53, false);

            //_brick.BatchCommand.StepMotoputPort.C, 100, 180, false);
            await _brick.BatchCommand.SendCommandAsync();
        }
        private async Task MoveLeft(Ev3Command command)
        {
            Console.WriteLine("Moving Left...");

            _brick.BatchCommand.Initialize(CommandType.DirectNoReply);
            _brick.BatchCommand.SetMotorPolarity(OutputPort.C, Polarity.Forward);
            //_brick.BatchCommand.SetMotorPolarity(OutputPort.C, Polarity.Forward);
            //_brick.BatchCommand.StepMotorAtSpeed(OutputPort.C, 100, 50, false);
            _brick.BatchCommand.StepMotorAtSpeed(OutputPort.C, 70, 33, false);


            //await _brick.SystemCommand.CopyFileAsync("test.rsf", "../prjs/myapp/test.rsf");

            //_brick.BatchCommand.PlaySound(100, "../prjs/myapp/test");

            //_brick.BatchCommand.StepMotorAtPower(OutputPort.C, 100, 180, false);
            await _brick.BatchCommand.SendCommandAsync();
        }
        private async Task StartEngines(Ev3Command command)
        {
            Console.WriteLine("Starting Engines...");

            uint distance = 30;

            if (!string.IsNullOrWhiteSpace(command.Value))
            {
                uint.TryParse(command.Value, out distance);
            }

            distance *= 10;
            _brick.BatchCommand.Initialize(CommandType.DirectNoReply);
            _brick.BatchCommand.SetMotorPolarity(OutputPort.A, Polarity.Forward);
            _brick.BatchCommand.SetMotorPolarity(OutputPort.D, Polarity.Forward);
            _brick.BatchCommand.StepMotorAtPower(OutputPort.A, 10, distance, false);
            _brick.BatchCommand.StepMotorAtPower(OutputPort.D, 10, distance, false);
            await _brick.BatchCommand.SendCommandAsync();
        }
        private async Task MoveForward(Ev3Command command)
        {
            Console.WriteLine("Moving Forward...");

            uint distance = 30;

            if (!string.IsNullOrWhiteSpace(command.Value))
            {
                uint.TryParse(command.Value, out distance);
            }

            //if(_distanceMeasured <= 30)
            //{
            //    distance = 0;
            //}

            if (speed == 10)
            {
                distance *= 1000;
                _brick.BatchCommand.Initialize(CommandType.DirectNoReply);
                _brick.BatchCommand.SetMotorPolarity(OutputPort.A, Polarity.Forward);
                _brick.BatchCommand.SetMotorPolarity(OutputPort.D, Polarity.Forward);
                _brick.BatchCommand.StepMotorAtPower(OutputPort.A, 10, distance, false);
                _brick.BatchCommand.StepMotorAtPower(OutputPort.D, 10, distance, false);
                await _brick.BatchCommand.SendCommandAsync();

                speed += 5;
            }


            distance *= 100;
            _brick.BatchCommand.Initialize(CommandType.DirectNoReply);
            _brick.BatchCommand.SetMotorPolarity(OutputPort.A, Polarity.Forward);
            _brick.BatchCommand.SetMotorPolarity(OutputPort.D, Polarity.Forward);
            _brick.BatchCommand.StepMotorAtPower(OutputPort.A, 100, distance, false);
            _brick.BatchCommand.StepMotorAtPower(OutputPort.D, 100, distance, false);
            await _brick.BatchCommand.SendCommandAsync();
        }
        private async Task ProcessCommandAsync(Ev3Command command)
        {
            switch (command.Action)
            {
            case "start":
                await StartEngines(command);

                break;

            case "engines":
                await StartEngines(command);

                break;

            case "forward":
                await MoveForward(command);

                break;

            case "straight":
                await MoveForward(command);

                break;

            case "ahead":
                await MoveForward(command);

                break;

            case "backward":
                await MoveBackward(command);

                break;

            case "reverse":
                await MoveBackward(command);

                break;

            case "back":
                await MoveBackward(command);

                break;

            case "center":
                await ReturnToCenter(command);

                break;

            case "left":
                await MoveLeft(command);

                break;

            case "right":
                await MoveRight(command);

                break;

            case "stop":
                await StopMoving();

                break;

            case "don't move":
                await StopMoving();

                break;

            case "do not move":
                await StopMoving();

                break;

            case "break":
                await StopMoving();

                break;

            case "fast":
                await GoFaster(command);

                break;

            case "faster":
                await GoFaster(command);

                break;

            case "accelerate":
                await GoFaster(command);

                break;

            case "increase":
                await GoFaster(command);

                break;

            case "slow":
                await GoSlower(command);

                break;

            case "slower":
                await GoSlower(command);

                break;

            case "down":
                await GoSlower(command);

                break;

            case "decrease":
                await GoSlower(command);

                break;

            case "fire":
                await FireGun(command);

                break;

            case "shoot":
                await FireGun(command);

                break;

            case "blast":
                await FireGun(command);

                break;

            // Add more commands here as needed.
            default:
                break;
            }

            System.Console.WriteLine("Command executed.");
        }