示例#1
0
 private void OnInputFrame(TwitchUser user, string frame)
 {
     if (_activeUser.Equals(user))
     {
         _sink.Update(InputFrame.ParseInputString(frame));
     }
 }
示例#2
0
        private void ThreadLoop(object arg)
        {
            var portName = (string)arg;

            _serialPort = new SerialPort
            {
                PortName     = portName,
                BaudRate     = 19200,
                Parity       = Parity.None,
                DataBits     = 8,
                StopBits     = StopBits.One,
                Handshake    = Handshake.None,
                ReadTimeout  = 100,
                WriteTimeout = 100
            };
            _serialPort.Open();
            if (!Sync())
            {
                throw new Exception("Unable to sync");
            }
            Console.WriteLine("Synced");

            var newFrame = new InputFrame();

            while (true)
            {
                if (newFrame.Wait <= 0)
                {
                    if (_queuedFrames.TryDequeue(out var queuedFrame))
                    {
                        newFrame = queuedFrame;
                        ApplyFrameToState(newFrame);
                        _callback?.Invoke(GetStateStr());
                        var packet = TranslateState(_state);
                        _serialPort.Write(packet, 0, packet.Length);
                    }
                }
                else
                {
                    newFrame.Wait--;
                }

                var resp = _serialPort.ReadByte();
                if (resp == 0x92)
                {
                    Console.Error.WriteLine("NACK");
                    if (!Sync())
                    {
                        throw new Exception("Unable to sync after NACK");
                    }
                }
                else if (resp != 0x90)
                {
                    // Unknown response
                }
            }
        }
示例#3
0
 private void ApplyFrameToState(InputFrame frame)
 {
     _state = new SwitchInputState
     {
         Buttons = (_state.Buttons | (frame.PressedButtons ?? Button.None)) & ~(frame.ReleasedButtons ?? Button.None), // & ~(Button.Home | Button.Share),
         DPad    = frame.DPad ?? _state.DPad,
         LeftX   = frame.LeftX ?? _state.LeftX,
         LeftY   = frame.LeftY ?? _state.LeftY,
         RightX  = frame.RightX ?? _state.RightX,
         RightY  = frame.RightY ?? _state.RightY
     };
 }
示例#4
0
        public static InputFrame ParseInputString(string str)
        {
            var frame = new InputFrame();
            var args  = str.Split(' ');

            foreach (var arg in args)
            {
                var kv = arg.Split('=');
                if (kv.Length != 2)
                {
                    throw new Exception($"Invalid input frame: {str}");
                }

                switch (kv[0])
                {
                case "P":
                    frame.PressedButtons = (Button)int.Parse(kv[1]);
                    break;

                case "R":
                    frame.ReleasedButtons = (Button)int.Parse(kv[1]);
                    break;

                case "D":
                    frame.DPad = (DPad)int.Parse(kv[1]);
                    break;

                case "LX":
                    frame.LeftX = byte.Parse(kv[1]);
                    break;

                case "LY":
                    frame.LeftY = byte.Parse(kv[1]);
                    break;

                case "RX":
                    frame.RightX = byte.Parse(kv[1]);
                    break;

                case "RY":
                    frame.RightY = byte.Parse(kv[1]);
                    break;

                default:
                    throw new Exception($"Invalid frame modifier specified: {str}");
                }
            }

            return(frame);
        }
示例#5
0
        public void Reset()
        {
            var newFrame = new InputFrame
            {
                PressedButtons  = Button.None,
                ReleasedButtons = Button.All,
                DPad            = DPad.None,
                LeftX           = 128,
                LeftY           = 128,
                RightX          = 128,
                RightY          = 128
            };

            _queuedFrames = new ConcurrentQueue <InputFrame>();
            Update(newFrame);
        }
示例#6
0
 public void Update(InputFrame newFrame)
 {
     _queuedFrames.Enqueue(newFrame);
 }