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.

368 satır
8.2KB

  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. FlightSimCommand * FlightSimCommand::first = NULL;
  36. FlightSimCommand * FlightSimCommand::last = NULL;
  37. FlightSimInteger * FlightSimInteger::first = NULL;
  38. FlightSimInteger * FlightSimInteger::last = NULL;
  39. FlightSimFloat * FlightSimFloat::first = NULL;
  40. FlightSimFloat * FlightSimFloat::last = NULL;
  41. uint8_t FlightSimClass::enabled = 0;
  42. uint8_t FlightSimClass::request_id_messages = 0;
  43. unsigned long FlightSimClass::frameCount = 0;
  44. elapsedMillis FlightSimClass::enableTimeout;
  45. static unsigned int unassigned_id = 1; // TODO: move into FlightSimClass
  46. FlightSimCommand::FlightSimCommand()
  47. {
  48. id = unassigned_id++;
  49. if (!first) {
  50. first = this;
  51. } else {
  52. last->next = this;
  53. }
  54. last = this;
  55. name = NULL;
  56. next = NULL;
  57. FlightSimClass::request_id_messages = 1;
  58. }
  59. void FlightSimCommand::identify(void)
  60. {
  61. uint8_t len, buf[6];
  62. if (!FlightSim.enabled || !name) return;
  63. len = strlen((const char *)name);
  64. buf[0] = len + 6;
  65. buf[1] = 1;
  66. buf[2] = id;
  67. buf[3] = id >> 8;
  68. buf[4] = 0;
  69. buf[5] = 0;
  70. FlightSimClass::xmit(buf, 6, name, len);
  71. }
  72. void FlightSimCommand::sendcmd(uint8_t n)
  73. {
  74. uint8_t buf[4];
  75. if (!FlightSim.enabled || !name) return;
  76. buf[0] = 4;
  77. buf[1] = n;
  78. buf[2] = id;
  79. buf[3] = id >> 8;
  80. FlightSimClass::xmit(buf, 4, NULL, 0);
  81. }
  82. FlightSimInteger::FlightSimInteger()
  83. {
  84. id = unassigned_id++;
  85. if (!first) {
  86. first = this;
  87. } else {
  88. last->next = this;
  89. }
  90. last = this;
  91. name = NULL;
  92. next = NULL;
  93. value = 0;
  94. change_callback = NULL;
  95. FlightSimClass::request_id_messages = 1;
  96. }
  97. void FlightSimInteger::identify(void)
  98. {
  99. uint8_t len, buf[6];
  100. if (!FlightSim.enabled || !name) return;
  101. len = strlen((const char *)name);
  102. buf[0] = len + 6;
  103. buf[1] = 1;
  104. buf[2] = id;
  105. buf[3] = id >> 8;
  106. buf[4] = 1;
  107. buf[5] = 0;
  108. FlightSimClass::xmit(buf, 6, name, len);
  109. }
  110. void FlightSimInteger::write(long val)
  111. {
  112. uint8_t buf[6];
  113. value = val;
  114. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  115. buf[0] = 10;
  116. buf[1] = 2;
  117. buf[2] = id;
  118. buf[3] = id >> 8;
  119. buf[4] = 1;
  120. buf[5] = 0;
  121. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  122. }
  123. void FlightSimInteger::update(long val)
  124. {
  125. value = val;
  126. if (change_callback) (*change_callback)(val);
  127. }
  128. FlightSimInteger * FlightSimInteger::find(unsigned int n)
  129. {
  130. for (FlightSimInteger *p = first; p; p = p->next) {
  131. if (p->id == n) return p;
  132. }
  133. return NULL;
  134. }
  135. FlightSimFloat::FlightSimFloat()
  136. {
  137. id = unassigned_id++;
  138. if (!first) {
  139. first = this;
  140. } else {
  141. last->next = this;
  142. }
  143. last = this;
  144. name = NULL;
  145. next = NULL;
  146. value = 0;
  147. change_callback = NULL;
  148. FlightSimClass::request_id_messages = 1;
  149. }
  150. void FlightSimFloat::identify(void)
  151. {
  152. uint8_t len, buf[6];
  153. if (!FlightSim.enabled || !name) return;
  154. len = strlen((const char *)name);
  155. buf[0] = len + 6;
  156. buf[1] = 1;
  157. buf[2] = id;
  158. buf[3] = id >> 8;
  159. buf[4] = 2;
  160. buf[5] = 0;
  161. FlightSimClass::xmit(buf, 6, name, len);
  162. }
  163. void FlightSimFloat::write(float val)
  164. {
  165. uint8_t buf[6];
  166. value = val;
  167. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  168. buf[0] = 10;
  169. buf[1] = 2;
  170. buf[2] = id;
  171. buf[3] = id >> 8;
  172. buf[4] = 2;
  173. buf[5] = 0;
  174. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  175. }
  176. void FlightSimFloat::update(float val)
  177. {
  178. value = val;
  179. if (change_callback) (*change_callback)(val);
  180. }
  181. FlightSimFloat * FlightSimFloat::find(unsigned int n)
  182. {
  183. for (FlightSimFloat *p = first; p; p = p->next) {
  184. if (p->id == n) return p;
  185. }
  186. return NULL;
  187. }
  188. FlightSimClass::FlightSimClass()
  189. {
  190. }
  191. void FlightSimClass::update(void)
  192. {
  193. uint8_t len, maxlen, type, *p, *end;
  194. usb_packet_t *rx_packet;
  195. uint16_t id;
  196. while (1) {
  197. if (!usb_configuration) break;
  198. rx_packet = usb_rx(FLIGHTSIM_RX_ENDPOINT);
  199. if (!rx_packet) break;
  200. p = rx_packet->buf;
  201. end = p + 64;
  202. maxlen = 64;
  203. do {
  204. len = p[0];
  205. if (len < 2 || len > maxlen) break;
  206. switch (p[1]) {
  207. case 0x02: // write data
  208. if (len < 10) break;
  209. id = p[2] | (p[3] << 8);
  210. type = p[4];
  211. if (type == 1) {
  212. FlightSimInteger *item = FlightSimInteger::find(id);
  213. if (!item) break;
  214. item->update(*(long *)(p + 6));
  215. } else if (type == 2) {
  216. FlightSimFloat *item = FlightSimFloat::find(id);
  217. if (!item) break;
  218. item->update(*(float *)(p + 6));
  219. }
  220. break;
  221. case 0x03: // enable/disable
  222. if (len < 4) break;
  223. switch (p[2]) {
  224. case 1:
  225. request_id_messages = 1;
  226. case 2:
  227. enable();
  228. frameCount++;
  229. break;
  230. case 3:
  231. disable();
  232. }
  233. }
  234. p += len;
  235. maxlen -= len;
  236. } while (p < end);
  237. usb_free(rx_packet);
  238. }
  239. if (enabled && request_id_messages) {
  240. request_id_messages = 0;
  241. for (FlightSimCommand *p = FlightSimCommand::first; p; p = p->next) {
  242. p->identify();
  243. }
  244. for (FlightSimInteger *p = FlightSimInteger::first; p; p = p->next) {
  245. p->identify();
  246. // TODO: send any dirty data
  247. }
  248. for (FlightSimFloat *p = FlightSimFloat::first; p; p = p->next) {
  249. p->identify();
  250. // TODO: send any dirty data
  251. }
  252. }
  253. }
  254. bool FlightSimClass::isEnabled(void)
  255. {
  256. if (!usb_configuration) return false;
  257. if (!enabled) return false;
  258. if (enableTimeout > 1500) return false;
  259. return true;
  260. }
  261. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  262. #define TX_PACKET_LIMIT 8
  263. static usb_packet_t *tx_packet=NULL;
  264. static volatile uint8_t tx_noautoflush=0;
  265. void FlightSimClass::xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  266. {
  267. uint8_t total;
  268. total = n1 + n2;
  269. if (total > FLIGHTSIM_TX_SIZE) return;
  270. if (!enabled || !usb_configuration) return;
  271. tx_noautoflush = 1;
  272. if (tx_packet) {
  273. if (total <= FLIGHTSIM_TX_SIZE - tx_packet->index) goto send;
  274. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  275. tx_packet->buf[i] = 0;
  276. }
  277. tx_packet->len = FLIGHTSIM_TX_SIZE;
  278. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  279. tx_packet = NULL;
  280. }
  281. while (1) {
  282. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  283. tx_packet = usb_malloc();
  284. if (tx_packet) break;
  285. }
  286. if (!enabled || !usb_configuration) {
  287. tx_noautoflush = 0;
  288. return;
  289. }
  290. tx_noautoflush = 0;
  291. yield();
  292. tx_noautoflush = 1;
  293. }
  294. send:
  295. memcpy(tx_packet->buf + tx_packet->index, p1, n1);
  296. tx_packet->index += n1;
  297. if (n2 > 0) {
  298. memcpy(tx_packet->buf + tx_packet->index, p2, n2);
  299. tx_packet->index += n2;
  300. }
  301. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  302. tx_packet->len = FLIGHTSIM_TX_SIZE;
  303. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  304. tx_packet = NULL;
  305. }
  306. tx_noautoflush = 0;
  307. }
  308. extern "C" {
  309. void usb_flightsim_flush_callback(void)
  310. {
  311. if (tx_noautoflush || !tx_packet || tx_packet->index == 0) return;
  312. for (int i=tx_packet->index; i < FLIGHTSIM_TX_ENDPOINT; i++) {
  313. tx_packet->buf[i] = 0;
  314. }
  315. tx_packet->len = FLIGHTSIM_TX_SIZE;
  316. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  317. tx_packet = NULL;
  318. }
  319. }
  320. #endif // FLIGHTSIM_INTERFACE