Teensy 4.1 core updated for C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

370 lines
8.3KB

  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. usb_packet_t *rx_packet;
  196. uint16_t id;
  197. while (1) {
  198. if (!usb_configuration) break;
  199. rx_packet = usb_rx(FLIGHTSIM_RX_ENDPOINT);
  200. if (!rx_packet) break;
  201. p = rx_packet->buf;
  202. end = p + 64;
  203. maxlen = 64;
  204. do {
  205. len = p[0];
  206. if (len < 2 || len > maxlen) break;
  207. switch (p[1]) {
  208. case 0x02: // write data
  209. if (len < 10) break;
  210. id = p[2] | (p[3] << 8);
  211. type = p[4];
  212. if (type == 1) {
  213. FlightSimInteger *item = FlightSimInteger::find(id);
  214. if (!item) break;
  215. item->update(*(long *)(p + 6));
  216. } else if (type == 2) {
  217. FlightSimFloat *item = FlightSimFloat::find(id);
  218. if (!item) break;
  219. item->update(*(float *)(p + 6));
  220. }
  221. break;
  222. case 0x03: // enable/disable
  223. if (len < 4) break;
  224. switch (p[2]) {
  225. case 1:
  226. request_id_messages = 1;
  227. case 2:
  228. enable();
  229. frameCount++;
  230. break;
  231. case 3:
  232. disable();
  233. }
  234. }
  235. p += len;
  236. maxlen -= len;
  237. } while (p < end);
  238. usb_free(rx_packet);
  239. }
  240. if (enabled && request_id_messages) {
  241. request_id_messages = 0;
  242. for (FlightSimCommand *p = FlightSimCommand::first; p; p = p->next) {
  243. p->identify();
  244. }
  245. for (FlightSimInteger *p = FlightSimInteger::first; p; p = p->next) {
  246. p->identify();
  247. // TODO: send any dirty data
  248. }
  249. for (FlightSimFloat *p = FlightSimFloat::first; p; p = p->next) {
  250. p->identify();
  251. // TODO: send any dirty data
  252. }
  253. }
  254. }
  255. bool FlightSimClass::isEnabled(void)
  256. {
  257. if (!usb_configuration) return false;
  258. if (!enabled) return false;
  259. if (enableTimeout > 1500) return false;
  260. return true;
  261. }
  262. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  263. #define TX_PACKET_LIMIT 8
  264. static usb_packet_t *tx_packet=NULL;
  265. static volatile uint8_t tx_noautoflush=0;
  266. void FlightSimClass::xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  267. {
  268. uint8_t total;
  269. total = n1 + n2;
  270. if (total > FLIGHTSIM_TX_SIZE) return;
  271. if (!enabled || !usb_configuration) return;
  272. tx_noautoflush = 1;
  273. if (tx_packet) {
  274. if (total <= FLIGHTSIM_TX_SIZE - tx_packet->index) goto send;
  275. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  276. tx_packet->buf[i] = 0;
  277. }
  278. tx_packet->len = FLIGHTSIM_TX_SIZE;
  279. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  280. tx_packet = NULL;
  281. }
  282. while (1) {
  283. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  284. tx_packet = usb_malloc();
  285. if (tx_packet) break;
  286. }
  287. if (!enabled || !usb_configuration) {
  288. tx_noautoflush = 0;
  289. return;
  290. }
  291. tx_noautoflush = 0;
  292. yield();
  293. tx_noautoflush = 1;
  294. }
  295. send:
  296. memcpy(tx_packet->buf + tx_packet->index, p1, n1);
  297. tx_packet->index += n1;
  298. if (n2 > 0) {
  299. memcpy(tx_packet->buf + tx_packet->index, p2, n2);
  300. tx_packet->index += n2;
  301. }
  302. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  303. tx_packet->len = FLIGHTSIM_TX_SIZE;
  304. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  305. tx_packet = NULL;
  306. }
  307. tx_noautoflush = 0;
  308. }
  309. extern "C" {
  310. void usb_flightsim_flush_callback(void)
  311. {
  312. if (tx_noautoflush || !tx_packet || tx_packet->index == 0) return;
  313. for (int i=tx_packet->index; i < FLIGHTSIM_TX_ENDPOINT; i++) {
  314. tx_packet->buf[i] = 0;
  315. }
  316. tx_packet->len = FLIGHTSIM_TX_SIZE;
  317. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  318. tx_packet = NULL;
  319. }
  320. }
  321. #endif // F_CPU
  322. #endif // FLIGHTSIM_INTERFACE