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.

events_cppm.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <Arduino.h>
  13. #include "data.h"
  14. #include "cppm.h"
  15. #include "events.h"
  16. JoystickEventsCPPM::JoystickEventsCPPM(JoystickEvents* client) : JoystickEvents(client) {
  17. for (uint8_t i = 0; i < channels; i++) {
  18. values[i] = 1500;
  19. invert[i] = 0;
  20. minimum[i] = 1000;
  21. maximum[i] = 2000;
  22. trim[i] = 0;
  23. }
  24. /*
  25. * Aux switches are commonly used for arming.
  26. * Ensure we're not sending high values when
  27. * no joystick has been connected...
  28. */
  29. values[CHANNEL_AUX1] = 1000;
  30. values[CHANNEL_AUX2] = 1000;
  31. /*
  32. * Default values to match my personal setup.
  33. * Can be changed using the on-screen menu.
  34. */
  35. invert[CHANNEL_THROTTLE] = 1;
  36. invert[CHANNEL_PITCH] = 1;
  37. minimum[CHANNEL_THROTTLE] = 1010;
  38. maximum[CHANNEL_THROTTLE] = 1950;
  39. minimum[CHANNEL_ROLL] = 1050;
  40. maximum[CHANNEL_ROLL] = 1950;
  41. minimum[CHANNEL_PITCH] = 1080;
  42. maximum[CHANNEL_PITCH] = 1890;
  43. minimum[CHANNEL_AUX1] = 990;
  44. maximum[CHANNEL_AUX1] = 2100;
  45. minimum[CHANNEL_AUX2] = 990;
  46. maximum[CHANNEL_AUX2] = 1990;
  47. CPPM::instance().copy(values);
  48. }
  49. void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) {
  50. for (uint8_t i = 0; i < (CHANNEL_AUX2 + 1); i++) {
  51. uint16_t value = ((int32_t)getJoystickAxis(evt, i)) + trim[i];
  52. values[i] = map(value, 0, getJoystickMax(i),
  53. invert[i] ? maximum[i] : minimum[i], invert[i] ? minimum[i] : maximum[i]);
  54. }
  55. CPPM::instance().copy(values);
  56. if (client) {
  57. client->OnGamePadChanged(evt);
  58. }
  59. }
  60. uint16_t JoystickEventsCPPM::getJoystickAxis(const GamePadEventData& evt, uint8_t ch) {
  61. if (ch == CHANNEL_ROLL) {
  62. return evt.X;
  63. } else if (ch == CHANNEL_PITCH) {
  64. return evt.Y;
  65. } else if (ch == CHANNEL_THROTTLE) {
  66. return evt.Z;
  67. } else if (ch == CHANNEL_YAW) {
  68. return evt.Rz;
  69. } else if (ch == CHANNEL_AUX1) {
  70. return evt.Ry;
  71. } else if (ch == CHANNEL_AUX2) {
  72. return evt.Slider;
  73. } else if (ch == (CHANNEL_AUX2 + 1)) {
  74. return evt.Rx;
  75. } else {
  76. return 0;
  77. }
  78. }
  79. uint16_t JoystickEventsCPPM::getJoystickMax(uint8_t ch) {
  80. if (ch == CHANNEL_ROLL) {
  81. return 0x7FF;
  82. } else if (ch == CHANNEL_PITCH) {
  83. return 0x7FF;
  84. } else if (ch == CHANNEL_THROTTLE) {
  85. return 0xFF;
  86. } else if (ch == CHANNEL_YAW) {
  87. return 0x3FF;
  88. } else if (ch == CHANNEL_AUX1) {
  89. return 0xFF;
  90. } else if (ch == CHANNEL_AUX2) {
  91. return 0xFF;
  92. } else if (ch == (CHANNEL_AUX2 + 1)) {
  93. return 0xFF;
  94. } else {
  95. return 0xFF;
  96. }
  97. }