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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. //#define DEBUG_INPUT
  22. USB usb;
  23. USBHub hub(&usb);
  24. HIDUniversal hid(&usb);
  25. X52 x52(&usb, &hid);
  26. JoystickEventsCPPM joyCPPM;
  27. JoystickEventsDeadZone joyevents((JoystickEvents*)&joyCPPM);
  28. JoystickReportParser joy(&joyevents);
  29. void setup() {
  30. #ifdef ENABLE_SERIAL_PORT
  31. Serial.begin(115200);
  32. #endif
  33. #ifdef DEBUG_OUTPUT
  34. Serial.println("Start");
  35. #endif
  36. if (usb.Init() == -1) {
  37. #ifdef DEBUG_OUTPUT
  38. Serial.println("OSC did not start.");
  39. #endif
  40. }
  41. if (!hid.SetReportParser(0, &joy)) {
  42. ErrorMessage<uint8_t >(PSTR("SetReportParser"), 1);
  43. }
  44. cppmInit();
  45. }
  46. void init_joystick() {
  47. x52.initialize();
  48. x52.setLEDBrightness(2);
  49. x52.setMFDBrightness(2);
  50. x52.setShift(0);
  51. x52.setBlink(0);
  52. x52.setMFDText(0, "Arduino X52 Host");
  53. x52.setMFDText(1, " initialized!");
  54. }
  55. void loop() {
  56. usb.Task();
  57. static unsigned long lastTime = 0;
  58. static uint8_t initialized = 0;
  59. if ((millis() - lastTime) >= 1000) {
  60. lastTime = millis();
  61. if (!initialized) {
  62. init_joystick();
  63. initialized = 1;
  64. }
  65. String text = "Uptime: " + String(millis() / 1000) + "s";
  66. x52.setMFDText(2, text.c_str());
  67. }
  68. #ifdef DEBUG_INPUT
  69. if (Serial.available()) {
  70. char c = Serial.read();
  71. if (c == 't') {
  72. x52.setMFDText(0, "Arduino");
  73. x52.setMFDText(1, "Hello");
  74. x52.setMFDText(2, "World");
  75. } else if (c == '0') {
  76. x52.setMFDBrightness(0);
  77. } else if (c == '1') {
  78. x52.setMFDBrightness(1);
  79. } else if (c == '2') {
  80. x52.setMFDBrightness(2);
  81. } else if (c == '3') {
  82. x52.setLEDBrightness(0);
  83. } else if (c == '4') {
  84. x52.setLEDBrightness(1);
  85. } else if (c == '5') {
  86. x52.setLEDBrightness(2);
  87. } else if (c == 'q') {
  88. x52.setShift(1);
  89. } else if (c == 'w') {
  90. x52.setShift(0);
  91. } else if (c == 'a') {
  92. x52.setBlink(1);
  93. } else if (c == 's') {
  94. x52.setBlink(0);
  95. } else if (c == 'z') {
  96. x52.setDate(1, 1, 1);
  97. } else if (c == 'x') {
  98. x52.setTime(12, 42);
  99. x52.setTimeOffset(0, -120);
  100. x52.setTimeOffset(0, 240);
  101. } else {
  102. Serial.println("Unknown command!");
  103. }
  104. }
  105. #endif
  106. }