Advertisement
Guest User

Untitled

a guest
Nov 15th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Input;
  8. using System.IO.Ports;
  9.  
  10. namespace poppetGamePad {
  11. class Program {
  12. static int Main(string[] args) {
  13. /*if (args.Length != 2) {
  14. Console.WriteLine("Invalid Args");
  15. return 1;
  16. }*/
  17. string COM = "COM17";
  18. int baud = 115200;
  19. SerialPort sp = new SerialPort(COM, baud);
  20. sp.Open();
  21. sp.Write(new byte[] { 7}, 0, 1);
  22. GamePadState oldState;
  23. GamePadState currentState = GamePad.GetState(PlayerIndex.One);
  24. bool running = true;
  25. while (running) {
  26. oldState = currentState;
  27. currentState = GamePad.GetState(PlayerIndex.One);
  28. if (currentState.Buttons.Back == ButtonState.Pressed) {
  29. running = false;
  30. }
  31.  
  32. if (currentState != oldState) {
  33. //do stuff
  34.  
  35. if (currentState.Buttons.A == ButtonState.Pressed) {
  36. //green
  37. sp.Write(new byte[] { (byte)(((byte)(currentState.Triggers.Right * 31) << 3) + 1) }, 0, 1);
  38. }
  39. if (currentState.Buttons.B == ButtonState.Pressed) {
  40. //red
  41. sp.Write(new byte[] { (byte)((byte)(currentState.Triggers.Right * 31) << 3) }, 0, 1);
  42. }
  43. if (currentState.Buttons.X == ButtonState.Pressed) {
  44. //blue
  45. sp.Write(new byte[] { (byte)(((byte)(currentState.Triggers.Right * 31) << 3) + 2) }, 0, 1);
  46. }
  47.  
  48. float X = currentState.ThumbSticks.Left.X;
  49. float Y = currentState.ThumbSticks.Left.Y;
  50.  
  51. float left = Y + X;
  52. float right = Y - X;
  53. if (left > 1) {
  54. left = 1;
  55. }
  56. if (left < -1) {
  57. left = -1;
  58. }
  59. if (right > 1) {
  60. right = 1;
  61. }
  62. if (right < -1) {
  63. right = -1;
  64. }
  65. Console.Write(left); Console.Write(" "); Console.WriteLine(right);
  66.  
  67. byte leftCommand = 3;
  68. byte rightCommand = 4;
  69. if (left < 0) {
  70. leftCommand = 5;
  71. left = -left;
  72. }
  73. if (right < 0) {
  74. rightCommand = 6;
  75. right = -right;
  76. }
  77. sp.Write(new byte[] { (byte)(((byte)(left * 31) << 3) + leftCommand) }, 0, 1);
  78. sp.Write(new byte[] { (byte)(((byte)(right * 31) << 3) + rightCommand) }, 0, 1);
  79. }
  80. if (currentState.Buttons.Y == ButtonState.Pressed) {
  81. //blue
  82. sp.Write(new byte[] { (byte)(((byte)(currentState.Triggers.Right * 31) << 3) + 7) }, 0, 1);
  83. }
  84. }
  85. sp.Close();
  86. return 0;
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement