PS2XMouse.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <PS2X_lib.h>
  2. PS2X ps2x; // create PS2 Controller Class
  3. int error = 0;
  4. byte type = 0;
  5. const int ledPin = 11; // Mouse control LED (11 on Teensy 2.0, 13 on Arduino Leonardo)
  6. // parameters for reading the joystick:
  7. int range = 12; // output range of X or Y movement
  8. int responseDelay = 5; // response delay of the mouse, in ms
  9. int threshold = range/4; // resting threshold
  10. int center = range/2; // resting position value
  11. boolean mouseIsActive = false; // whether or not to control the mouse
  12. int lastSwitchState = LOW; // previous switch state
  13. void setup(){
  14. Serial.begin(57600);
  15. error = ps2x.config_gamepad(15,14,13,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  16. if(error == 0){
  17. Serial.println("Found Controller, configured successful");
  18. }
  19. else if(error == 1)
  20. Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
  21. else if(error == 2)
  22. Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
  23. else if(error == 3)
  24. Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  25. type = ps2x.readType();
  26. switch(type) {
  27. case 0:
  28. Serial.println("Unknown Controller type");
  29. break;
  30. case 1:
  31. Serial.println("DualShock Controller Found");
  32. break;
  33. case 2:
  34. Serial.println("GuitarHero Controller Found");
  35. break;
  36. }
  37. // take control of the mouse:
  38. Mouse.begin();
  39. Keyboard.begin();
  40. }
  41. void loop()
  42. {
  43. if(error == 1) //skip loop if no controller found
  44. return;
  45. ps2x.read_gamepad(false, 0); //read controller and set large motor to spin at 'vibrate' speed
  46. // read the switch:
  47. int switchState = ps2x.ButtonPressed(PSB_RED);
  48. // if it's changed and it's high, toggle the mouse state:
  49. if (switchState != lastSwitchState) {
  50. if (switchState == HIGH) {
  51. mouseIsActive = !mouseIsActive;
  52. // turn on LED to indicate mouse state:
  53. digitalWrite(ledPin, mouseIsActive);
  54. }
  55. }
  56. // save switch state for next comparison:
  57. lastSwitchState = switchState;
  58. // read and scale the two axes:
  59. int xReading = readAxis(PSS_LX);
  60. int yReading = readAxis(PSS_LY);
  61. // if the mouse control state is active, move the mouse:
  62. if (mouseIsActive) {
  63. Mouse.move(xReading, yReading, 0);
  64. }
  65. // read the mouse button and click or not click:
  66. // if the mouse button is pressed:
  67. if (ps2x.ButtonPressed(PSB_BLUE)) {
  68. // if the mouse is not pressed, press it:
  69. if (!Mouse.isPressed(MOUSE_LEFT)) {
  70. Mouse.press(MOUSE_LEFT);
  71. }
  72. }
  73. // else the mouse button is not pressed:
  74. else {
  75. // if the mouse is pressed, release it:
  76. if (Mouse.isPressed(MOUSE_LEFT)) {
  77. Mouse.release(MOUSE_LEFT);
  78. }
  79. }
  80. if (ps2x.Button(PSB_PAD_UP)) {
  81. Keyboard.press(KEY_UP_ARROW);
  82. } else {
  83. Keyboard.release(KEY_UP_ARROW);
  84. }
  85. if (ps2x.Button(PSB_PAD_DOWN)) {
  86. Keyboard.press(KEY_DOWN_ARROW);
  87. } else {
  88. Keyboard.release(KEY_DOWN_ARROW);
  89. }
  90. if (ps2x.Button(PSB_PAD_RIGHT)) {
  91. Keyboard.press(KEY_RIGHT_ARROW);
  92. } else {
  93. Keyboard.release(KEY_RIGHT_ARROW);
  94. }
  95. if (ps2x.Button(PSB_PAD_LEFT)) {
  96. Keyboard.press(KEY_LEFT_ARROW);
  97. } else {
  98. Keyboard.release(KEY_LEFT_ARROW);
  99. }
  100. delay(5);
  101. }
  102. /*
  103. reads an axis (0 or 1 for x or y) and scales the
  104. analog input range to a range from 0 to <range>
  105. */
  106. int readAxis(int thisAxis) {
  107. // read the analog input:
  108. int reading = ps2x.Analog(thisAxis);
  109. // map the reading from the analog input range to the output range:
  110. reading = map(reading, 0, 255, 0, range);
  111. // if the output reading is outside from the
  112. // rest position threshold, use it:
  113. int distance = reading - center;
  114. if (abs(distance) < threshold) {
  115. distance = 0;
  116. }
  117. // return the distance for this axis:
  118. return distance;
  119. }