No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsbJoystickDemo.pde 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* This demo uses the UsbJoystick.h library, a library based on AVRUSB from ObDev
  2. * It demonstrates a 6-axis joystick with 4 buttons by reading all the analog ports and
  3. * digtal pins 9,10,11,and 12
  4. * Adapted from the UsbKeyboard.h library from Phil Lindsay
  5. *
  6. * by Michel Gutlich 19-8-2008
  7. */
  8. #include <USBJoystick.h>
  9. unsigned short a2dValue;
  10. unsigned char high;
  11. unsigned char low;
  12. unsigned char temp;
  13. unsigned char report[8];
  14. void setup() {
  15. pinMode (9,INPUT);
  16. pinMode (10,INPUT);
  17. pinMode (11,INPUT);
  18. pinMode (12,INPUT);
  19. }
  20. void loop () {
  21. UsbJoystick.refresh(); // Let the AVRUSB driver do some houskeeping
  22. calculateReport(); // Jump to our port read routine that orders the values
  23. UsbJoystick.sendJoystick(report[0],report[1],reportBuffer[2],report[3],report[4],report[5],report[6],report[7]); // send the values
  24. }
  25. void calculateReport() { //The values read from the analog ports have to be ordered in a way the HID protocol wants it; a bit confusing.
  26. a2dValue = analogRead(0);
  27. high = a2dValue >> 8;
  28. low = a2dValue & 255;
  29. report[0] = low;
  30. temp = high;
  31. a2dValue = analogRead(1);
  32. high = a2dValue >> 6;
  33. low = a2dValue & 63;
  34. report[1] = (low << 2) + temp;
  35. temp = high;
  36. a2dValue =analogRead(2);
  37. high = a2dValue >> 4;
  38. low = a2dValue & 15;
  39. report[2] = (low << 4) + temp;
  40. temp = high;
  41. a2dValue = analogRead(3);
  42. high = a2dValue >> 2;
  43. low = a2dValue & 3;
  44. report[3] = (low << 6) + temp;
  45. temp = high;
  46. high = 0;
  47. low = 0;
  48. report[4] = temp;
  49. temp = high;
  50. a2dValue = analogRead(4);
  51. high = a2dValue >> 8;
  52. low = a2dValue & 255;
  53. report[5] = low + temp;
  54. temp = high;
  55. a2dValue = analogRead(5);
  56. high = a2dValue >> 6;
  57. low = a2dValue & 63;
  58. report[6] = (low << 2) + temp;
  59. temp = high;
  60. // 4 buttons , tossed around
  61. report[7] = (temp & 15) + (digitalRead(9) << 4) + (digitalRead(10) << 5) + (digitalRead(11) << 6) + (digitalRead(12) << 7);
  62. }