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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "frsky.h"
  20. #define ENABLE_SERIAL_PORT
  21. //#define DEBUG_OUTPUT Serial
  22. //#define DEBUG_MFD_UPTIME
  23. USB usb;
  24. USBHub hub(&usb);
  25. HIDUniversal hid(&usb);
  26. X52 x52(&usb, &hid);
  27. JoystickEventsCPPM joyCPPM;
  28. JoystickEventsButtons joyButtons(&x52, (JoystickEvents*)&joyCPPM);
  29. JoystickEventsDeadZone joyDeadZone((JoystickEvents*)&joyButtons);
  30. JoystickReportParser joy(&joyDeadZone);
  31. FrSky frsky(&Serial);
  32. void setup() {
  33. #ifdef ENABLE_SERIAL_PORT
  34. Serial.begin(115200);
  35. #endif
  36. #ifdef DEBUG_OUTPUT
  37. DEBUG_OUTPUT.println("Start");
  38. #endif
  39. pinMode(13, OUTPUT);
  40. digitalWrite(13, LOW);
  41. if (usb.Init() == -1) {
  42. digitalWrite(13, HIGH);
  43. #ifdef DEBUG_OUTPUT
  44. DEBUG_OUTPUT.println("OSC did not start.");
  45. #endif
  46. }
  47. if (!hid.SetReportParser(0, &joy)) {
  48. digitalWrite(13, HIGH);
  49. #ifdef DEBUG_OUTPUT
  50. DEBUG_OUTPUT.println("Error adding report parser.");
  51. #endif
  52. }
  53. CPPM::instance().init();
  54. }
  55. void init_joystick() {
  56. x52.initialize();
  57. x52.setMFDText(0, "Arduino X52 Host");
  58. x52.setMFDText(1, "should be ready!");
  59. x52.setMFDText(2, " OK for options ");
  60. // Sometimes the first message is lost, so send again
  61. if (joyButtons.getCurrentMode() == 1) {
  62. x52.setLEDBrightness(2);
  63. x52.setMFDBrightness(2);
  64. } else if (joyButtons.getCurrentMode() == 2) {
  65. x52.setLEDBrightness(1);
  66. x52.setMFDBrightness(1);
  67. } else if (joyButtons.getCurrentMode() == 3) {
  68. x52.setLEDBrightness(0);
  69. x52.setMFDBrightness(0);
  70. }
  71. }
  72. void loop() {
  73. usb.Task();
  74. frsky.poll();
  75. static unsigned long lastTime = 0;
  76. static uint8_t initialized = 0;
  77. if ((millis() - lastTime) >= 1000) {
  78. lastTime = millis();
  79. if (initialized == 0) {
  80. initialized = 1;
  81. } else if (initialized == 1) {
  82. init_joystick();
  83. initialized = 2;
  84. }
  85. #ifdef DEBUG_MFD_UPTIME
  86. String text = "Uptime: " + String(millis() / 1000) + "s";
  87. x52.setMFDText(2, text.c_str());
  88. #endif
  89. }
  90. }