public override void UpdateJoystick(VJoy vjoy) { int v = ChannelValue; for (var i = 0; i < 16; i++) { var p = Parameters[i]; if (p.Enabled) { vjoy.SetButton(((v & (1 << i)) != 0) ^ p.Invert, p.Button); } } }
private void disconnect2() { VJoy.Release(); try { serialReader.ClosePort(); } catch (Exception) {} ActiveChannels = 0; comboProtocol.Enabled = true; comboPorts.Enabled = true; buttonPortSetup.Enabled = true; buttonPortsRefresh.Enabled = true; buttonProtocolSetup.Enabled = true; buttonConnect.Text = "Connect"; connected = false; toolStripStatusLabel.Text = "Disconnected"; comboJoysticks.Enabled = true; }
public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); Instance = this; Channels = new int[256]; VJoy = new VJoy(); if (!VJoy.Init()) { Application.Exit(); } comboProtocol.SelectedIndex = 0; reloadComPorts(); reloadJoysticks(); ChannelDataUpdate += onChannelDataUpdate; config = Configuration.Load(); reloadProfiles(); var defaultProfile = config.GetProfile(config.DefaultProfile); if (defaultProfile == null && comboProfiles.Items.Count > 0) { var first = comboProfiles.Items[0].ToString(); defaultProfile = config.GetProfile(first); comboProfiles.Text = first; } if (defaultProfile != null) { comboProfiles.Text = config.DefaultProfile; loadProfile(defaultProfile); } toolStripStatusLabel.Text = "Disconnected"; }
private void connect() { if (comboJoysticks.SelectedItem != null) { try { VJoy = vJoyEnumerator.GetVJoy(comboJoysticks.SelectedItem.ToString()); } catch (VJoyBase.VJoyException ex) { ErrorMessageBox(ex.Message, "VJoy Error"); return; } } serialReader = createSerialReader(); var sp = useCustomSerialParameters ? serialParameters : serialReader.GetDefaultSerialParameters(); if (!serialReader.OpenPort((string)comboPorts.SelectedItem, sp)) { ErrorMessageBox("Can not open the port", "Serial Error"); VJoy.Release(); return; } comboProtocol.Enabled = false; comboPorts.Enabled = false; buttonPortSetup.Enabled = false; buttonPortsRefresh.Enabled = false; buttonProtocolSetup.Enabled = false; buttonConnect.Text = "Disconnect"; comboJoysticks.Enabled = false; toolStripStatusLabel.Text = "Connecting ..."; connected = true; lua = new Lua(luaScript); backgroundWorker.RunWorkerAsync(); }
void BackgroundWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { Thread readerThread = null; try { serialReader.Init(Channels, protocolConfig); serialReader.Start(); double nextUIUpdateTime = 0, nextRateUpdateTime = 0, prevTime = 0; double updateSum = 0; double now; int updateCount = 0; int timeToWait; double failsafeAt = Now + failsafeTime; double nextFailsafeUpdate = 0; Failsafe = false; readerReadyToRead.Set(); readerResultReady.Reset(); readerThread = new Thread(SerialReaderRunner); readerThread.IsBackground = true; readerThread.Start(); /** * The main loop is a little complicated because it supports two modes of action: * 1. Normal mode. In this case the update rate is dictated by the the serial * reader * * 2. If we do not receive serial data for certain amount of time (failsafeTime) * we enter failsafe mode. In this mode we generate our own update rate, * while continuing to try to read the serial port. */ while (true) { if (backgroundWorker.CancellationPending) { return; } // If not in failsafe mode, we can afford to wait at most up to the time // when failsafe shall be activated. // If already in failsafe - wait until it is time for a failsafe update timeToWait = Failsafe ? (int)(nextFailsafeUpdate - Now) : (int)(failsafeAt - Now); if (timeToWait < 0) { timeToWait = 0; } bool readDone; if (readDone = readerResultReady.WaitOne(timeToWait)) { // serial read completed if (readerException == null) { // successful read ActiveChannels = readerResult; } else { // the SerialReader threw exception ActiveChannels = 0; if (readerException is InvalidOperationException) { System.Diagnostics.Debug.WriteLine(readerException.Message); this.Invoke((Action)(() => ErrorMessageBox("The Serial Port was Disconnected!", "Disconnect"))); backgroundWorker.CancelAsync(); continue; } else if (readerException is TimeoutException) { failsafeReason = "Serial Port Read Timeout"; } else { failsafeReason = readerException.Message; } } readerResultReady.Reset(); readerReadyToRead.Set(); } // else Wait timedout now = Now; if (readDone && ActiveChannels > 0) { // normal mode, we have serial data Failsafe = false; failsafeReason = null; } else if (!Failsafe && now >= failsafeAt) { // failsafeTime elapsed, time to get in failsafe ActiveChannels = 0; Failsafe = true; if (failsafeReason == null) { failsafeReason = "Waiting for Serial Data"; } } else if (Failsafe && now >= nextFailsafeUpdate) { // time for failsafe update } else { // no serial data and not yet time for failsafe or failsafe update continue; } // Update foreach (Mapping m in mappings) { if (m.Channel >= 0 && m.Channel < Channels.Length) { if (Failsafe) { m.Failsafe(); } else { m.Input = Channels[m.Channel]; } } } try { lua.Update(VJoy, Channels, Failsafe); } catch (NullReferenceException) { // could happen lua==null if we loadProfile while connected } catch (InterpreterException ex) { this.Invoke((Action)(() => ErrorMessageBox("Lua script execution failed. Scripting disabled:\n\n" + ex.DecoratedMessage, "Lua Error"))); } foreach (Mapping m in mappings) { m.UpdateJoystick(VJoy); } VJoy.SetState(); if (comAutomation != null) { comAutomation.Dispatch(); } if (webSocket != null) { webSocket.Dispatch(); } // since the time between frames may vary we sum the times here // and later publish the average if (now > prevTime) { updateSum += 1000.0 / (now - prevTime); updateCount++; } // update UI on every 100ms if (now >= nextUIUpdateTime) { nextUIUpdateTime = now + 100; // update the Rate on evert 500ms if (now >= nextRateUpdateTime) { nextRateUpdateTime = now + 500; if (updateCount > 0) { updateRate = updateSum / updateCount; updateSum = updateCount = 0; } } // will emit the ChannelDataUpdate event on the UI thread backgroundWorker.ReportProgress(0); } prevTime = now; if (!Failsafe) { failsafeAt = now + failsafeTime; } else { nextFailsafeUpdate = now + failsafeUpdateRate; } } } catch (Exception ex) { this.Invoke((Action)(() => ErrorMessageBox(ex.ToString(), "Main Worker"))); backgroundWorker.CancelAsync(); // needed to stop the reader thread } finally { if (readerThread != null) { readerThread.Join(); } serialReader.Stop(); } }
/// <summary> /// This method gets called when the mapping should write its joystick. /// </summary> abstract public void UpdateJoystick(VJoy vjoy);
public override void UpdateJoystick(VJoy vjoy) { pushed = Parameters.Transform(ChannelValue); vjoy.SetButton(pushed, Button); }
public override void UpdateJoystick(VJoy vjoy) { lastTransformedValue = Parameters.Transform(ChannelValue); vjoy.SetAxis(lastTransformedValue, Axis); }