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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. CPPMMENU,
  56. TRIMAXISMENU,
  57. TRIMENDPOINTMENU,
  58. INVERTAXISMENU,
  59. STATES_EDIT,
  60. EDIT_CHANNELS,
  61. EDIT_FRAME_LENGTH,
  62. EDIT_PULSE_LENGTH,
  63. EDIT_INVERT,
  64. STATES_MAX
  65. };
  66. void printMenu();
  67. void menuHelper(uint8_t count, const char** menu, const char* title);
  68. void printValue(uint16_t min, uint16_t max, const char* title);
  69. X52* x52;
  70. MenuState state;
  71. uint8_t index;
  72. uint16_t value;
  73. unsigned long menuTime;
  74. };
  75. #endif // __JOYSTICK_EVENTS_H__