Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

usb_flightsim.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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. #ifndef USBflightsim_h_
  31. #define USBflightsim_h_
  32. #include "usb_desc.h"
  33. #if defined(FLIGHTSIM_INTERFACE)
  34. #ifdef __cplusplus
  35. #include <inttypes.h>
  36. #include "elapsedMillis.h"
  37. // workaround for elapsedMillis.h bringing in WProgram.h which brings usb_undef.h
  38. #undef USB_DESC_LIST_DEFINE
  39. #include "usb_desc.h"
  40. class FlightSimClass;
  41. class FlightSimCommand;
  42. class FlightSimInteger;
  43. class _XpRefStr_;
  44. #define XPlaneRef(str) ((const _XpRefStr_ *)(str))
  45. class FlightSimClass
  46. {
  47. public:
  48. FlightSimClass();
  49. static void update(void);
  50. static bool isEnabled(void);
  51. static unsigned long getFrameCount(void) { return frameCount; }
  52. private:
  53. static uint8_t request_id_messages;
  54. static uint8_t enabled;
  55. static elapsedMillis enableTimeout;
  56. static unsigned long frameCount;
  57. static void enable(void) { enabled = 1; enableTimeout = 0; }
  58. static void disable(void) { enabled = 0; }
  59. static void xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2);
  60. static void xmit_big_packet(const void *p1, uint8_t n1, const void *p2, uint8_t n2);
  61. friend class FlightSimCommand;
  62. friend class FlightSimInteger;
  63. friend class FlightSimFloat;
  64. /// JB
  65. friend class FlightSimEvent;
  66. friend class FlightSimData;
  67. /// JB End
  68. };
  69. class FlightSimCommand
  70. {
  71. public:
  72. FlightSimCommand();
  73. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  74. FlightSimCommand & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  75. void begin(void) { sendcmd(4); }
  76. void end(void) { sendcmd(5); }
  77. FlightSimCommand & operator = (int n) { sendcmd((n) ? 4 : 5); return *this; }
  78. void once(void) { sendcmd(6); }
  79. void identify(void);
  80. private:
  81. unsigned int id;
  82. const _XpRefStr_ *name;
  83. void sendcmd(uint8_t n);
  84. FlightSimCommand *next;
  85. static FlightSimCommand *first;
  86. static FlightSimCommand *last;
  87. friend class FlightSimClass;
  88. };
  89. /// JB
  90. class FlightSimEvent
  91. {
  92. public:
  93. FlightSimEvent();
  94. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  95. FlightSimEvent & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  96. void send() { send(0,0); }
  97. void send(int data) { send(data,0); }
  98. void sendOnce() { send(0,0); }
  99. void sendOnce(int data) { send(data,0); }
  100. void sendRepeat(int data, uint16_t initialDelay, uint16_t repeatDelay) { send(data, initialDelay<<16 | repeatDelay); }
  101. void sendRepeat(uint16_t initialDelay, uint16_t repeatDelay) { send(0, initialDelay<<16 | repeatDelay); }
  102. void stopRepeat() { send(0,-1); }
  103. FlightSimEvent & operator = (int n) { send(n,0); return *this; }
  104. bool occurred() { bool hasOccurred = occurredFlag; occurredFlag = 0; return hasOccurred; }
  105. void identify();
  106. static FlightSimEvent * find(unsigned int n);
  107. void update(long val);
  108. void onOccur(void (*fptr)(long)) {
  109. hasCallbackInfo=false;
  110. occur_callback = fptr;
  111. }
  112. void onOccur(void (*fptr)(long,void*), void* info) {
  113. hasCallbackInfo=true;
  114. occur_callback = (void (*)(long))fptr;
  115. callbackInfo = info;
  116. }
  117. private:
  118. void send(unsigned int data, unsigned int flags);
  119. unsigned int id;
  120. const _XpRefStr_ *name;
  121. bool occurredFlag;
  122. unsigned int value;
  123. FlightSimEvent *next;
  124. void (*occur_callback)(long);
  125. void* callbackInfo;
  126. bool hasCallbackInfo;
  127. static FlightSimEvent *first;
  128. static FlightSimEvent *last;
  129. friend class FlightSimClass;
  130. };
  131. #define FLIGHTSIM_DATA_MAXLEN 58
  132. class FlightSimData
  133. {
  134. public:
  135. FlightSimData();
  136. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  137. FlightSimData & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  138. char *read() { return value; }
  139. operator char* () { return value; }
  140. void identify();
  141. void update(char *val, size_t len);
  142. size_t len() { return valueLen; }
  143. static FlightSimData * find(unsigned int n);
  144. void onChange(void (*fptr)(char *)) {
  145. hasCallbackInfo = false;
  146. change_callback = fptr;
  147. }
  148. void onChange(void (*fptr)(char *,void *), void *info) {
  149. hasCallbackInfo = true;
  150. change_callback = (void (*)(char*)) fptr;
  151. callbackInfo = info;
  152. }
  153. void onChange(void (*fptr)(FlightSimData *)) {
  154. callbackWithObject = true;
  155. hasCallbackInfo = false;
  156. change_callback = (void (*)(char*)) fptr;
  157. }
  158. void onChange(void (*fptr)(FlightSimData *, void*), void* info) {
  159. callbackWithObject = true;
  160. hasCallbackInfo = true;
  161. change_callback = (void (*)(char*)) fptr;
  162. callbackInfo = info;
  163. }
  164. private:
  165. unsigned int id;
  166. const _XpRefStr_ *name;
  167. char value[FLIGHTSIM_DATA_MAXLEN];
  168. size_t valueLen;
  169. void (*change_callback)(char *);
  170. void* callbackInfo;
  171. bool hasCallbackInfo;
  172. bool callbackWithObject;
  173. FlightSimData *next;
  174. static FlightSimData *first;
  175. static FlightSimData *last;
  176. friend class FlightSimClass;
  177. };
  178. /// JB End
  179. class FlightSimInteger
  180. {
  181. public:
  182. FlightSimInteger();
  183. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  184. FlightSimInteger & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  185. void write(long val);
  186. FlightSimInteger & operator = (char n) { write((long)n); return *this; }
  187. FlightSimInteger & operator = (int n) { write((long)n); return *this; }
  188. FlightSimInteger & operator = (long n) { write(n); return *this; }
  189. FlightSimInteger & operator = (unsigned char n) { write((long)n); return *this; }
  190. FlightSimInteger & operator = (unsigned int n) { write((long)n); return *this; }
  191. FlightSimInteger & operator = (unsigned long n) { write((long)n); return *this; }
  192. FlightSimInteger & operator = (float n) { write((long)n); return *this; }
  193. FlightSimInteger & operator = (double n) { write((long)n); return *this; }
  194. long read(void) const { return value; }
  195. operator long () const { return value; }
  196. void identify(void);
  197. void update(long val);
  198. static FlightSimInteger * find(unsigned int n);
  199. void onChange(void (*fptr)(long)) {
  200. hasCallbackInfo=false;
  201. change_callback = fptr;
  202. }
  203. void onChange(void (*fptr)(long,void*), void* info) {
  204. hasCallbackInfo=true;
  205. change_callback = (void (*)(long))fptr;
  206. callbackInfo = info;
  207. }
  208. // TODO: math operators.... + - * / % ++ --
  209. private:
  210. unsigned int id;
  211. const _XpRefStr_ *name;
  212. long value;
  213. void (*change_callback)(long);
  214. void* callbackInfo;
  215. bool hasCallbackInfo;
  216. FlightSimInteger *next;
  217. static FlightSimInteger *first;
  218. static FlightSimInteger *last;
  219. friend class FlightSimClass;
  220. };
  221. class FlightSimFloat
  222. {
  223. public:
  224. FlightSimFloat();
  225. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  226. FlightSimFloat & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  227. void write(float val);
  228. FlightSimFloat & operator = (char n) { write((float)n); return *this; }
  229. FlightSimFloat & operator = (int n) { write((float)n); return *this; }
  230. FlightSimFloat & operator = (long n) { write((float)n); return *this; }
  231. FlightSimFloat & operator = (unsigned char n) { write((float)n); return *this; }
  232. FlightSimFloat & operator = (unsigned int n) { write((float)n); return *this; }
  233. FlightSimFloat & operator = (unsigned long n) { write((float)n); return *this; }
  234. FlightSimFloat & operator = (float n) { write(n); return *this; }
  235. FlightSimFloat & operator = (double n) { write((float)n); return *this; }
  236. float read(void) const { return value; }
  237. operator float () const { return value; }
  238. void identify(void);
  239. void update(float val);
  240. static FlightSimFloat * find(unsigned int n);
  241. void onChange(void (*fptr)(float)) {
  242. hasCallbackInfo=false;
  243. change_callback = fptr;
  244. }
  245. void onChange(void (*fptr)(float,void*), void* info) {
  246. hasCallbackInfo=true;
  247. change_callback = (void (*)(float))fptr;
  248. callbackInfo = info;
  249. }
  250. // TODO: math operators.... + - * / % ++ --
  251. private:
  252. unsigned int id;
  253. const _XpRefStr_ *name;
  254. float value;
  255. void (*change_callback)(float);
  256. void* callbackInfo;
  257. bool hasCallbackInfo;
  258. FlightSimFloat *next;
  259. static FlightSimFloat *first;
  260. static FlightSimFloat *last;
  261. friend class FlightSimClass;
  262. };
  263. class FlightSimElapsedFrames
  264. {
  265. private:
  266. unsigned long count;
  267. public:
  268. FlightSimElapsedFrames(void) { count = FlightSimClass::getFrameCount(); }
  269. FlightSimElapsedFrames(unsigned long val) { count = FlightSimClass::getFrameCount() - val; }
  270. FlightSimElapsedFrames(const FlightSimElapsedFrames &orig) { count = orig.count; }
  271. operator unsigned long () const { return FlightSimClass::getFrameCount() - count; }
  272. FlightSimElapsedFrames & operator = (const FlightSimElapsedFrames &rhs) { count = rhs.count; return *this; }
  273. FlightSimElapsedFrames & operator = (unsigned long val) { count = FlightSimClass::getFrameCount() - val; return *this; }
  274. FlightSimElapsedFrames & operator -= (unsigned long val) { count += val; return *this; }
  275. FlightSimElapsedFrames & operator += (unsigned long val) { count -= val; return *this; }
  276. FlightSimElapsedFrames operator - (int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  277. FlightSimElapsedFrames operator - (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  278. FlightSimElapsedFrames operator - (long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  279. FlightSimElapsedFrames operator - (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  280. FlightSimElapsedFrames operator + (int val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  281. FlightSimElapsedFrames operator + (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  282. FlightSimElapsedFrames operator + (long val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  283. FlightSimElapsedFrames operator + (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  284. };
  285. extern FlightSimClass FlightSim;
  286. #endif // __cplusplus
  287. #endif // FLIGHTSIM_INTERFACE
  288. #endif // USBflightsim_h_