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_cppm.cpp 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <Arduino.h>
  13. #include "data.h"
  14. #include "cppm.h"
  15. #include "events.h"
  16. #define CHANNEL_THROTTLE 2
  17. #define CHANNEL_PITCH 1
  18. #define CHANNEL_ROLL 0
  19. #define CHANNEL_YAW 3
  20. #define CHANNEL_AUX1 4
  21. #define CHANNEL_AUX2 5
  22. JoystickEventsCPPM::JoystickEventsCPPM(JoystickEvents* client) : JoystickEvents(client) {
  23. for (uint8_t i = 0; i < channels; i++) {
  24. values[i] = 1500;
  25. }
  26. values[CHANNEL_AUX1] = 1000;
  27. values[CHANNEL_AUX2] = 1000;
  28. cppmCopy(values);
  29. }
  30. void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) {
  31. values[CHANNEL_THROTTLE] = map(evt.Z, 0, 0xFF, 2000, 1000);
  32. values[CHANNEL_PITCH] = map(evt.Y, 0, 0x7FF, 1000, 2000);
  33. values[CHANNEL_ROLL] = map(evt.X, 0, 0x7FF, 1000, 2000);
  34. values[CHANNEL_YAW] = map(evt.Rz, 0, 0x3FF, 1000, 2000);
  35. values[CHANNEL_AUX1] = map(evt.Ry, 0, 0xFF, 1000, 2000);
  36. values[CHANNEL_AUX2] = map(evt.Slider, 0, 0xFF, 1000, 2000);
  37. cppmCopy(values);
  38. if (client) {
  39. client->OnGamePadChanged(evt);
  40. }
  41. }
  42. void JoystickEventsCPPM::OnHatSwitch(uint8_t hat) {
  43. if (client) {
  44. client->OnHatSwitch(hat);
  45. }
  46. }
  47. void JoystickEventsCPPM::OnButtonUp(uint8_t but_id) {
  48. if (client) {
  49. client->OnButtonUp(but_id);
  50. }
  51. }
  52. void JoystickEventsCPPM::OnButtonDown(uint8_t but_id) {
  53. if (client) {
  54. client->OnButtonDown(but_id);
  55. }
  56. }
  57. void JoystickEventsCPPM::OnMouseMoved(uint8_t x, uint8_t y) {
  58. if (client) {
  59. client->OnMouseMoved(x, y);
  60. }
  61. }