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.6KB

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