Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

186 lines
7.4KB

  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. #if defined(USB_FLIGHTSIM) && defined(__cplusplus)
  33. #include <inttypes.h>
  34. #include "elapsedMillis.h"
  35. class FlightSimClass;
  36. class FlightSimCommand;
  37. class FlightSimInteger;
  38. class _XpRefStr_;
  39. #define XPlaneRef(str) ((const _XpRefStr_ *)(str))
  40. class FlightSimClass
  41. {
  42. public:
  43. FlightSimClass();
  44. static void update(void);
  45. static bool isEnabled(void);
  46. static unsigned long getFrameCount(void) { return frameCount; }
  47. private:
  48. static uint8_t request_id_messages;
  49. static uint8_t enabled;
  50. static elapsedMillis enableTimeout;
  51. static unsigned long frameCount;
  52. static void enable(void) { enabled = 1; enableTimeout = 0; }
  53. static void disable(void) { enabled = 0; }
  54. static void xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2);
  55. friend class FlightSimCommand;
  56. friend class FlightSimInteger;
  57. friend class FlightSimFloat;
  58. };
  59. class FlightSimCommand
  60. {
  61. public:
  62. FlightSimCommand();
  63. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  64. FlightSimCommand & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  65. void begin(void) { sendcmd(4); }
  66. void end(void) { sendcmd(5); }
  67. FlightSimCommand & operator = (int n) { sendcmd((n) ? 4 : 5); return *this; }
  68. void once(void) { sendcmd(6); }
  69. void identify(void);
  70. private:
  71. unsigned int id;
  72. const _XpRefStr_ *name;
  73. void sendcmd(uint8_t n);
  74. FlightSimCommand *next;
  75. static FlightSimCommand *first;
  76. static FlightSimCommand *last;
  77. friend class FlightSimClass;
  78. };
  79. class FlightSimInteger
  80. {
  81. public:
  82. FlightSimInteger();
  83. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  84. FlightSimInteger & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  85. void write(long val);
  86. FlightSimInteger & operator = (char n) { write((long)n); return *this; }
  87. FlightSimInteger & operator = (int n) { write((long)n); return *this; }
  88. FlightSimInteger & operator = (long n) { write(n); return *this; }
  89. FlightSimInteger & operator = (unsigned char n) { write((long)n); return *this; }
  90. FlightSimInteger & operator = (unsigned int n) { write((long)n); return *this; }
  91. FlightSimInteger & operator = (unsigned long n) { write((long)n); return *this; }
  92. FlightSimInteger & operator = (float n) { write((long)n); return *this; }
  93. FlightSimInteger & operator = (double n) { write((long)n); return *this; }
  94. long read(void) const { return value; }
  95. operator long () const { return value; }
  96. void identify(void);
  97. void update(long val);
  98. static FlightSimInteger * find(unsigned int n);
  99. void onChange(void (*fptr)(long)) { change_callback = fptr; }
  100. // TODO: math operators.... + - * / % ++ --
  101. private:
  102. unsigned int id;
  103. const _XpRefStr_ *name;
  104. long value;
  105. void (*change_callback)(long);
  106. FlightSimInteger *next;
  107. static FlightSimInteger *first;
  108. static FlightSimInteger *last;
  109. friend class FlightSimClass;
  110. };
  111. class FlightSimFloat
  112. {
  113. public:
  114. FlightSimFloat();
  115. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  116. FlightSimFloat & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  117. void write(float val);
  118. FlightSimFloat & operator = (char n) { write((float)n); return *this; }
  119. FlightSimFloat & operator = (int n) { write((float)n); return *this; }
  120. FlightSimFloat & operator = (long n) { write((float)n); return *this; }
  121. FlightSimFloat & operator = (unsigned char n) { write((float)n); return *this; }
  122. FlightSimFloat & operator = (unsigned int n) { write((float)n); return *this; }
  123. FlightSimFloat & operator = (unsigned long n) { write((float)n); return *this; }
  124. FlightSimFloat & operator = (float n) { write(n); return *this; }
  125. FlightSimFloat & operator = (double n) { write((float)n); return *this; }
  126. float read(void) const { return value; }
  127. operator float () const { return value; }
  128. void identify(void);
  129. void update(float val);
  130. static FlightSimFloat * find(unsigned int n);
  131. void onChange(void (*fptr)(float)) { change_callback = fptr; }
  132. // TODO: math operators.... + - * / % ++ --
  133. private:
  134. unsigned int id;
  135. const _XpRefStr_ *name;
  136. float value;
  137. void (*change_callback)(float);
  138. FlightSimFloat *next;
  139. static FlightSimFloat *first;
  140. static FlightSimFloat *last;
  141. friend class FlightSimClass;
  142. };
  143. class FlightSimElapsedFrames
  144. {
  145. private:
  146. unsigned long count;
  147. public:
  148. FlightSimElapsedFrames(void) { count = FlightSimClass::getFrameCount(); }
  149. FlightSimElapsedFrames(unsigned long val) { count = FlightSimClass::getFrameCount() - val; }
  150. FlightSimElapsedFrames(const FlightSimElapsedFrames &orig) { count = orig.count; }
  151. operator unsigned long () const { return FlightSimClass::getFrameCount() - count; }
  152. FlightSimElapsedFrames & operator = (const FlightSimElapsedFrames &rhs) { count = rhs.count; return *this; }
  153. FlightSimElapsedFrames & operator = (unsigned long val) { count = FlightSimClass::getFrameCount() - val; return *this; }
  154. FlightSimElapsedFrames & operator -= (unsigned long val) { count += val; return *this; }
  155. FlightSimElapsedFrames & operator += (unsigned long val) { count -= val; return *this; }
  156. FlightSimElapsedFrames operator - (int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  157. FlightSimElapsedFrames operator - (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  158. FlightSimElapsedFrames operator - (long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  159. FlightSimElapsedFrames operator - (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  160. FlightSimElapsedFrames operator + (int val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  161. FlightSimElapsedFrames operator + (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  162. FlightSimElapsedFrames operator + (long val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  163. FlightSimElapsedFrames operator + (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  164. };
  165. extern FlightSimClass FlightSim;
  166. #endif
  167. #endif