Control drones with a proper joystick
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.

Saitek-X52-PPM.ino 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Saitek X52 Arduino USB Host Shield Library.
  3. * Copyright 2016 by Thomas Buck <xythobuz@xythobuz.de>
  4. *
  5. * Based on the USB Host Library HID Joystick example
  6. * https://www.circuitsathome.com/mcu/hid-joystick-code-sample
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2.
  11. */
  12. #include <hid.h>
  13. #include <hiduniversal.h>
  14. #include <usbhub.h>
  15. #include "events.h"
  16. #include "parser.h"
  17. #include "x52.h"
  18. #include "cppm.h"
  19. #define ENABLE_SERIAL_PORT
  20. #define DEBUG_OUTPUT
  21. USB usb;
  22. USBHub hub(&usb);
  23. HIDUniversal hid(&usb);
  24. X52 x52(&usb, &hid);
  25. JoystickEventsCPPM joyCPPM;
  26. JoystickEventsDeadZone joyevents((JoystickEvents*)&joyCPPM);
  27. JoystickReportParser joy(&joyevents);
  28. void setup() {
  29. #ifdef ENABLE_SERIAL_PORT
  30. Serial.begin(115200);
  31. #endif
  32. #ifdef DEBUG_OUTPUT
  33. Serial.println("Start");
  34. #endif
  35. if (usb.Init() == -1) {
  36. #ifdef DEBUG_OUTPUT
  37. Serial.println("OSC did not start.");
  38. #endif
  39. }
  40. if (!hid.SetReportParser(0, &joy)) {
  41. ErrorMessage<uint8_t >(PSTR("SetReportParser"), 1);
  42. }
  43. cppmInit();
  44. }
  45. void init_joystick() {
  46. x52.setLEDBrightness(2);
  47. x52.setMFDBrightness(2);
  48. x52.setShift(0);
  49. x52.setBlink(0);
  50. x52.setMFDText(0, "Arduino X52 Host");
  51. x52.setMFDText(1, " has been ");
  52. x52.setMFDText(2, " initialized! ");
  53. }
  54. void loop() {
  55. usb.Task();
  56. static unsigned long lastTime = 0;
  57. static uint8_t initialized = 0;
  58. if ((millis() - lastTime) >= 1000) {
  59. lastTime = millis();
  60. if (!initialized) {
  61. init_joystick();
  62. initialized = 1;
  63. }
  64. }
  65. #ifdef DEBUG_OUTPUT
  66. if (Serial.available()) {
  67. char c = Serial.read();
  68. if (c == 't') {
  69. x52.setMFDText(0, "Arduino");
  70. x52.setMFDText(1, "Hello");
  71. x52.setMFDText(2, "World");
  72. } else if (c == '0') {
  73. x52.setMFDBrightness(0);
  74. } else if (c == '1') {
  75. x52.setMFDBrightness(1);
  76. } else if (c == '2') {
  77. x52.setMFDBrightness(2);
  78. } else if (c == '3') {
  79. x52.setLEDBrightness(0);
  80. } else if (c == '4') {
  81. x52.setLEDBrightness(1);
  82. } else if (c == '5') {
  83. x52.setLEDBrightness(2);
  84. } else if (c == 'q') {
  85. x52.setShift(1);
  86. } else if (c == 'w') {
  87. x52.setShift(0);
  88. } else if (c == 'a') {
  89. x52.setBlink(1);
  90. } else if (c == 's') {
  91. x52.setBlink(0);
  92. } else if (c == 'z') {
  93. x52.setDate(1, 1, 1);
  94. } else if (c == 'x') {
  95. x52.setTime(12, 42);
  96. x52.setTimeOffset(0, -120);
  97. x52.setTimeOffset(0, 240);
  98. } else {
  99. Serial.println("Unknown command!");
  100. }
  101. }
  102. #endif
  103. }