public static void SetCtx(UsbCtx ctx)
 {
     ctx.Switch = UsbDevice.OpenUsbDevice(Switch);
     ctx.Write  = ctx.Switch.OpenEndpointWriter(WriteEndpointID.Ep01);
     ctx.Read   = ctx.Switch.OpenEndpointReader(ReadEndpointID.Ep01);
     ctx.IsSet  = true;
 }
 public static int WriteBuf(UsbCtx ctx, byte[] buffer)
 {
     if (!ctx.IsSet)
     {
         SetCtx(ctx);
     }
     ctx.Write.Write(buffer, 1000, out int len);
     return(len);
 }
 public static void WriteScreen(UsbCtx ctx)
 {
     using (var initBmp = new Bitmap(ScreenBounds.Width, ScreenBounds.Height, PixelFormat.Format24bppRgb))
         using (var graphics = Graphics.FromImage(initBmp))
         {
             graphics.CopyFromScreen(ScreenBounds.Left, ScreenBounds.Top, 0, 0, initBmp.Size, CopyPixelOperation.SourceCopy);
             using (var finalBmp = new Bitmap(initBmp, FrmBufBounds.Width, FrmBufBounds.Height))
             {
                 var locked = finalBmp.LockBits(FrmBufBounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                 WriteBuf(ctx, locked.Scan0.Bytes());
                 finalBmp.UnlockBits(locked);
             }
         }
 }
    public static InputPkg ReadPkg(UsbCtx ctx)
    {
        if (!ctx.IsSet)
        {
            SetCtx(ctx);
        }

        var buf = new byte[0x10];
        var pkg = new InputPkg();

        ctx.Read.Read(buf, 1000, out int len);

        pkg.HeldKeys = BitConverter.ToUInt64(buf, 0);
        pkg.LJoyX    = BitConverter.ToInt16(buf, 8);
        pkg.LJoyY    = BitConverter.ToInt16(buf, 10);
        pkg.RJoyX    = BitConverter.ToInt16(buf, 12);
        pkg.RJoyY    = BitConverter.ToInt16(buf, 14);

        return(pkg);
    }
Exemplo n.º 5
0
        private static void Main(string[] args)
        {
            var scp  = new ScpBus();
            var ctrl = new X360Controller();

            scp.PlugIn(1);

            using (var Context = new UsbCtx())
            {
                while (true)
                {
                    WriteScreen(Context);

                    var pkg = ReadPkg(Context);

                    void map(InputKeys inkey, X360Buttons outkey)
                    {
                        if ((pkg.HeldKeys & (ulong)inkey) > 0)
                        {
                            ctrl.Buttons |= outkey;
                        }
                        else
                        {
                            ctrl.Buttons &= ~outkey;
                        }
                    }

                    map(InputKeys.A, X360Buttons.B);
                    map(InputKeys.B, X360Buttons.A);
                    map(InputKeys.X, X360Buttons.Y);
                    map(InputKeys.Y, X360Buttons.X);

                    map(InputKeys.L, X360Buttons.LeftBumper);
                    map(InputKeys.R, X360Buttons.RightBumper);

                    map(InputKeys.LS, X360Buttons.LeftStick);
                    map(InputKeys.RS, X360Buttons.RightStick);

                    map(InputKeys.Plus, X360Buttons.Start);
                    map(InputKeys.Minus, X360Buttons.Back);

                    map(InputKeys.Up, X360Buttons.Up);
                    map(InputKeys.Down, X360Buttons.Down);
                    map(InputKeys.Left, X360Buttons.Left);
                    map(InputKeys.Right, X360Buttons.Right);

                    if ((pkg.HeldKeys & (ulong)InputKeys.ZL) > 0)
                    {
                        ctrl.LeftTrigger = byte.MaxValue;
                    }
                    else
                    {
                        ctrl.LeftTrigger = byte.MinValue;
                    }

                    if ((pkg.HeldKeys & (ulong)InputKeys.ZR) > 0)
                    {
                        ctrl.RightTrigger = byte.MaxValue;
                    }
                    else
                    {
                        ctrl.RightTrigger = byte.MinValue;
                    }

                    ctrl.LeftStickX = pkg.LJoyX;
                    ctrl.LeftStickY = pkg.LJoyY;

                    ctrl.RightStickX = pkg.RJoyX;
                    ctrl.RightStickY = pkg.RJoyY;

                    scp.Report(1, ctrl.GetReport());
                }
            }
        }