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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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
  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 joyevents((JoystickEvents*)&joyButtons);
  30. JoystickReportParser joy(&joyevents);
  31. FrSky frsky(&Serial);
  32. void setup() {
  33. #ifdef ENABLE_SERIAL_PORT
  34. Serial.begin(115200);
  35. #endif
  36. #ifdef DEBUG_OUTPUT
  37. Serial.println("Start");
  38. #endif
  39. if (usb.Init() == -1) {
  40. #ifdef DEBUG_OUTPUT
  41. Serial.println("OSC did not start.");
  42. #endif
  43. }
  44. if (!hid.SetReportParser(0, &joy)) {
  45. ErrorMessage<uint8_t >(PSTR("SetReportParser"), 1);
  46. }
  47. CPPM::instance().init();
  48. }
  49. void init_joystick() {
  50. x52.initialize();
  51. x52.setMFDText(0, "Arduino X52 Host");
  52. x52.setMFDText(1, "should be ready!");
  53. x52.setMFDText(2, " OK for options ");
  54. }
  55. void loop() {
  56. usb.Task();
  57. frsky.poll();
  58. static unsigned long lastTime = 0;
  59. static uint8_t initialized = 0;
  60. if ((millis() - lastTime) >= 1000) {
  61. lastTime = millis();
  62. if (!initialized) {
  63. init_joystick();
  64. initialized = 1;
  65. }
  66. #ifdef DEBUG_MFD_UPTIME
  67. String text = "Uptime: " + String(millis() / 1000) + "s";
  68. x52.setMFDText(2, text.c_str());
  69. #endif
  70. }
  71. }