Ejemplo n.º 1
0
        static ControllerUpdate parseUpdate(JoystickUpdate update, JoystickControlUpdate l, JoystickControlUpdate r, double direction)
        {
            JoystickButtons btn = (JoystickButtons)update.Offset;

            switch (btn)
            {
            case JoystickButtons.X:
            case JoystickButtons.A:
            case JoystickButtons.B:
            case JoystickButtons.Y:
            case JoystickButtons.LB:
            case JoystickButtons.RB:
            case JoystickButtons.LT:
            case JoystickButtons.RT:
            case JoystickButtons.BACK:
            case JoystickButtons.START:
            case JoystickButtons.LJ:
            case JoystickButtons.RJ:
                return(new ButtonUpdate(btn, update.Value == 128));

            //case JoystickButtons.POV: //Top, Bottom, Left, Right
            //    double val = update.Value / 100;
            //    val = ((val > 180.0) && (val < 360.0)) ? val - 360.0 : val;
            //    val = (update.Value == -1) ? -1 : val;
            //    data += " " + ((update.Value == 0) || (update.Value == 4500) || (update.Value == 31500) ? 1 : 0) + " "
            //                + ((update.Value == 18000) || (update.Value == 13500) || (update.Value == 22500) ? 1 : 0) + " "
            //                + ((update.Value == 27000) || (update.Value == 22500) || (update.Value == 31500) ? 1 : 0) + " "
            //                + ((update.Value == 9000) || (update.Value == 4500) || (update.Value == 13500) ? 1 : 0) + " " + (val).ToString();
            //    break;
            case JoystickButtons.LX:
                return(new JoystickControlUpdate(btn, deadband(((update.Value / 65535.0) - 0.5) * -2.0 * direction, 0.25), l.y));

            case JoystickButtons.LY:
                return(new JoystickControlUpdate(btn, l.x, deadband(((update.Value / 65535.0) - 0.5) * 2.0 * direction, 0.25)));

            case JoystickButtons.RX:
                return(new JoystickControlUpdate(btn, deadband(((update.Value / 65535.0) - 0.5) * -2.0 * direction, 0.25), r.y));

            case JoystickButtons.RY:
                return(new JoystickControlUpdate(btn, r.x, deadband(((update.Value / 65535.0) - 0.5) * 2.0 * direction, 0.25)));

            default:
                return(null);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            /** BLUETOOTH INIT **/
            // Replace this COM port by the appropriate one on your computer
            SerialPort arduino = new SerialPort("COM7");

            //Open Arduino connection
            while (!arduino.IsOpen)
            {
                Console.WriteLine("Attempting to connect to Arduino (COM 7)...");
                try
                {
                    arduino.Open();
                } catch (Exception)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }
            Console.WriteLine("Connected to Arduino!");

            /** CONTROLLER INIT **/
            // Initialize DirectInput
            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
                                                                  DeviceEnumerationFlags.AllDevices))
            {
                joystickGuid = deviceInstance.InstanceGuid;
            }

            // If Gamepad not found, look for a Joystick
            if (joystickGuid == Guid.Empty)
            {
                foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick,
                                                                      DeviceEnumerationFlags.AllDevices))
                {
                    joystickGuid = deviceInstance.InstanceGuid;
                }
            }

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Console.WriteLine("No joystick/Gamepad found.");
                Console.ReadKey();
                Environment.Exit(1);
            }

            // Instantiate the joystick
            var joystick = new Joystick(directInput, joystickGuid);

            Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

            // Set BufferSize in order to use buffered data.
            joystick.Properties.BufferSize = 128;

            // Acquire the joystick
            joystick.Acquire();

            // Poll events from joystick
            JoystickControlUpdate joyL = new JoystickControlUpdate();
            JoystickControlUpdate joyR = new JoystickControlUpdate();

            int  direction  = 1; //direction switch
            bool lastYState = false;

            /** EVENT LOOP **/
            while (true)
            {
                try
                {
                    //Poll joystick
                    joystick.Poll();
                    var updates = joystick.GetBufferedData();
                    //JoystickState state = joystick.GetCurrentState();

                    foreach (JoystickUpdate update in updates)
                    {
                        ControllerUpdate result = parseUpdate(update, joyL, joyR, direction);
                        if (result.updateType == ControllerUpdateType.BUTTON)
                        {
                            ButtonUpdate btnUpdate = (ButtonUpdate)result;

                            Console.WriteLine(btnUpdate.ToString());

                            //We have nothing to do with buttons right now

                            if ((btnUpdate.btn == JoystickButtons.Y) && (btnUpdate.pressed == true) && (lastYState == false))
                            {
                                direction = -direction;
                            }
                            if (btnUpdate.btn == JoystickButtons.Y)
                            {
                                lastYState = btnUpdate.pressed;
                            }
                        }
                        else if (result.updateType == ControllerUpdateType.JOYSTICK)
                        {
                            //TODO put everything below in here
                            JoystickControlUpdate joyUpdate = (JoystickControlUpdate)result;

                            //Flush if previous bin is not equal to previous bin
                            bool flush = false;
                            if (joyUpdate.getJoystick() == JoystickType.LEFT)
                            {
                                flush = (joyL.getBinX() != joyUpdate.getBinX()) || (joyL.getBinY() != joyUpdate.getBinY());
                                joyL  = joyUpdate;
                            }
                            if (joyUpdate.getJoystick() == JoystickType.RIGHT)
                            {
                                flush = (joyR.getBinX() != joyUpdate.getBinX()) || (joyR.getBinY() != joyUpdate.getBinY());
                                joyR  = joyUpdate;
                            }

                            try
                            {
                                Console.WriteLine(joyUpdate.ToBinnedString(direction));
                                if (flush)
                                {
                                    arduino.WriteLine(joyUpdate.ToBinnedString(direction));
                                }
                            }
                            catch (Exception)
                            {
                                if (!arduino.IsOpen)
                                {
                                    do
                                    {
                                        Console.WriteLine("Reconnecting to Arduino (COM 7)...");
                                        try
                                        {
                                            arduino.Open();
                                        }
                                        catch (Exception)
                                        {
                                            System.Threading.Thread.Sleep(1000);
                                        }
                                    } while (!arduino.IsOpen);
                                    Console.WriteLine("Connected to Arduino!");
                                }
                                else
                                {
                                    Console.WriteLine("Unknown Bluetooth exception. Restart program.");
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Error reading from joystick.");
                    bool okay = false;
                    while (!okay)
                    {
                        try
                        {
                            joystick.Unacquire();
                            joystick.Acquire();
                            okay = true;
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }