Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

325 lines
11KB

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