Control drones with a proper joystick
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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