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

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