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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <avr/wdt.h>
  16. #include "events.h"
  17. #include "parser.h"
  18. #include "x52.h"
  19. #include "cppm.h"
  20. #include "frsky.h"
  21. #include "config.h"
  22. #define ENABLE_SERIAL_PORT
  23. //#define DEBUG_OUTPUT Serial
  24. //#define DEBUG_MFD_UPTIME
  25. USB usb;
  26. USBHub hub(&usb);
  27. HIDUniversal hid(&usb);
  28. X52 x52(&usb, &hid);
  29. JoystickEventsCPPM joyCPPM;
  30. JoystickEventsButtons joyButtons((JoystickEvents*)&joyCPPM);
  31. JoystickEventsDeadZone joyDeadZone((JoystickEvents*)&joyButtons);
  32. JoystickReportParser joy(&joyDeadZone);
  33. FrSky frsky(&Serial);
  34. static void stringHelper(String& a, String& b, char delim, uint8_t line) {
  35. String s = a + delim;
  36. for (uint8_t i = 0; i < (14 - a.length() - b.length()); i++) {
  37. s += ' ';
  38. }
  39. s += b + delim;
  40. x52.setMFDText(line, s.c_str());
  41. }
  42. void statusCallback(uint8_t a1, uint8_t a2, uint8_t q1, uint8_t q2) {
  43. x52.setMFDText(0, "Telemetry Status");
  44. uint16_t l1 = q1 * 100 / 255, l2 = q2 * 100 / 255;
  45. String link1(l1), link2(l2);
  46. stringHelper(link1, link2, '%', 1);
  47. uint32_t v1 = a1 * 330 / 255;
  48. uint32_t v1hundred = v1 / 100;
  49. uint32_t v1ten = v1 % 100;
  50. String volt1 = String(v1hundred) + '.' + (v1ten);
  51. uint32_t v2 = a2 * 330 / 255;
  52. uint32_t v2hundred = v2 / 100;
  53. uint32_t v2ten = v2 % 100;
  54. String volt2 = String(v2hundred) + '.' + (v2ten);
  55. stringHelper(volt1, volt2, 'V', 2);
  56. }
  57. void setup() {
  58. #ifdef ENABLE_SERIAL_PORT
  59. Serial.begin(115200);
  60. #endif
  61. #ifdef DEBUG_OUTPUT
  62. DEBUG_OUTPUT.println("Start");
  63. #endif
  64. pinMode(13, OUTPUT);
  65. digitalWrite(13, LOW);
  66. eepromRead();
  67. if (usb.Init() == -1) {
  68. digitalWrite(13, HIGH);
  69. #ifdef DEBUG_OUTPUT
  70. DEBUG_OUTPUT.println("OSC did not start.");
  71. #endif
  72. }
  73. if (!hid.SetReportParser(0, &joy)) {
  74. digitalWrite(13, HIGH);
  75. #ifdef DEBUG_OUTPUT
  76. DEBUG_OUTPUT.println("Error adding report parser.");
  77. #endif
  78. }
  79. CPPM::instance().init();
  80. frsky.setDataHandler(&statusCallback);
  81. wdt_enable(WDTO_500MS);
  82. }
  83. void init_joystick() {
  84. x52.initialize();
  85. statusCallback(0, 0, 0, 0);
  86. // Sometimes the first message is lost, so send again
  87. if (joyButtons.getCurrentMode() == 1) {
  88. x52.setLEDBrightness(2);
  89. x52.setMFDBrightness(2);
  90. } else if (joyButtons.getCurrentMode() == 2) {
  91. x52.setLEDBrightness(1);
  92. x52.setMFDBrightness(1);
  93. } else if (joyButtons.getCurrentMode() == 3) {
  94. x52.setLEDBrightness(0);
  95. x52.setMFDBrightness(0);
  96. }
  97. }
  98. void loop() {
  99. wdt_reset();
  100. usb.Task();
  101. frsky.poll();
  102. static unsigned long lastTime = 0;
  103. static uint8_t initialized = 0;
  104. if ((millis() - lastTime) >= 1000) {
  105. lastTime = millis();
  106. if (initialized == 0) {
  107. initialized = 1;
  108. } else if (initialized == 1) {
  109. init_joystick();
  110. initialized = 2;
  111. }
  112. #ifdef DEBUG_MFD_UPTIME
  113. String text = "Uptime: " + String(millis() / 1000) + "s";
  114. x52.setMFDText(2, text.c_str());
  115. #endif
  116. }
  117. }