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.

usb_flightsim.cpp 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_dev.h"
  31. #include "usb_flightsim.h"
  32. #include "core_pins.h" // for yield(), millis()
  33. #include <string.h> // for memcpy()
  34. #ifdef FLIGHTSIM_INTERFACE // defined by usb_dev.h -> usb_desc.h
  35. #if F_CPU >= 20000000
  36. FlightSimCommand * FlightSimCommand::first = NULL;
  37. FlightSimCommand * FlightSimCommand::last = NULL;
  38. FlightSimInteger * FlightSimInteger::first = NULL;
  39. FlightSimInteger * FlightSimInteger::last = NULL;
  40. FlightSimFloat * FlightSimFloat::first = NULL;
  41. FlightSimFloat * FlightSimFloat::last = NULL;
  42. uint8_t FlightSimClass::enabled = 0;
  43. uint8_t FlightSimClass::request_id_messages = 0;
  44. unsigned long FlightSimClass::frameCount = 0;
  45. elapsedMillis FlightSimClass::enableTimeout;
  46. static unsigned int unassigned_id = 1; // TODO: move into FlightSimClass
  47. FlightSimCommand::FlightSimCommand()
  48. {
  49. id = unassigned_id++;
  50. if (!first) {
  51. first = this;
  52. } else {
  53. last->next = this;
  54. }
  55. last = this;
  56. name = NULL;
  57. next = NULL;
  58. FlightSimClass::request_id_messages = 1;
  59. }
  60. void FlightSimCommand::identify(void)
  61. {
  62. uint8_t len, buf[6];
  63. if (!FlightSim.enabled || !name) return;
  64. len = strlen((const char *)name);
  65. buf[0] = len + 6;
  66. buf[1] = 1;
  67. buf[2] = id;
  68. buf[3] = id >> 8;
  69. buf[4] = 0;
  70. buf[5] = 0;
  71. FlightSimClass::xmit(buf, 6, name, len);
  72. }
  73. void FlightSimCommand::sendcmd(uint8_t n)
  74. {
  75. uint8_t buf[4];
  76. if (!FlightSim.enabled || !name) return;
  77. buf[0] = 4;
  78. buf[1] = n;
  79. buf[2] = id;
  80. buf[3] = id >> 8;
  81. FlightSimClass::xmit(buf, 4, NULL, 0);
  82. }
  83. FlightSimInteger::FlightSimInteger()
  84. {
  85. id = unassigned_id++;
  86. if (!first) {
  87. first = this;
  88. } else {
  89. last->next = this;
  90. }
  91. last = this;
  92. name = NULL;
  93. next = NULL;
  94. value = 0;
  95. change_callback = NULL;
  96. FlightSimClass::request_id_messages = 1;
  97. }
  98. void FlightSimInteger::identify(void)
  99. {
  100. uint8_t len, buf[6];
  101. if (!FlightSim.enabled || !name) return;
  102. len = strlen((const char *)name);
  103. buf[0] = len + 6;
  104. buf[1] = 1;
  105. buf[2] = id;
  106. buf[3] = id >> 8;
  107. buf[4] = 1;
  108. buf[5] = 0;
  109. FlightSimClass::xmit(buf, 6, name, len);
  110. }
  111. void FlightSimInteger::write(long val)
  112. {
  113. uint8_t buf[6];
  114. value = val;
  115. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  116. buf[0] = 10;
  117. buf[1] = 2;
  118. buf[2] = id;
  119. buf[3] = id >> 8;
  120. buf[4] = 1;
  121. buf[5] = 0;
  122. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  123. }
  124. void FlightSimInteger::update(long val)
  125. {
  126. value = val;
  127. if (change_callback) (*change_callback)(val);
  128. }
  129. FlightSimInteger * FlightSimInteger::find(unsigned int n)
  130. {
  131. for (FlightSimInteger *p = first; p; p = p->next) {
  132. if (p->id == n) return p;
  133. }
  134. return NULL;
  135. }
  136. FlightSimFloat::FlightSimFloat()
  137. {
  138. id = unassigned_id++;
  139. if (!first) {
  140. first = this;
  141. } else {
  142. last->next = this;
  143. }
  144. last = this;
  145. name = NULL;
  146. next = NULL;
  147. value = 0;
  148. change_callback = NULL;
  149. FlightSimClass::request_id_messages = 1;
  150. }
  151. void FlightSimFloat::identify(void)
  152. {
  153. uint8_t len, buf[6];
  154. if (!FlightSim.enabled || !name) return;
  155. len = strlen((const char *)name);
  156. buf[0] = len + 6;
  157. buf[1] = 1;
  158. buf[2] = id;
  159. buf[3] = id >> 8;
  160. buf[4] = 2;
  161. buf[5] = 0;
  162. FlightSimClass::xmit(buf, 6, name, len);
  163. }
  164. void FlightSimFloat::write(float val)
  165. {
  166. uint8_t buf[6];
  167. value = val;
  168. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  169. buf[0] = 10;
  170. buf[1] = 2;
  171. buf[2] = id;
  172. buf[3] = id >> 8;
  173. buf[4] = 2;
  174. buf[5] = 0;
  175. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  176. }
  177. void FlightSimFloat::update(float val)
  178. {
  179. value = val;
  180. if (change_callback) (*change_callback)(val);
  181. }
  182. FlightSimFloat * FlightSimFloat::find(unsigned int n)
  183. {
  184. for (FlightSimFloat *p = first; p; p = p->next) {
  185. if (p->id == n) return p;
  186. }
  187. return NULL;
  188. }
  189. FlightSimClass::FlightSimClass()
  190. {
  191. }
  192. void FlightSimClass::update(void)
  193. {
  194. uint8_t len, maxlen, type, *p, *end;
  195. union {
  196. uint8_t b[4];
  197. long l;
  198. float f;
  199. } data;
  200. usb_packet_t *rx_packet;
  201. uint16_t id;
  202. while (1) {
  203. if (!usb_configuration) break;
  204. rx_packet = usb_rx(FLIGHTSIM_RX_ENDPOINT);
  205. if (!rx_packet) break;
  206. p = rx_packet->buf;
  207. end = p + 64;
  208. maxlen = 64;
  209. do {
  210. len = p[0];
  211. if (len < 2 || len > maxlen) break;
  212. switch (p[1]) {
  213. case 0x02: // write data
  214. if (len < 10) break;
  215. id = p[2] | (p[3] << 8);
  216. type = p[4];
  217. if (type == 1) {
  218. FlightSimInteger *item = FlightSimInteger::find(id);
  219. if (!item) break;
  220. #ifdef KINETISK
  221. data.l = *(long *)(p + 6);
  222. #else
  223. data.b[0] = p[6];
  224. data.b[1] = p[7];
  225. data.b[2] = p[8];
  226. data.b[3] = p[9];
  227. #endif
  228. item->update(data.l);
  229. } else if (type == 2) {
  230. FlightSimFloat *item = FlightSimFloat::find(id);
  231. if (!item) break;
  232. #ifdef KINETISK
  233. data.f = *(float *)(p + 6);
  234. #else
  235. data.b[0] = p[6];
  236. data.b[1] = p[7];
  237. data.b[2] = p[8];
  238. data.b[3] = p[9];
  239. #endif
  240. item->update(data.f);
  241. }
  242. break;
  243. case 0x03: // enable/disable
  244. if (len < 4) break;
  245. switch (p[2]) {
  246. case 1:
  247. request_id_messages = 1;
  248. case 2:
  249. enable();
  250. frameCount++;
  251. break;
  252. case 3:
  253. disable();
  254. }
  255. }
  256. p += len;
  257. maxlen -= len;
  258. } while (p < end);
  259. usb_free(rx_packet);
  260. }
  261. if (enabled && request_id_messages) {
  262. request_id_messages = 0;
  263. for (FlightSimCommand *p = FlightSimCommand::first; p; p = p->next) {
  264. p->identify();
  265. }
  266. for (FlightSimInteger *p = FlightSimInteger::first; p; p = p->next) {
  267. p->identify();
  268. // TODO: send any dirty data
  269. }
  270. for (FlightSimFloat *p = FlightSimFloat::first; p; p = p->next) {
  271. p->identify();
  272. // TODO: send any dirty data
  273. }
  274. }
  275. }
  276. bool FlightSimClass::isEnabled(void)
  277. {
  278. if (!usb_configuration) return false;
  279. if (!enabled) return false;
  280. if (enableTimeout > 1500) return false;
  281. return true;
  282. }
  283. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  284. #define TX_PACKET_LIMIT 8
  285. static usb_packet_t *tx_packet=NULL;
  286. static volatile uint8_t tx_noautoflush=0;
  287. void FlightSimClass::xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  288. {
  289. uint8_t total;
  290. total = n1 + n2;
  291. if (total > FLIGHTSIM_TX_SIZE) return;
  292. if (!enabled || !usb_configuration) return;
  293. tx_noautoflush = 1;
  294. if (tx_packet) {
  295. if (total <= FLIGHTSIM_TX_SIZE - tx_packet->index) goto send;
  296. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  297. tx_packet->buf[i] = 0;
  298. }
  299. tx_packet->len = FLIGHTSIM_TX_SIZE;
  300. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  301. tx_packet = NULL;
  302. }
  303. while (1) {
  304. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  305. tx_packet = usb_malloc();
  306. if (tx_packet) break;
  307. }
  308. if (!enabled || !usb_configuration) {
  309. tx_noautoflush = 0;
  310. return;
  311. }
  312. tx_noautoflush = 0;
  313. yield();
  314. tx_noautoflush = 1;
  315. }
  316. send:
  317. memcpy(tx_packet->buf + tx_packet->index, p1, n1);
  318. tx_packet->index += n1;
  319. if (n2 > 0) {
  320. memcpy(tx_packet->buf + tx_packet->index, p2, n2);
  321. tx_packet->index += n2;
  322. }
  323. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  324. tx_packet->len = FLIGHTSIM_TX_SIZE;
  325. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  326. tx_packet = NULL;
  327. }
  328. tx_noautoflush = 0;
  329. }
  330. extern "C" {
  331. void usb_flightsim_flush_callback(void)
  332. {
  333. if (tx_noautoflush || !tx_packet || tx_packet->index == 0) return;
  334. for (int i=tx_packet->index; i < FLIGHTSIM_TX_ENDPOINT; i++) {
  335. tx_packet->buf[i] = 0;
  336. }
  337. tx_packet->len = FLIGHTSIM_TX_SIZE;
  338. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  339. tx_packet = NULL;
  340. }
  341. }
  342. #endif // F_CPU
  343. #endif // FLIGHTSIM_INTERFACE