Esempio n. 1
0
        /// <summary>
        ///     Read the port's current inputs and update the values accordingly
        /// </summary>
        /// <returns>An awaitable task that completes when finished</returns>
        protected override async Task readPort()
        {
            var data = await dev.ReadDataAsync((byte)numBytes).ConfigureAwait(false);

            for (var i = 0; i < Pins.Count; i++)
            {
                var bank = i / 8;
                var bit  = i % 8;
                if (data[bank] >> bit == 0x01)
                {
                    Pins[i].UpdateInputValue(true);
                }
                else
                {
                    Pins[i].UpdateInputValue(false);
                }
            }
        }
        public async Task UpdateAsync()
        {
            await dev.WriteByteAsync(0x00).ConfigureAwait(false);

            var response = await dev.ReadDataAsync(6).ConfigureAwait(false);

            var lx = (response[0] & 0x3F) - 32;
            var ly = (response[1] & 0x3F) - 32;
            var rx = (response[2] >> 7) | ((response[1] & 0xC0) >> 5) | (((response[0] & 0xC0) >> 3) - 16);
            var ry = (response[2] & 0x1F) - 16;

            leftStick.X = lx > 0 ? lx / 31f : lx / 32f;
            leftStick.Y = ly > 0 ? ly / 31f : ly / 32f;

            rightStick.X = rx > 0 ? rx / 15f : rx / 16f;
            rightStick.Y = ry > 0 ? ry / 15f : ry / 16f;

            var lt = ((response[2] & 0x60) >> 2) | (response[3] >> 5);
            var rt = response[3] & 0x1F;

            leftTriggerForce  = lt / 31f;
            rightTriggerForce = rt / 31f;

            var array = new BitArray(response.Skip(4).Take(2).ToArray());

            ((DigitalInPeripheralPin)R.Input).DigitalValue     = !array[1];
            ((DigitalInPeripheralPin)Plus.Input).DigitalValue  = !array[2];
            ((DigitalInPeripheralPin)Home.Input).DigitalValue  = !array[3];
            ((DigitalInPeripheralPin)Minus.Input).DigitalValue = !array[4];
            ((DigitalInPeripheralPin)L.Input).DigitalValue     = !array[5];

            // D-Pad stuff
            var temp = dPad;

            if (!array[6])
            {
                dPad = DPadState.Down;
            }
            else if (!array[7])
            {
                dPad = DPadState.Right;
            }
            else if (!array[8])
            {
                dPad = DPadState.Up;
            }
            else if (!array[9])
            {
                dPad = DPadState.Left;
            }
            else
            {
                dPad = DPadState.None;
            }

            if (temp != dPad)
            {
                DPadStateChanged?.Invoke(this, new DPadStateEventArgs {
                    NewValue = dPad
                });
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DPad)));
            }


            ((DigitalInPeripheralPin)ZR.Input).DigitalValue = !array[10];
            ((DigitalInPeripheralPin)X.Input).DigitalValue  = !array[11];
            ((DigitalInPeripheralPin)A.Input).DigitalValue  = !array[12];
            ((DigitalInPeripheralPin)Y.Input).DigitalValue  = !array[13];
            ((DigitalInPeripheralPin)B.Input).DigitalValue  = !array[14];
            ((DigitalInPeripheralPin)ZL.Input).DigitalValue = !array[15];
        }