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 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. class JoystickEventsCPPM : public JoystickEvents {
  40. public:
  41. JoystickEventsCPPM(JoystickEvents* client = 0);
  42. virtual void OnGamePadChanged(const GamePadEventData& evt);
  43. private:
  44. const static uint8_t channels = 12;
  45. uint16_t values[channels];
  46. };
  47. class JoystickEventsButtons : public JoystickEvents {
  48. public:
  49. JoystickEventsButtons(X52* x = 0, JoystickEvents* client = 0);
  50. virtual void OnButtonDown(uint8_t but_id);
  51. private:
  52. enum MenuState {
  53. NONE = 0,
  54. MAINMENU,
  55. STATES_EDIT,
  56. EDIT_CHANNELS,
  57. EDIT_FRAME_LENGTH,
  58. EDIT_PULSE_LENGTH,
  59. EDIT_INVERT,
  60. STATES_MAX
  61. };
  62. void printMenu();
  63. void menuHelper(uint8_t count, const char** menu, const char* title);
  64. void printValue(uint16_t min, uint16_t max, const char* title);
  65. X52* x52;
  66. MenuState state;
  67. uint8_t index;
  68. uint16_t value;
  69. };
  70. #endif // __JOYSTICK_EVENTS_H__