private void joystickTimer_Tick_1(object sender, EventArgs e) { try { joystick.UpdateStatus(); joystickButtons = joystick.buttons; if (joystick.Xaxis == 0) { try { SP.Open(); SP.Write("e"); Thread.Sleep(1); SP.Close(); } catch { MessageBox.Show("ERROR"); } output.Text += "Left\n"; } if (joystick.Xaxis == 65535) //right { try { SP.Open(); SP.Write("f"); Thread.Sleep(1); SP.Close(); } catch { MessageBox.Show("ERROR"); } output.Text += "Right\n"; } if (joystick.Yaxis == 0) { try { SP.Open(); SP.Write("a"); Thread.Sleep(5); SP.Close(); } catch { MessageBox.Show("ERROR"); } output.Text += "Down\n"; } if (joystick.Yaxis == 65535) { try { SP.Open(); SP.Write("b"); Thread.Sleep(5); SP.Close(); } catch { MessageBox.Show("ERROR"); } output.Text += "Up\n"; } if (joystick.jsPOV[0] == 27000) { try { SP.Open(); SP.Write("c"); Thread.Sleep(5); SP.Close(); } catch { MessageBox.Show("ERROR"); } output.Text += Convert.ToString(joystick.jsPOV[0]) + "\n"; } if (joystick.jsPOV[0] == 9000) { try { SP.Open(); SP.Write("d"); Thread.Sleep(5); SP.Close(); } catch { MessageBox.Show("ERROR"); } output.Text += Convert.ToString(joystick.jsPOV[0]) + "\n"; } if (joystick.jsPOV[0] == 18000) { try { SP.Open(); SP.Write("g"); Thread.Sleep(5); SP.Close(); } catch { MessageBox.Show("ERROR"); } output.Text += Convert.ToString(joystick.jsPOV[0]) + "\n"; } for (int i = 0; i < joystickButtons.Length; i++) { if (joystickButtons[i] == true) { output.Text += "Button " + i + " Pressed\n"; } } SP.Dispose(); } catch { joystickTimer.Enabled = false; connectToJoystick(joystick); } }
private void joystickTimer_Tick_1(object sender, EventArgs e) { JoystickState state; try { joystick.UpdateStatus(); joystickButtons = joystick.buttons; //state = joystick.GetJoystickState(); //System.Diagnostics.Trace.WriteLine("loc: " + joystick.Xaxis + " " + joystick.Yaxis + " " + state.Rx + " " + state.Ry); System.Diagnostics.Trace.WriteLine("loc: " + joystick.Xaxis + " " + joystick.Yaxis); if (joystick.Xaxis == 0) { output.Text += "Left\n"; } if (joystick.Xaxis == 65535) { output.Text += "Right\n"; } if (joystick.Yaxis == 0) { output.Text += "Up\n"; } if (joystick.Yaxis == 65535) { output.Text += "Down\n"; } for (int i = 0; i < joystickButtons.Length; i++) { if (joystickButtons[i] == true) { output.Text += "Button " + i + " Pressed\n"; } } this.txtXRaw.Text = joystick.Xaxis.ToString(); this.txtYRaw.Text = joystick.Yaxis.ToString(); this.txtRzRaw.Text = joystick.RZaxis.ToString(); this.txtThRaw.Text = joystick.Throttle.ToString(); float processedX; float processedY; float xfactor = (float)(65535 - joystick.Throttle) / 65535f + 1f; processedX = (joystick.Xaxis + this.settings.x1Offset) * xfactor * this.settings.x1Factor; processedY = (joystick.Yaxis + this.settings.x2Offset) * xfactor * this.settings.x2Factor; float processedRZ = (joystick.RZaxis + this.settings.RzOffset) * this.settings.RzFactor; this.txtXProcessed.Text = processedX.ToString(); this.txtYProcessed.Text = processedY.ToString(); this.txtRzProcessed.Text = processedRZ.ToString(); // point of view (x,y counters) switch (joystick.PointOfView) { case -1: // do nothing - top stick is idle break; case 0: // move up if (this.topStickY < this.settings.TopStickMaxY) { this.topStickY += this.settings.TopStickStepY; } break; case 90: // move right if (this.topStickX < this.settings.TopStickMaxX) { this.topStickX += this.settings.TopStickStepX; } break; case 180: // move down if (this.topStickY > this.settings.TopStickMinY) { this.topStickY -= this.settings.TopStickStepY; } break; case 270: // move left if (this.topStickX > this.settings.TopStickMinX) { this.topStickX -= this.settings.TopStickStepX; } break; } int track4ActualValue = this.track4.Maximum - this.track4.Value + this.track4.Minimum; int track8ActualValue = this.track8.Maximum - this.track8.Value + this.track8.Minimum; this.txtTrack1.Text = this.track1.Value.ToString(); this.txtTrack2.Text = this.track2.Value.ToString(); this.txtTrack3.Text = this.track3.Value.ToString(); this.txtTrack4.Text = track4ActualValue.ToString(); this.txtTrack5.Text = this.track5.Value.ToString(); this.txtTrack6.Text = this.track6.Value.ToString(); this.txtTrack7.Text = this.track7.Value.ToString(); this.txtTrack8.Text = track8ActualValue.ToString(); WriteComport(processedX, processedY, processedRZ, topStickX, topStickY, this.track1.Value, this.track2.Value, this.track3.Value, track4ActualValue, this.track5.Value, this.track6.Value, this.track7.Value, this.track8.Value); } catch { joystickTimer.Enabled = false; connectToJoystick(joystick); } }
/// <summary> /// Returns the joystick button, will be used for debouncing, check if the return is button.notRecognized /// </summary> /// <param name="js">Joystick object</param> /// <param name="jsTimer">JoystickTimer (Control) that ticks for the joystick</param> public static buttons getPressedButton(Joystick js, Timer jsTimer) { previousState = currentState; try { js.UpdateStatus(); joystickButtons = js.availableButtons; //Get joystick all buttons in a bool array if (js.Xaxis == 0) { currentState = buttons.Left; } if (js.Xaxis == axis_Max_Val) { currentState = buttons.Right; } if (js.Yaxis == 0) { currentState = buttons.Up; } if (js.Yaxis == axis_Max_Val) { currentState = buttons.Down; } //Find the pressed button for (int i = 0; i < joystickButtons.Length; i++) { if (joystickButtons[i] == true) //The pressed button will have a true value { currentState = (buttons)(i + 1); //Save the pressed button } } } catch { jsTimer.Enabled = false; //Disable the timer Joystick.connectToJoystick(js, jsTimer); //Reconnect to the joystick } return currentState; //Return the current button }