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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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), menuTime(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. "Status", "Trim Axis", "Trim Endpoint", "Invert Axis", "CPPM Config"
  66. };
  67. static const uint8_t mainMenuCount = sizeof(mainMenu) / sizeof(mainMenu[0]);
  68. static const char* cppmMenu[] = {
  69. "Channels", "Frame Length", "Pulse Length", "Invert", "Main Menu"
  70. };
  71. static const uint8_t cppmMenuCount = sizeof(cppmMenu) / sizeof(cppmMenu[0]);
  72. static const char* axisMenu[] = {
  73. "Roll", "Pitch", "Yaw", "Throttle", "Aux1", "Aux2", "Main Menu"
  74. };
  75. static const uint8_t axisMenuCount = sizeof(axisMenu) / sizeof(axisMenu[0]);
  76. static const char* endpointMenu[] = {
  77. "Roll Min", "Roll Max", "Pitch Min", "Pitch Max", "Yaw Min", "Yaw Max",
  78. "Throttle Min", "Throttle Max", "Aux1 Min", "Aux1 Max", "Aux2 Min",
  79. "Aux2 Max", "Main Menu"
  80. };
  81. static const uint8_t endpointMenuCount = sizeof(endpointMenu) / sizeof(endpointMenu[0]);
  82. if (state == MAINMENU) {
  83. menuHelper(mainMenuCount, mainMenu, "Main Menu");
  84. } else if (state == CPPMMENU) {
  85. menuHelper(cppmMenuCount, cppmMenu, "CPPM Config Menu");
  86. } else if (state == TRIMAXISMENU) {
  87. menuHelper(axisMenuCount, axisMenu, "Trim Axis Menu");
  88. } else if (state == TRIMENDPOINTMENU) {
  89. menuHelper(endpointMenuCount, endpointMenu, "Trim Endpoints");
  90. } else if (state == INVERTAXISMENU) {
  91. menuHelper(axisMenuCount, axisMenu, "Invert Axis Menu");
  92. } else if (state == EDIT_CHANNELS) {
  93. printValue(4, CHANNELS_MAX, mainMenu[0]);
  94. } else if (state == EDIT_FRAME_LENGTH) {
  95. printValue(10000, 30000, mainMenu[1]);
  96. } else if (state == EDIT_PULSE_LENGTH) {
  97. printValue(100, 1000, mainMenu[2]);
  98. } else if (state == EDIT_INVERT) {
  99. printValue(0, 1, mainMenu[3]);
  100. }
  101. menuTime = millis();
  102. }
  103. void JoystickEventsButtons::printValue(uint16_t min, uint16_t max, const char* title) {
  104. #ifdef DEBUG_OUTPUT
  105. Serial.print("printValue() state=");
  106. Serial.print(state);
  107. Serial.print(" index=");
  108. Serial.print(index);
  109. Serial.print(" min=");
  110. Serial.print(min);
  111. Serial.print(" max=");
  112. Serial.println(max);
  113. #endif
  114. if (value < min) {
  115. value = min;
  116. }
  117. if (value > max) {
  118. value = max;
  119. }
  120. x52->setMFDText(0, (String(title) + ":").c_str());
  121. x52->setMFDText(1, String(value).c_str());
  122. x52->setMFDText(2, "Press OK to save");
  123. }
  124. void JoystickEventsButtons::OnButtonDown(uint8_t but_id) {
  125. #ifdef DEBUG_BUTTON_MFD
  126. String text = "Button " + String(but_id) + " down";
  127. x52->setMFDText(1, text.c_str());
  128. #endif
  129. if (but_id == MODE_BUTTON_GREEN) {
  130. x52->setLEDBrightness(2);
  131. x52->setMFDBrightness(2);
  132. } else if (but_id == MODE_BUTTON_YELLOW) {
  133. x52->setLEDBrightness(1);
  134. x52->setMFDBrightness(1);
  135. } else if (but_id == MODE_BUTTON_RED) {
  136. x52->setLEDBrightness(0);
  137. x52->setMFDBrightness(0);
  138. } else if ((but_id == MENU_BUTTON_ENTER_1) || (but_id == MENU_BUTTON_ENTER_2)) {
  139. if (state == NONE) {
  140. state = MAINMENU;
  141. index = 0;
  142. } else if (state == MAINMENU) {
  143. if (index == 0) {
  144. state = NONE;
  145. } else if (index == 1) {
  146. //state = TRIMAXISMENU;
  147. //index = 0;
  148. } else if (index == 2) {
  149. //state = TRIMENDPOINTMENU;
  150. //index = 0;
  151. } else if (index == 3) {
  152. //state = INVERTAXISMENU;
  153. //index = 0;
  154. } else if (index == 4) {
  155. state = CPPMMENU;
  156. index = 0;
  157. }
  158. } else if (state == TRIMAXISMENU) {
  159. if (index == 0) {
  160. } else if (index == 1) {
  161. } else if (index == 2) {
  162. } else if (index == 3) {
  163. } else if (index == 4) {
  164. } else if (index == 5) {
  165. } else if (index == 6) {
  166. state = MAINMENU;
  167. index = 0;
  168. }
  169. } else if (state == TRIMENDPOINTMENU) {
  170. if (index == 0) {
  171. } else if (index == 1) {
  172. } else if (index == 2) {
  173. } else if (index == 3) {
  174. } else if (index == 4) {
  175. } else if (index == 5) {
  176. } else if (index == 6) {
  177. } else if (index == 7) {
  178. } else if (index == 8) {
  179. } else if (index == 9) {
  180. } else if (index == 10) {
  181. } else if (index == 11) {
  182. } else if (index == 12) {
  183. state = MAINMENU;
  184. index = 0;
  185. }
  186. } else if (state == INVERTAXISMENU) {
  187. if (index == 0) {
  188. } else if (index == 1) {
  189. } else if (index == 2) {
  190. } else if (index == 3) {
  191. } else if (index == 4) {
  192. } else if (index == 5) {
  193. } else if (index == 6) {
  194. state = MAINMENU;
  195. index = 0;
  196. }
  197. } else if (state == CPPMMENU) {
  198. if (index == 0) {
  199. state = EDIT_CHANNELS;
  200. value = CPPM::instance().getChannels();
  201. } else if (index == 1) {
  202. state = EDIT_FRAME_LENGTH;
  203. value = CPPM::instance().getFrameLength();
  204. } else if (index == 2) {
  205. state = EDIT_PULSE_LENGTH;
  206. value = CPPM::instance().getPulseLength();
  207. } else if (index == 3) {
  208. state = EDIT_INVERT;
  209. value = CPPM::instance().getInvert();
  210. } else if (index == 4) {
  211. state = MAINMENU;
  212. index = 0;
  213. }
  214. } else if (state == EDIT_CHANNELS) {
  215. CPPM::instance().setChannels(value);
  216. state = MAINMENU;
  217. } else if (state == EDIT_FRAME_LENGTH) {
  218. CPPM::instance().setFrameLength(value);
  219. state = MAINMENU;
  220. } else if (state == EDIT_PULSE_LENGTH) {
  221. CPPM::instance().setPulseLength(value);
  222. state = MAINMENU;
  223. } else if (state == EDIT_INVERT) {
  224. CPPM::instance().setInvert(value);
  225. state = MAINMENU;
  226. }
  227. printMenu();
  228. } else if (but_id == MENU_BUTTON_DOWN) {
  229. if (state > STATES_EDIT) {
  230. if (value > 0) {
  231. value--;
  232. }
  233. } else if (state != NONE) {
  234. index++;
  235. }
  236. printMenu();
  237. } else if (but_id == MENU_BUTTON_UP) {
  238. if (state > STATES_EDIT) {
  239. if (value < 0xFFFF) {
  240. value++;
  241. }
  242. } else if (state != NONE) {
  243. if (index > 0) {
  244. index--;
  245. }
  246. }
  247. printMenu();
  248. }
  249. if (client) {
  250. client->OnButtonDown(but_id);
  251. }
  252. }