選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

usb_flightsim.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #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. };
  65. class FlightSimCommand
  66. {
  67. public:
  68. FlightSimCommand();
  69. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  70. FlightSimCommand & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  71. void begin(void) { sendcmd(4); }
  72. void end(void) { sendcmd(5); }
  73. FlightSimCommand & operator = (int n) { sendcmd((n) ? 4 : 5); return *this; }
  74. void once(void) { sendcmd(6); }
  75. void identify(void);
  76. private:
  77. unsigned int id;
  78. const _XpRefStr_ *name;
  79. void sendcmd(uint8_t n);
  80. FlightSimCommand *next;
  81. static FlightSimCommand *first;
  82. static FlightSimCommand *last;
  83. friend class FlightSimClass;
  84. };
  85. class FlightSimInteger
  86. {
  87. public:
  88. FlightSimInteger();
  89. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  90. FlightSimInteger & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  91. void write(long val);
  92. FlightSimInteger & operator = (char n) { write((long)n); return *this; }
  93. FlightSimInteger & operator = (int n) { write((long)n); return *this; }
  94. FlightSimInteger & operator = (long n) { write(n); return *this; }
  95. FlightSimInteger & operator = (unsigned char n) { write((long)n); return *this; }
  96. FlightSimInteger & operator = (unsigned int n) { write((long)n); return *this; }
  97. FlightSimInteger & operator = (unsigned long n) { write((long)n); return *this; }
  98. FlightSimInteger & operator = (float n) { write((long)n); return *this; }
  99. FlightSimInteger & operator = (double n) { write((long)n); return *this; }
  100. long read(void) const { return value; }
  101. operator long () const { return value; }
  102. void identify(void);
  103. void update(long val);
  104. static FlightSimInteger * find(unsigned int n);
  105. void onChange(void (*fptr)(long)) {
  106. hasCallbackInfo=false;
  107. change_callback = fptr;
  108. }
  109. void onChange(void (*fptr)(long,void*), void* info) {
  110. hasCallbackInfo=true;
  111. change_callback = (void (*)(long))fptr;
  112. callbackInfo = info;
  113. }
  114. // TODO: math operators.... + - * / % ++ --
  115. private:
  116. unsigned int id;
  117. const _XpRefStr_ *name;
  118. long value;
  119. void (*change_callback)(long);
  120. void* callbackInfo;
  121. bool hasCallbackInfo;
  122. FlightSimInteger *next;
  123. static FlightSimInteger *first;
  124. static FlightSimInteger *last;
  125. friend class FlightSimClass;
  126. };
  127. class FlightSimFloat
  128. {
  129. public:
  130. FlightSimFloat();
  131. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  132. FlightSimFloat & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  133. void write(float val);
  134. FlightSimFloat & operator = (char n) { write((float)n); return *this; }
  135. FlightSimFloat & operator = (int n) { write((float)n); return *this; }
  136. FlightSimFloat & operator = (long n) { write((float)n); return *this; }
  137. FlightSimFloat & operator = (unsigned char n) { write((float)n); return *this; }
  138. FlightSimFloat & operator = (unsigned int n) { write((float)n); return *this; }
  139. FlightSimFloat & operator = (unsigned long n) { write((float)n); return *this; }
  140. FlightSimFloat & operator = (float n) { write(n); return *this; }
  141. FlightSimFloat & operator = (double n) { write((float)n); return *this; }
  142. float read(void) const { return value; }
  143. operator float () const { return value; }
  144. void identify(void);
  145. void update(float val);
  146. static FlightSimFloat * find(unsigned int n);
  147. void onChange(void (*fptr)(float)) {
  148. hasCallbackInfo=false;
  149. change_callback = fptr;
  150. }
  151. void onChange(void (*fptr)(float,void*), void* info) {
  152. hasCallbackInfo=true;
  153. change_callback = (void (*)(float))fptr;
  154. callbackInfo = info;
  155. }
  156. // TODO: math operators.... + - * / % ++ --
  157. private:
  158. unsigned int id;
  159. const _XpRefStr_ *name;
  160. float value;
  161. void (*change_callback)(float);
  162. void* callbackInfo;
  163. bool hasCallbackInfo;
  164. FlightSimFloat *next;
  165. static FlightSimFloat *first;
  166. static FlightSimFloat *last;
  167. friend class FlightSimClass;
  168. };
  169. class FlightSimElapsedFrames
  170. {
  171. private:
  172. unsigned long count;
  173. public:
  174. FlightSimElapsedFrames(void) { count = FlightSimClass::getFrameCount(); }
  175. FlightSimElapsedFrames(unsigned long val) { count = FlightSimClass::getFrameCount() - val; }
  176. FlightSimElapsedFrames(const FlightSimElapsedFrames &orig) { count = orig.count; }
  177. operator unsigned long () const { return FlightSimClass::getFrameCount() - count; }
  178. FlightSimElapsedFrames & operator = (const FlightSimElapsedFrames &rhs) { count = rhs.count; return *this; }
  179. FlightSimElapsedFrames & operator = (unsigned long val) { count = FlightSimClass::getFrameCount() - val; return *this; }
  180. FlightSimElapsedFrames & operator -= (unsigned long val) { count += val; return *this; }
  181. FlightSimElapsedFrames & operator += (unsigned long val) { count -= val; return *this; }
  182. FlightSimElapsedFrames operator - (int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  183. FlightSimElapsedFrames operator - (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  184. FlightSimElapsedFrames operator - (long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  185. FlightSimElapsedFrames operator - (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  186. FlightSimElapsedFrames operator + (int val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  187. FlightSimElapsedFrames operator + (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  188. FlightSimElapsedFrames operator + (long val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  189. FlightSimElapsedFrames operator + (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  190. };
  191. extern FlightSimClass FlightSim;
  192. #endif // __cplusplus
  193. #endif // FLIGHTSIM_INTERFACE
  194. #endif // USBflightsim_h_