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

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