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.

usb_flightsim.h 7.6KB

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