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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifndef __JOYSTICK_EVENTS_H__
  13. #define __JOYSTICK_EVENTS_H__
  14. #include <stdint.h>
  15. #include "config.h"
  16. class GamePadEventData;
  17. class JoystickEvents {
  18. public:
  19. JoystickEvents(JoystickEvents* _client = 0) : client(_client) { }
  20. virtual void OnGamePadChanged(const GamePadEventData& evt) { if (client) client->OnGamePadChanged(evt); }
  21. virtual void OnHatSwitch(uint8_t hat) { if(client) client->OnHatSwitch(hat); }
  22. virtual void OnButtonUp(uint8_t but_id) { if(client) client->OnButtonUp(but_id); }
  23. virtual void OnButtonDown(uint8_t but_id) { if(client) client->OnButtonDown(but_id); }
  24. virtual void OnMouseMoved(uint8_t x, uint8_t y) { if (client) client->OnMouseMoved(x, y); }
  25. protected:
  26. JoystickEvents* client;
  27. };
  28. class JoystickEventsDeadZone : public JoystickEvents {
  29. public:
  30. JoystickEventsDeadZone(JoystickEvents* client = 0) : JoystickEvents(client) { }
  31. virtual void OnGamePadChanged(const GamePadEventData& evt);
  32. virtual void OnMouseMoved(uint8_t x, uint8_t y);
  33. private:
  34. const static GamePadEventData deadZone;
  35. const static uint8_t deadZoneMouseX, deadZoneMouseY;
  36. const static GamePadEventData centerValue;
  37. const static uint8_t centerMouseX, centerMouseY;
  38. };
  39. class JoystickEventsCPPM : public JoystickEvents {
  40. public:
  41. JoystickEventsCPPM(JoystickEvents* client = 0);
  42. virtual void OnGamePadChanged(const GamePadEventData& evt);
  43. uint8_t getInvert(uint8_t ch) {
  44. if (ch < CHANNELS_MAX) return invert[ch];
  45. else return 0;
  46. }
  47. void setInvert(uint8_t ch, uint8_t i) {
  48. if (ch < CHANNELS_MAX) invert[ch] = i;
  49. }
  50. uint16_t getMinimum(uint8_t ch) {
  51. if (ch < CHANNELS_MAX) return minimum[ch];
  52. else return 0;
  53. }
  54. void setMinimum(uint8_t ch, uint16_t i) {
  55. if (ch < CHANNELS_MAX) minimum[ch] = i;
  56. }
  57. uint16_t getMaximum(uint8_t ch) {
  58. if (ch < CHANNELS_MAX) return maximum[ch];
  59. else return 0;
  60. }
  61. void setMaximum(uint8_t ch, uint16_t i) {
  62. if (ch < CHANNELS_MAX) maximum[ch] = i;
  63. }
  64. int16_t getTrim(uint8_t ch) {
  65. if (ch < CHANNELS_MAX) return trim[ch];
  66. else return 0;
  67. }
  68. void setTrim(uint8_t ch, int16_t i) {
  69. if (ch < CHANNELS_MAX) trim[ch] = i;
  70. }
  71. private:
  72. uint16_t getJoystickAxis(const GamePadEventData& evt, uint8_t ch);
  73. uint16_t getJoystickMax(uint8_t ch);
  74. uint16_t values[CHANNELS_MAX];
  75. uint8_t invert[CHANNELS_MAX];
  76. uint16_t minimum[CHANNELS_MAX];
  77. uint16_t maximum[CHANNELS_MAX];
  78. int16_t trim[CHANNELS_MAX];
  79. };
  80. class JoystickEventsButtons : public JoystickEvents {
  81. public:
  82. JoystickEventsButtons(JoystickEvents* client = 0);
  83. virtual void OnButtonDown(uint8_t but_id);
  84. uint8_t getCurrentMode() { return currentMode; }
  85. private:
  86. enum MenuState {
  87. NONE = 0,
  88. MAINMENU,
  89. CPPMMENU,
  90. TRIMAXISMENU,
  91. TRIMENDPOINTMENU,
  92. INVERTAXISMENU,
  93. STATES_EDIT,
  94. EDIT_CHANNELS,
  95. EDIT_FRAME_LENGTH,
  96. EDIT_PULSE_LENGTH,
  97. EDIT_INVERT,
  98. EDIT_CPPM_PIN,
  99. EDIT_MIN_ROLL,
  100. EDIT_MAX_ROLL,
  101. EDIT_MIN_PITCH,
  102. EDIT_MAX_PITCH,
  103. EDIT_MIN_YAW,
  104. EDIT_MAX_YAW,
  105. EDIT_MIN_THROTTLE,
  106. EDIT_MAX_THROTTLE,
  107. EDIT_MIN_AUX1,
  108. EDIT_MAX_AUX1,
  109. EDIT_MIN_AUX2,
  110. EDIT_MAX_AUX2,
  111. EDIT_INVERT_ROLL,
  112. EDIT_INVERT_PITCH,
  113. EDIT_INVERT_YAW,
  114. EDIT_INVERT_THROTTLE,
  115. EDIT_INVERT_AUX1,
  116. EDIT_INVERT_AUX2,
  117. STATES_EDIT_SIGNED,
  118. EDIT_TRIM_ROLL,
  119. EDIT_TRIM_PITCH,
  120. EDIT_TRIM_YAW,
  121. EDIT_TRIM_THROTTLE,
  122. EDIT_TRIM_AUX1,
  123. EDIT_TRIM_AUX2,
  124. STATES_MAX
  125. };
  126. void printMenu();
  127. void menuHelper(uint8_t count, const char** menu, const char* title);
  128. void printValue(uint16_t min, uint16_t max, const char* title);
  129. void printSignedValue(int16_t min, int16_t max, const char* title);
  130. MenuState state;
  131. uint8_t index;
  132. uint16_t value;
  133. int16_t signedValue;
  134. uint8_t currentMode;
  135. };
  136. extern JoystickEventsCPPM joyCPPM;
  137. extern JoystickEventsButtons joyButtons;
  138. extern JoystickEventsDeadZone joyDeadZone;
  139. #endif // __JOYSTICK_EVENTS_H__