Control drones with a proper joystick
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

events_buttons.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. #include "config.h"
  18. //#define DEBUG_OUTPUT Serial
  19. //#define DEBUG_BUTTON_MFD
  20. #define MENU_BUTTON_ENTER_1 29
  21. #define MENU_BUTTON_ENTER_2 26
  22. #define MENU_BUTTON_DOWN 27
  23. #define MENU_BUTTON_UP 28
  24. #define MODE_BUTTON_GREEN 23
  25. #define MODE_BUTTON_YELLOW 24
  26. #define MODE_BUTTON_RED 25
  27. #define MIN_FRAME_LENGTH 10000
  28. #define MAX_FRAME_LENGTH 30000
  29. #define MIN_PULSE_LENGTH 100
  30. #define MAX_PULSE_LENGTH 1000
  31. #define MIN_LOW_ENDPOINT 500
  32. #define MAX_LOW_ENDPOINT 1500
  33. #define MIN_HIGH_ENDPOINT 1500
  34. #define MAX_HIGH_ENDPOINT 2500
  35. #define MIN_TRIM -100
  36. #define MAX_TRIM 100
  37. void statusCallback(uint8_t a1, uint8_t a2, uint8_t q1, uint8_t q2);
  38. JoystickEventsButtons::JoystickEventsButtons(JoystickEvents* client)
  39. : JoystickEvents(client), state(NONE), index(0),
  40. value(0), signedValue(0), currentMode(0) { }
  41. void JoystickEventsButtons::printMenu() {
  42. static const char* mainMenu[] = {
  43. "Status", "Trim Axis", "Trim Endpoint", "Invert Axis", "CPPM Config",
  44. "Save EEPROM", "Load EEPROM"
  45. };
  46. static const uint8_t mainMenuCount = sizeof(mainMenu) / sizeof(mainMenu[0]);
  47. static const char* cppmMenu[] = {
  48. "Channels", "Frame Length", "Pulse Length", "Invert", "Main Menu"
  49. };
  50. static const uint8_t cppmMenuCount = sizeof(cppmMenu) / sizeof(cppmMenu[0]);
  51. static const char* axisMenu[] = {
  52. "Roll", "Pitch", "Yaw", "Throttle", "Aux1", "Aux2", "Main Menu"
  53. };
  54. static const uint8_t axisMenuCount = sizeof(axisMenu) / sizeof(axisMenu[0]);
  55. static const char* endpointMenu[] = {
  56. "Roll Min", "Roll Max", "Pitch Min", "Pitch Max", "Yaw Min", "Yaw Max",
  57. "Throttle Min", "Throttle Max", "Aux1 Min", "Aux1 Max", "Aux2 Min",
  58. "Aux2 Max", "Main Menu"
  59. };
  60. static const uint8_t endpointMenuCount = sizeof(endpointMenu) / sizeof(endpointMenu[0]);
  61. if (state == NONE) {
  62. statusCallback(0, 0, 0, 0);
  63. } else if (state == MAINMENU) {
  64. menuHelper(mainMenuCount, mainMenu, "Main Menu");
  65. } else if (state == CPPMMENU) {
  66. menuHelper(cppmMenuCount, cppmMenu, "CPPM Config Menu");
  67. } else if (state == TRIMAXISMENU) {
  68. menuHelper(axisMenuCount, axisMenu, "Trim Axis Menu");
  69. } else if (state == TRIMENDPOINTMENU) {
  70. menuHelper(endpointMenuCount, endpointMenu, "Trim Endpoints");
  71. } else if (state == INVERTAXISMENU) {
  72. menuHelper(axisMenuCount, axisMenu, "Invert Axis Menu");
  73. } else if (state == EDIT_CHANNELS) {
  74. printValue(4, CHANNELS_MAX, cppmMenu[0]);
  75. } else if (state == EDIT_FRAME_LENGTH) {
  76. printValue(MIN_FRAME_LENGTH, MAX_FRAME_LENGTH, cppmMenu[1]);
  77. } else if (state == EDIT_PULSE_LENGTH) {
  78. printValue(MIN_PULSE_LENGTH, MAX_PULSE_LENGTH, cppmMenu[2]);
  79. } else if (state == EDIT_INVERT) {
  80. printValue(0, 1, cppmMenu[3]);
  81. } else if ((state >= EDIT_INVERT_ROLL) && (state <= EDIT_INVERT_AUX2)) {
  82. uint8_t index = state - EDIT_INVERT_ROLL;
  83. printValue(0, 1, (String("Invert ") + axisMenu[index]).c_str());
  84. } else if ((state >= EDIT_MIN_ROLL) && (state <= EDIT_MAX_AUX2)) {
  85. uint8_t index = state - EDIT_MIN_ROLL;
  86. if (index & 0x01) {
  87. printValue(MIN_HIGH_ENDPOINT, MAX_HIGH_ENDPOINT, endpointMenu[index]);
  88. } else {
  89. printValue(MIN_LOW_ENDPOINT, MAX_LOW_ENDPOINT, endpointMenu[index]);
  90. }
  91. } else if ((state >= EDIT_TRIM_ROLL) && (state <= EDIT_TRIM_AUX2)) {
  92. uint8_t index = state - EDIT_TRIM_ROLL;
  93. printSignedValue(MIN_TRIM, MAX_TRIM, (String("Trim ") + axisMenu[index]).c_str());
  94. }
  95. }
  96. void JoystickEventsButtons::OnButtonDown(uint8_t but_id) {
  97. #ifdef DEBUG_BUTTON_MFD
  98. String text = "Button " + String(but_id) + " down";
  99. x52.setMFDText(1, text.c_str());
  100. #endif
  101. if (but_id == MODE_BUTTON_GREEN) {
  102. x52.setLEDBrightness(2);
  103. x52.setMFDBrightness(2);
  104. currentMode = 1;
  105. } else if (but_id == MODE_BUTTON_YELLOW) {
  106. x52.setLEDBrightness(1);
  107. x52.setMFDBrightness(1);
  108. currentMode = 2;
  109. } else if (but_id == MODE_BUTTON_RED) {
  110. x52.setLEDBrightness(0);
  111. x52.setMFDBrightness(0);
  112. currentMode = 3;
  113. } else if ((but_id == MENU_BUTTON_ENTER_1) || (but_id == MENU_BUTTON_ENTER_2)) {
  114. if (state == NONE) {
  115. state = MAINMENU;
  116. index = 0;
  117. } else if (state == MAINMENU) {
  118. if (index == 0) {
  119. state = NONE;
  120. } else if (index == 1) {
  121. state = TRIMAXISMENU;
  122. index = 0;
  123. } else if (index == 2) {
  124. state = TRIMENDPOINTMENU;
  125. index = 0;
  126. } else if (index == 3) {
  127. state = INVERTAXISMENU;
  128. index = 0;
  129. } else if (index == 4) {
  130. state = CPPMMENU;
  131. index = 0;
  132. } else if (index == 5) {
  133. eepromWrite();
  134. index = 0;
  135. } else if (index == 6) {
  136. eepromRead();
  137. index = 0;
  138. }
  139. } else if (state == TRIMAXISMENU) {
  140. if (index == 0) {
  141. state = EDIT_TRIM_ROLL;
  142. signedValue = joyCPPM.getTrim(CHANNEL_ROLL);
  143. } else if (index == 1) {
  144. state = EDIT_TRIM_PITCH;
  145. signedValue = joyCPPM.getTrim(CHANNEL_PITCH);
  146. } else if (index == 2) {
  147. state = EDIT_TRIM_YAW;
  148. signedValue = joyCPPM.getTrim(CHANNEL_YAW);
  149. } else if (index == 3) {
  150. state = EDIT_TRIM_THROTTLE;
  151. signedValue = joyCPPM.getTrim(CHANNEL_THROTTLE);
  152. } else if (index == 4) {
  153. state = EDIT_TRIM_AUX1;
  154. signedValue = joyCPPM.getTrim(CHANNEL_AUX1);
  155. } else if (index == 5) {
  156. state = EDIT_TRIM_AUX2;
  157. signedValue = joyCPPM.getTrim(CHANNEL_AUX2);
  158. } else if (index == 6) {
  159. state = MAINMENU;
  160. index = 0;
  161. }
  162. } else if (state == TRIMENDPOINTMENU) {
  163. if (index == 0) {
  164. state = EDIT_MIN_ROLL;
  165. value = joyCPPM.getMinimum(CHANNEL_ROLL);
  166. } else if (index == 1) {
  167. state = EDIT_MAX_ROLL;
  168. value = joyCPPM.getMaximum(CHANNEL_ROLL);
  169. } else if (index == 2) {
  170. state = EDIT_MIN_PITCH;
  171. value = joyCPPM.getMinimum(CHANNEL_PITCH);
  172. } else if (index == 3) {
  173. state = EDIT_MAX_PITCH;
  174. value = joyCPPM.getMaximum(CHANNEL_PITCH);
  175. } else if (index == 4) {
  176. state = EDIT_MIN_YAW;
  177. value = joyCPPM.getMinimum(CHANNEL_YAW);
  178. } else if (index == 5) {
  179. state = EDIT_MAX_YAW;
  180. value = joyCPPM.getMaximum(CHANNEL_YAW);
  181. } else if (index == 6) {
  182. state = EDIT_MIN_THROTTLE;
  183. value = joyCPPM.getMinimum(CHANNEL_THROTTLE);
  184. } else if (index == 7) {
  185. state = EDIT_MAX_THROTTLE;
  186. value = joyCPPM.getMaximum(CHANNEL_THROTTLE);
  187. } else if (index == 8) {
  188. state = EDIT_MIN_AUX1;
  189. value = joyCPPM.getMinimum(CHANNEL_AUX1);
  190. } else if (index == 9) {
  191. state = EDIT_MAX_AUX1;
  192. value = joyCPPM.getMaximum(CHANNEL_AUX1);
  193. } else if (index == 10) {
  194. state = EDIT_MIN_AUX2;
  195. value = joyCPPM.getMinimum(CHANNEL_AUX2);
  196. } else if (index == 11) {
  197. state = EDIT_MAX_AUX2;
  198. value = joyCPPM.getMaximum(CHANNEL_AUX2);
  199. } else if (index == 12) {
  200. state = EDIT_MIN_ROLL;
  201. state = MAINMENU;
  202. index = 0;
  203. }
  204. } else if (state == INVERTAXISMENU) {
  205. if (index == 0) {
  206. state = EDIT_INVERT_ROLL;
  207. value = joyCPPM.getInvert(CHANNEL_ROLL);
  208. } else if (index == 1) {
  209. state = EDIT_INVERT_PITCH;
  210. value = joyCPPM.getInvert(CHANNEL_PITCH);
  211. } else if (index == 2) {
  212. state = EDIT_INVERT_YAW;
  213. value = joyCPPM.getInvert(CHANNEL_YAW);
  214. } else if (index == 3) {
  215. state = EDIT_INVERT_THROTTLE;
  216. value = joyCPPM.getInvert(CHANNEL_THROTTLE);
  217. } else if (index == 4) {
  218. state = EDIT_INVERT_AUX1;
  219. value = joyCPPM.getInvert(CHANNEL_AUX1);
  220. } else if (index == 5) {
  221. state = EDIT_INVERT_AUX2;
  222. value = joyCPPM.getInvert(CHANNEL_AUX2);
  223. } else if (index == 6) {
  224. state = MAINMENU;
  225. index = 0;
  226. }
  227. } else if (state == CPPMMENU) {
  228. if (index == 0) {
  229. state = EDIT_CHANNELS;
  230. value = CPPM::instance().getChannels();
  231. } else if (index == 1) {
  232. state = EDIT_FRAME_LENGTH;
  233. value = CPPM::instance().getFrameLength();
  234. } else if (index == 2) {
  235. state = EDIT_PULSE_LENGTH;
  236. value = CPPM::instance().getPulseLength();
  237. } else if (index == 3) {
  238. state = EDIT_INVERT;
  239. value = CPPM::instance().getInvert();
  240. } else if (index == 4) {
  241. state = MAINMENU;
  242. index = 0;
  243. }
  244. } else if (state == EDIT_CHANNELS) {
  245. CPPM::instance().setChannels(value);
  246. state = CPPMMENU;
  247. } else if (state == EDIT_FRAME_LENGTH) {
  248. CPPM::instance().setFrameLength(value);
  249. state = CPPMMENU;
  250. } else if (state == EDIT_PULSE_LENGTH) {
  251. CPPM::instance().setPulseLength(value);
  252. state = CPPMMENU;
  253. } else if (state == EDIT_INVERT) {
  254. CPPM::instance().setInvert(value);
  255. state = CPPMMENU;
  256. } else if (state == EDIT_INVERT_ROLL) {
  257. joyCPPM.setInvert(CHANNEL_ROLL, value);
  258. state = INVERTAXISMENU;
  259. } else if (state == EDIT_INVERT_PITCH) {
  260. joyCPPM.setInvert(CHANNEL_PITCH, value);
  261. state = INVERTAXISMENU;
  262. } else if (state == EDIT_INVERT_YAW) {
  263. joyCPPM.setInvert(CHANNEL_YAW, value);
  264. state = INVERTAXISMENU;
  265. } else if (state == EDIT_INVERT_THROTTLE) {
  266. joyCPPM.setInvert(CHANNEL_THROTTLE, value);
  267. state = INVERTAXISMENU;
  268. } else if (state == EDIT_INVERT_AUX1) {
  269. joyCPPM.setInvert(CHANNEL_AUX1, value);
  270. state = INVERTAXISMENU;
  271. } else if (state == EDIT_INVERT_AUX2) {
  272. joyCPPM.setInvert(CHANNEL_AUX2, value);
  273. state = INVERTAXISMENU;
  274. } else if (state == EDIT_MIN_ROLL) {
  275. joyCPPM.setMinimum(CHANNEL_ROLL, value);
  276. state = TRIMENDPOINTMENU;
  277. } else if (state == EDIT_MAX_ROLL) {
  278. joyCPPM.setMaximum(CHANNEL_ROLL, value);
  279. state = TRIMENDPOINTMENU;
  280. } else if (state == EDIT_MIN_PITCH) {
  281. joyCPPM.setMinimum(CHANNEL_PITCH, value);
  282. state = TRIMENDPOINTMENU;
  283. } else if (state == EDIT_MAX_PITCH) {
  284. joyCPPM.setMaximum(CHANNEL_PITCH, value);
  285. state = TRIMENDPOINTMENU;
  286. } else if (state == EDIT_MIN_YAW) {
  287. joyCPPM.setMinimum(CHANNEL_YAW, value);
  288. state = TRIMENDPOINTMENU;
  289. } else if (state == EDIT_MAX_YAW) {
  290. joyCPPM.setMaximum(CHANNEL_YAW, value);
  291. state = TRIMENDPOINTMENU;
  292. } else if (state == EDIT_MIN_THROTTLE) {
  293. joyCPPM.setMinimum(CHANNEL_THROTTLE, value);
  294. state = TRIMENDPOINTMENU;
  295. } else if (state == EDIT_MAX_THROTTLE) {
  296. joyCPPM.setMaximum(CHANNEL_THROTTLE, value);
  297. state = TRIMENDPOINTMENU;
  298. } else if (state == EDIT_MIN_AUX1) {
  299. joyCPPM.setMinimum(CHANNEL_AUX1, value);
  300. state = TRIMENDPOINTMENU;
  301. } else if (state == EDIT_MAX_AUX1) {
  302. joyCPPM.setMaximum(CHANNEL_AUX1, value);
  303. state = TRIMENDPOINTMENU;
  304. } else if (state == EDIT_MIN_AUX2) {
  305. joyCPPM.setMinimum(CHANNEL_AUX2, value);
  306. state = TRIMENDPOINTMENU;
  307. } else if (state == EDIT_MAX_AUX2) {
  308. joyCPPM.setMaximum(CHANNEL_AUX2, value);
  309. state = TRIMENDPOINTMENU;
  310. } else if (state == EDIT_TRIM_ROLL) {
  311. joyCPPM.setTrim(CHANNEL_ROLL, signedValue);
  312. state = TRIMAXISMENU;
  313. } else if (state == EDIT_TRIM_PITCH) {
  314. joyCPPM.setTrim(CHANNEL_PITCH, signedValue);
  315. state = TRIMAXISMENU;
  316. } else if (state == EDIT_TRIM_YAW) {
  317. joyCPPM.setTrim(CHANNEL_YAW, signedValue);
  318. state = TRIMAXISMENU;
  319. } else if (state == EDIT_TRIM_THROTTLE) {
  320. joyCPPM.setTrim(CHANNEL_THROTTLE, signedValue);
  321. state = TRIMAXISMENU;
  322. } else if (state == EDIT_TRIM_AUX1) {
  323. joyCPPM.setTrim(CHANNEL_AUX1, signedValue);
  324. state = TRIMAXISMENU;
  325. } else if (state == EDIT_TRIM_AUX2) {
  326. joyCPPM.setTrim(CHANNEL_AUX2, signedValue);
  327. state = TRIMAXISMENU;
  328. }
  329. printMenu();
  330. } else if (but_id == MENU_BUTTON_DOWN) {
  331. if ((state > STATES_EDIT) && (state < STATES_EDIT_SIGNED)) {
  332. if (value > 0) {
  333. value--;
  334. }
  335. } else if (state > STATES_EDIT_SIGNED) {
  336. if (signedValue > -32767) {
  337. signedValue--;
  338. }
  339. } else if (state != NONE) {
  340. index++;
  341. }
  342. printMenu();
  343. } else if (but_id == MENU_BUTTON_UP) {
  344. if ((state > STATES_EDIT) && (state < STATES_EDIT_SIGNED)) {
  345. if (value < 0xFFFF) {
  346. value++;
  347. }
  348. } else if (state > STATES_EDIT_SIGNED) {
  349. if (signedValue < 32767) {
  350. signedValue++;
  351. }
  352. } else if (state != NONE) {
  353. if (index > 0) {
  354. index--;
  355. }
  356. }
  357. printMenu();
  358. }
  359. if (client) {
  360. client->OnButtonDown(but_id);
  361. }
  362. }
  363. void JoystickEventsButtons::menuHelper(uint8_t count, const char** menu, const char* title) {
  364. if (index >= count) {
  365. index = count - 1;
  366. }
  367. uint8_t start = 0, line = 0;
  368. if (index > 1) {
  369. start = index - 1;
  370. }
  371. uint8_t end = start + 2;
  372. if (index == 0) {
  373. x52.setMFDText(0, title);
  374. line = 1;
  375. end = start + 1;
  376. }
  377. if (end >= count) {
  378. end = count - 1;
  379. }
  380. for (uint8_t i = start; i <= end; i++) {
  381. String tmp = (index == i) ? "-> " : " ";
  382. x52.setMFDText(line++, (tmp + menu[i]).c_str());
  383. }
  384. if (line == 2) {
  385. x52.setMFDText(2);
  386. }
  387. #ifdef DEBUG_OUTPUT
  388. DEBUG_OUTPUT.print("menuHelper() state=");
  389. DEBUG_OUTPUT.print(state);
  390. DEBUG_OUTPUT.print(" index=");
  391. DEBUG_OUTPUT.print(index);
  392. DEBUG_OUTPUT.print(" start=");
  393. DEBUG_OUTPUT.print(start);
  394. DEBUG_OUTPUT.print(" end=");
  395. DEBUG_OUTPUT.println(end);
  396. #endif
  397. }
  398. void JoystickEventsButtons::printValue(uint16_t min, uint16_t max, const char* title) {
  399. #ifdef DEBUG_OUTPUT
  400. DEBUG_OUTPUT.print("printValue() state=");
  401. DEBUG_OUTPUT.print(state);
  402. DEBUG_OUTPUT.print(" index=");
  403. DEBUG_OUTPUT.print(index);
  404. DEBUG_OUTPUT.print(" min=");
  405. DEBUG_OUTPUT.print(min);
  406. DEBUG_OUTPUT.print(" max=");
  407. DEBUG_OUTPUT.println(max);
  408. #endif
  409. if (value < min) {
  410. value = min;
  411. }
  412. if (value > max) {
  413. value = max;
  414. }
  415. x52.setMFDText(0, (String(title) + ":").c_str());
  416. x52.setMFDText(1, String(value).c_str());
  417. x52.setMFDText(2, "Press OK to save");
  418. }
  419. void JoystickEventsButtons::printSignedValue(int16_t min, int16_t max, const char* title) {
  420. #ifdef DEBUG_OUTPUT
  421. DEBUG_OUTPUT.print("printSignedValue() state=");
  422. DEBUG_OUTPUT.print(state);
  423. DEBUG_OUTPUT.print(" index=");
  424. DEBUG_OUTPUT.print(index);
  425. DEBUG_OUTPUT.print(" min=");
  426. DEBUG_OUTPUT.print(min);
  427. DEBUG_OUTPUT.print(" max=");
  428. DEBUG_OUTPUT.println(max);
  429. #endif
  430. if (signedValue < min) {
  431. signedValue = min;
  432. }
  433. if (signedValue > max) {
  434. signedValue = max;
  435. }
  436. x52.setMFDText(0, (String(title) + ":").c_str());
  437. x52.setMFDText(1, String(signedValue).c_str());
  438. x52.setMFDText(2, "Press OK to save");
  439. }