Geen omschrijving
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.

USBPPM.ino 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "UsbJoystick.h"
  2. #include "PPM.h"
  3. #define CHANNELS 8
  4. // D3 / PD3 / INT1 for PPM input
  5. // D2 / PD2 / INT0 used for D+ of USB
  6. // D4 / PD4 used for D- of USB
  7. unsigned short a2dValue;
  8. unsigned char high;
  9. unsigned char low;
  10. unsigned char temp;
  11. unsigned char report[8];
  12. void setup(void) {
  13. //Serial.begin(115200);
  14. //Serial.println("Init");
  15. ppm.begin(CHANNELS);
  16. usbDeviceConnect();
  17. }
  18. void loop(void) {
  19. UsbJoystick.refresh(); // Let the AVRUSB driver do some houskeeping
  20. calculateReport(); // Jump to our port read routine that orders the values
  21. UsbJoystick.sendJoystick(report[0],report[1],report[2],report[3],report[4],report[5],report[6],report[7]); // send the values
  22. }
  23. void calculateReport() { //The values read from the analog ports have to be ordered in a way the HID protocol wants it; a bit confusing.
  24. if(!ppm.available()) {
  25. //Serial.println("abort");
  26. return;
  27. }
  28. /*
  29. Serial.print(ppm.get(0) - MIN_PULSE);
  30. Serial.print(' ');
  31. Serial.print(ppm.get(1) - MIN_PULSE);
  32. Serial.print(' ');
  33. Serial.print(ppm.get(2) - MIN_PULSE);
  34. Serial.println();
  35. */
  36. a2dValue = ppm.get(1) - MIN_PULSE;
  37. high = a2dValue >> 8;
  38. low = a2dValue & 255;
  39. report[0] = low;
  40. temp = high;
  41. a2dValue = ppm.get(0) - MIN_PULSE;
  42. high = a2dValue >> 6;
  43. low = a2dValue & 63;
  44. report[1] = (low << 2) + temp;
  45. temp = high;
  46. a2dValue = ppm.get(2) - MIN_PULSE;
  47. high = a2dValue >> 4;
  48. low = a2dValue & 15;
  49. report[2] = (low << 4) + temp;
  50. temp = high;
  51. a2dValue = ppm.get(3) - MIN_PULSE;
  52. high = a2dValue >> 2;
  53. low = a2dValue & 3;
  54. report[3] = (low << 6) + temp;
  55. temp = high;
  56. high = 0;
  57. low = 0;
  58. report[4] = temp;
  59. temp = high;
  60. a2dValue = ppm.get(4) - MIN_PULSE;
  61. high = a2dValue >> 8;
  62. low = a2dValue & 255;
  63. report[5] = low + temp;
  64. temp = high;
  65. a2dValue = ppm.get(5) - MIN_PULSE;
  66. high = a2dValue >> 6;
  67. low = a2dValue & 63;
  68. report[6] = (low << 2) + temp;
  69. temp = high;
  70. // 4 buttons , tossed around
  71. report[7] = (temp & 15);
  72. }