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.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. class GamePadEventData;
  16. class X52;
  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. #define CHANNEL_ROLL 0
  40. #define CHANNEL_PITCH 1
  41. #define CHANNEL_THROTTLE 2
  42. #define CHANNEL_YAW 3
  43. #define CHANNEL_AUX1 4
  44. #define CHANNEL_AUX2 5
  45. class JoystickEventsCPPM : public JoystickEvents {
  46. public:
  47. JoystickEventsCPPM(JoystickEvents* client = 0);
  48. virtual void OnGamePadChanged(const GamePadEventData& evt);
  49. uint8_t getInvert(uint8_t ch) {
  50. if (ch < channels) return invert[ch];
  51. else return 0;
  52. }
  53. void setInvert(uint8_t ch, uint8_t i) {
  54. if (ch < channels) invert[ch] = i;
  55. }
  56. uint16_t getMinimum(uint8_t ch) {
  57. if (ch < channels) return minimum[ch];
  58. else return 0;
  59. }
  60. void setMinimum(uint8_t ch, uint16_t i) {
  61. if (ch < channels) minimum[ch] = i;
  62. }
  63. uint16_t getMaximum(uint8_t ch) {
  64. if (ch < channels) return maximum[ch];
  65. else return 0;
  66. }
  67. void setMaximum(uint8_t ch, uint16_t i) {
  68. if (ch < channels) maximum[ch] = i;
  69. }
  70. int16_t getTrim(uint8_t ch) {
  71. if (ch < channels) return trim[ch];
  72. else return 0;
  73. }
  74. void setTrim(uint8_t ch, int16_t i) {
  75. if (ch < channels) trim[ch] = i;
  76. }
  77. private:
  78. uint16_t getJoystickAxis(const GamePadEventData& evt, uint8_t ch);
  79. uint16_t getJoystickMax(uint8_t ch);
  80. const static uint8_t channels = 12;
  81. uint16_t values[channels];
  82. uint8_t invert[channels];
  83. uint16_t minimum[channels];
  84. uint16_t maximum[channels];
  85. int16_t trim[channels];
  86. };
  87. class JoystickEventsButtons : public JoystickEvents {
  88. public:
  89. JoystickEventsButtons(X52* x = 0, JoystickEvents* client = 0);
  90. virtual void OnButtonDown(uint8_t but_id);
  91. uint8_t getCurrentMode() { return currentMode; }
  92. private:
  93. enum MenuState {
  94. NONE = 0,
  95. MAINMENU,
  96. CPPMMENU,
  97. TRIMAXISMENU,
  98. TRIMENDPOINTMENU,
  99. INVERTAXISMENU,
  100. STATES_EDIT,
  101. EDIT_CHANNELS,
  102. EDIT_FRAME_LENGTH,
  103. EDIT_PULSE_LENGTH,
  104. EDIT_INVERT,
  105. EDIT_MIN_ROLL,
  106. EDIT_MAX_ROLL,
  107. EDIT_MIN_PITCH,
  108. EDIT_MAX_PITCH,
  109. EDIT_MIN_YAW,
  110. EDIT_MAX_YAW,
  111. EDIT_MIN_THROTTLE,
  112. EDIT_MAX_THROTTLE,
  113. EDIT_MIN_AUX1,
  114. EDIT_MAX_AUX1,
  115. EDIT_MIN_AUX2,
  116. EDIT_MAX_AUX2,
  117. EDIT_INVERT_ROLL,
  118. EDIT_INVERT_PITCH,
  119. EDIT_INVERT_YAW,
  120. EDIT_INVERT_THROTTLE,
  121. EDIT_INVERT_AUX1,
  122. EDIT_INVERT_AUX2,
  123. STATES_EDIT_SIGNED,
  124. EDIT_TRIM_ROLL,
  125. EDIT_TRIM_PITCH,
  126. EDIT_TRIM_YAW,
  127. EDIT_TRIM_THROTTLE,
  128. EDIT_TRIM_AUX1,
  129. EDIT_TRIM_AUX2,
  130. STATES_MAX
  131. };
  132. void printMenu();
  133. void menuHelper(uint8_t count, const char** menu, const char* title);
  134. void printValue(uint16_t min, uint16_t max, const char* title);
  135. void printSignedValue(int16_t min, int16_t max, const char* title);
  136. X52* x52;
  137. MenuState state;
  138. uint8_t index;
  139. uint16_t value;
  140. int16_t signedValue;
  141. uint8_t currentMode;
  142. };
  143. extern JoystickEventsCPPM joyCPPM;
  144. extern JoystickEventsButtons joyButtons;
  145. extern JoystickEventsDeadZone joyDeadZone;
  146. #endif // __JOYSTICK_EVENTS_H__