public void SendAnalog(Int32 LX, Int32 LY, Int32 RX, Int32 RY, int Delay) { try { if (Program.botRunning || _serialPort.IsOpen) { newFrame = new InputFrame(); newFrame.LeftX = (byte)LX; newFrame.LeftY = (byte)LY; newFrame.RightX = (byte)RX; newFrame.RightY = (byte)RY; Update(newFrame); BotWait(new Random().Next(800, 1000)); newFrame.LeftX = 128; newFrame.LeftY = 128; newFrame.RightX = 128; newFrame.RightY = 128; Update(newFrame); BotWait(Delay); } } catch (Exception ex) { Program.form.ApplyLog(ex.Message); } }
public void KeepAlive() { while (Program.botRunning) { 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--; Reset(); } try { 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 } } catch { Program.form.ApplyLog("Connection lost!"); Program.botConnected = false; Program.botRunning = false; } Thread.Sleep(10); } _serialPort.Dispose(); _serialPort.Close(); Program.form.UpdateStatus("Disconnected!"); Program.form.UpdateUSBDisplay(false); }
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); }
private void ApplyFrameToState(InputFrame frame) { try { _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 }; } catch (Exception ex) { Program.form.ApplyLog(ex.Message); } }
public void SendButton(Button button, int Delay) { try { newFrame = new InputFrame(); newFrame.PressedButtons = button; // newFrame.Wait = 1; Update(newFrame); BotWait(150); newFrame.PressedButtons = Button.None; newFrame.ReleasedButtons = Button.All; Update(newFrame); BotWait(Delay); } catch (Exception ex) { Program.form.ApplyLog(ex.Message); } }
public void SendDpad(DPad button, int Delay) { try { if (Program.botRunning || _serialPort.IsOpen) { newFrame = new InputFrame(); newFrame.DPad = button; //newFrame.Wait = 1; Update(newFrame); BotWait(150); newFrame.DPad = DPad.None; Update(newFrame); BotWait(Delay); } } catch (Exception ex) { Program.form.ApplyLog(ex.Message); } }
public void Reset() { try { var newFrame2 = new InputFrame { PressedButtons = Button.None, ReleasedButtons = Button.All, DPad = DPad.None, LeftX = 128, LeftY = 128, RightX = 128, RightY = 128, }; _queuedFrames = new ConcurrentQueue <InputFrame>(); Update(newFrame2); } catch (Exception ex) { Program.form.ApplyLog(ex.Message); } }
public void Update(InputFrame newFrame) { _queuedFrames.Enqueue(newFrame); }