Exemplo n.º 1
0
        //Event raise on connection received a message
        private void Con_MessageReceived(object sender, StringEventArgs e)
        {
            listBoxKey.Items.Insert(0, e.Message);
            if (e.Message.All(char.IsDigit))
            {
                int command = int.Parse(e.Message);
                switch ((_Command)command)
                {
                case _Command.Stop:
                {
                    car.Stop();
                    break;
                }

                case _Command.Avant:
                {
                    car.Forward();
                    break;
                }

                case _Command.Avant_Gauche:
                {
                    car.ForwardLeft();
                    break;
                }

                case _Command.Avant_Droite:
                {
                    car.ForwardRight();
                    break;
                }

                case _Command.Arriere:
                {
                    car.Backward();
                    break;
                }

                case _Command.Arriere_Gauche:
                {
                    car.BackwardLeft();
                    break;
                }

                case _Command.Arriere_Droite:
                {
                    car.BackwardRight();
                    break;
                }

                case _Command.Servo_Gauche:
                {
                    car.servoTurnLeft();
                    break;
                }

                case _Command.Servo_Droite:
                {
                    car.servoTurnRight();
                    break;
                }

                default:
                {
                    break;
                }
                }
            }
            //check if string contains space and continue using the _Command enum for setspeed etc...
            else if (e.Message.Split(' ').Length == 2 || e.Message.Split(' ')[0].All(char.IsDigit))
            {
                string[] fullCommand = e.Message.Split(' ');
                _Command command     = (_Command)int.Parse(fullCommand[0]);
                int      param       = int.Parse(fullCommand[1]);
                switch (command)
                {
                case _Command.Set_Speed:
                    car.setSpeed(param);
                    break;

                case _Command.Set_Left_Speed:
                    break;

                case _Command.Set_Right_Speed:
                    break;

                default:
                    break;
                }
            }
        }