Control drones with a proper joystick
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

Saitek-X52-PPM.ino 2.8KB

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