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_buttons.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "x52.h"
  15. #include "cppm.h"
  16. #include "events.h"
  17. //#define DEBUG_OUTPUT
  18. //#define DEBUG_BUTTON_MFD
  19. #define MENU_BUTTON_ENTER_1 29
  20. #define MENU_BUTTON_ENTER_2 26
  21. #define MENU_BUTTON_DOWN 27
  22. #define MENU_BUTTON_UP 28
  23. #define MODE_BUTTON_GREEN 23
  24. #define MODE_BUTTON_YELLOW 24
  25. #define MODE_BUTTON_RED 25
  26. JoystickEventsButtons::JoystickEventsButtons(X52* x, JoystickEvents* client)
  27. : JoystickEvents(client), x52(x), state(NONE), index(0), value(0) { }
  28. void JoystickEventsButtons::menuHelper(uint8_t count, const char** menu, const char* title) {
  29. if (index >= count) {
  30. index = count - 1;
  31. }
  32. uint8_t start = 0, line = 0;
  33. if (index > 1) {
  34. start = index - 1;
  35. }
  36. uint8_t end = start + 2;
  37. if (index == 0) {
  38. x52->setMFDText(0, title);
  39. line = 1;
  40. end = start + 1;
  41. }
  42. if (end >= count) {
  43. end = count - 1;
  44. }
  45. for (uint8_t i = start; i <= end; i++) {
  46. String tmp = (index == i) ? "-> " : " ";
  47. x52->setMFDText(line++, (tmp + menu[i]).c_str());
  48. }
  49. if (line == 2) {
  50. x52->setMFDText(2);
  51. }
  52. #ifdef DEBUG_OUTPUT
  53. Serial.print("menuHelper() state=");
  54. Serial.print(state);
  55. Serial.print(" index=");
  56. Serial.print(index);
  57. Serial.print(" start=");
  58. Serial.print(start);
  59. Serial.print(" end=");
  60. Serial.println(end);
  61. #endif
  62. }
  63. void JoystickEventsButtons::printMenu() {
  64. static const char* mainMenu[] = {
  65. "Channels", "Frame Length",
  66. "Pulse Length", "Invert"
  67. };
  68. static const uint8_t mainMenuCount = sizeof(mainMenu) / sizeof(mainMenu[0]);
  69. if (state == MAINMENU) {
  70. menuHelper(mainMenuCount, mainMenu, "Main Menu");
  71. } else if (state == EDIT_CHANNELS) {
  72. printValue(4, CHANNELS_MAX, mainMenu[0]);
  73. } else if (state == EDIT_FRAME_LENGTH) {
  74. printValue(10000, 30000, mainMenu[1]);
  75. } else if (state == EDIT_PULSE_LENGTH) {
  76. printValue(100, 1000, mainMenu[2]);
  77. } else if (state == EDIT_INVERT) {
  78. printValue(0, 1, mainMenu[3]);
  79. }
  80. }
  81. void JoystickEventsButtons::printValue(uint16_t min, uint16_t max, const char* title) {
  82. #ifdef DEBUG_OUTPUT
  83. Serial.print("printValue() state=");
  84. Serial.print(state);
  85. Serial.print(" index=");
  86. Serial.print(index);
  87. Serial.print(" min=");
  88. Serial.print(min);
  89. Serial.print(" max=");
  90. Serial.println(max);
  91. #endif
  92. if (value < min) {
  93. value = min;
  94. }
  95. if (value > max) {
  96. value = max;
  97. }
  98. x52->setMFDText(0, (String(title) + ":").c_str());
  99. x52->setMFDText(1, String(value).c_str());
  100. x52->setMFDText(2, "Press OK to save");
  101. }
  102. void JoystickEventsButtons::OnButtonDown(uint8_t but_id) {
  103. #ifdef DEBUG_BUTTON_MFD
  104. String text = "Button " + String(but_id) + " down";
  105. x52->setMFDText(1, text.c_str());
  106. #endif
  107. if (but_id == MODE_BUTTON_GREEN) {
  108. x52->setLEDBrightness(2);
  109. x52->setMFDBrightness(2);
  110. } else if (but_id == MODE_BUTTON_YELLOW) {
  111. x52->setLEDBrightness(1);
  112. x52->setMFDBrightness(1);
  113. } else if (but_id == MODE_BUTTON_RED) {
  114. x52->setLEDBrightness(0);
  115. x52->setMFDBrightness(0);
  116. } else if ((but_id == MENU_BUTTON_ENTER_1) || (but_id == MENU_BUTTON_ENTER_2)) {
  117. if (state == NONE) {
  118. state = MAINMENU;
  119. } else if (state == MAINMENU) {
  120. if (index == 0) {
  121. state = EDIT_CHANNELS;
  122. value = CPPM::instance().getChannels();
  123. } else if (index == 1) {
  124. state = EDIT_FRAME_LENGTH;
  125. value = CPPM::instance().getFrameLength();
  126. } else if (index == 2) {
  127. state = EDIT_PULSE_LENGTH;
  128. value = CPPM::instance().getPulseLength();
  129. } else if (index == 3) {
  130. state = EDIT_INVERT;
  131. value = CPPM::instance().getInvert();
  132. }
  133. } else if (state == EDIT_CHANNELS) {
  134. CPPM::instance().setChannels(value);
  135. state = MAINMENU;
  136. } else if (state == EDIT_FRAME_LENGTH) {
  137. CPPM::instance().setFrameLength(value);
  138. state = MAINMENU;
  139. } else if (state == EDIT_PULSE_LENGTH) {
  140. CPPM::instance().setPulseLength(value);
  141. state = MAINMENU;
  142. } else if (state == EDIT_INVERT) {
  143. CPPM::instance().setInvert(value);
  144. state = MAINMENU;
  145. }
  146. printMenu();
  147. } else if (but_id == MENU_BUTTON_DOWN) {
  148. if (state > STATES_EDIT) {
  149. if (value > 0) {
  150. value--;
  151. }
  152. } else if (state != NONE) {
  153. index++;
  154. }
  155. printMenu();
  156. } else if (but_id == MENU_BUTTON_UP) {
  157. if (state > STATES_EDIT) {
  158. if (value < 0xFFFF) {
  159. value++;
  160. }
  161. } else if (state != NONE) {
  162. if (index > 0) {
  163. index--;
  164. }
  165. }
  166. printMenu();
  167. }
  168. if (client) {
  169. client->OnButtonDown(but_id);
  170. }
  171. }