您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef USBserial_h_
  2. #define USBserial_h_
  3. #include <inttypes.h>
  4. #include "Print.h"
  5. #include "Stream.h"
  6. #include "elapsedMillis.h"
  7. class FlightSimClass;
  8. class FlightSimCommand;
  9. class FlightSimInteger;
  10. class _XpRefStr_;
  11. #define XPlaneRef(str) ((const _XpRefStr_ *)(PSTR(str)))
  12. class FlightSimClass
  13. {
  14. public:
  15. FlightSimClass();
  16. static void update(void);
  17. static bool isEnabled(void);
  18. static unsigned long getFrameCount(void) { return frameCount; }
  19. private:
  20. static uint8_t request_id_messages;
  21. static uint8_t enabled;
  22. static elapsedMillis enableTimeout;
  23. static unsigned long frameCount;
  24. static void enable(void) { enabled = 1; enableTimeout = 0; }
  25. static void disable(void) { enabled = 0; }
  26. static uint8_t recv(uint8_t *buffer);
  27. static void xmit(const uint8_t *p1, uint8_t n1);
  28. static void xmit(const uint8_t *p1, uint8_t n1, const uint8_t *p2, uint8_t n2);
  29. static void xmit(const uint8_t *p1, uint8_t n1, const _XpRefStr_ *p2, uint16_t n2);
  30. static void xmit_big_packet(const uint8_t *p1, uint8_t n1, const _XpRefStr_ *p2, uint16_t n2);
  31. friend class FlightSimCommand;
  32. friend class FlightSimInteger;
  33. friend class FlightSimFloat;
  34. };
  35. class FlightSimCommand
  36. {
  37. public:
  38. FlightSimCommand();
  39. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  40. FlightSimCommand & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  41. void begin(void) { sendcmd(4); }
  42. void end(void) { sendcmd(5); }
  43. FlightSimCommand & operator = (int n) { sendcmd((n) ? 4 : 5); return *this; }
  44. void once(void) { sendcmd(6); }
  45. void identify(void);
  46. private:
  47. unsigned int id;
  48. const _XpRefStr_ *name;
  49. void sendcmd(uint8_t n);
  50. FlightSimCommand *next;
  51. static FlightSimCommand *first;
  52. static FlightSimCommand *last;
  53. friend class FlightSimClass;
  54. };
  55. class FlightSimInteger
  56. {
  57. public:
  58. FlightSimInteger();
  59. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  60. FlightSimInteger & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  61. void write(long val);
  62. FlightSimInteger & operator = (char n) { write((long)n); return *this; }
  63. FlightSimInteger & operator = (int n) { write((long)n); return *this; }
  64. FlightSimInteger & operator = (long n) { write(n); return *this; }
  65. FlightSimInteger & operator = (unsigned char n) { write((long)n); return *this; }
  66. FlightSimInteger & operator = (unsigned int n) { write((long)n); return *this; }
  67. FlightSimInteger & operator = (unsigned long n) { write((long)n); return *this; }
  68. FlightSimInteger & operator = (float n) { write((long)n); return *this; }
  69. FlightSimInteger & operator = (double n) { write((long)n); return *this; }
  70. long read(void) const { return value; }
  71. operator long () const { return value; }
  72. void identify(void);
  73. void update(long val);
  74. static FlightSimInteger * find(unsigned int n);
  75. void onChange(void (*fptr)(long)) {
  76. hasCallbackInfo=false;
  77. change_callback = fptr;
  78. }
  79. void onChange(void (*fptr)(long,void*), void* info) {
  80. hasCallbackInfo=true;
  81. change_callback = (void (*)(long))fptr;
  82. callbackInfo = info;
  83. }
  84. // TODO: math operators.... + - * / % ++ --
  85. private:
  86. unsigned int id;
  87. const _XpRefStr_ *name;
  88. long value;
  89. void (*change_callback)(long);
  90. void* callbackInfo;
  91. bool hasCallbackInfo;
  92. FlightSimInteger *next;
  93. static FlightSimInteger *first;
  94. static FlightSimInteger *last;
  95. friend class FlightSimClass;
  96. };
  97. class FlightSimFloat
  98. {
  99. public:
  100. FlightSimFloat();
  101. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  102. FlightSimFloat & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  103. void write(float val);
  104. FlightSimFloat & operator = (char n) { write((float)n); return *this; }
  105. FlightSimFloat & operator = (int n) { write((float)n); return *this; }
  106. FlightSimFloat & operator = (long n) { write((float)n); return *this; }
  107. FlightSimFloat & operator = (unsigned char n) { write((float)n); return *this; }
  108. FlightSimFloat & operator = (unsigned int n) { write((float)n); return *this; }
  109. FlightSimFloat & operator = (unsigned long n) { write((float)n); return *this; }
  110. FlightSimFloat & operator = (float n) { write(n); return *this; }
  111. FlightSimFloat & operator = (double n) { write((float)n); return *this; }
  112. float read(void) const { return value; }
  113. operator float () const { return value; }
  114. void identify(void);
  115. void update(float val);
  116. static FlightSimFloat * find(unsigned int n);
  117. void onChange(void (*fptr)(float)) {
  118. hasCallbackInfo=false;
  119. change_callback = fptr;
  120. }
  121. void onChange(void (*fptr)(float,void*), void* info) {
  122. hasCallbackInfo=true;
  123. change_callback = (void (*)(float))fptr;
  124. callbackInfo = info;
  125. }
  126. // TODO: math operators.... + - * / % ++ --
  127. private:
  128. unsigned int id;
  129. const _XpRefStr_ *name;
  130. float value;
  131. void (*change_callback)(float);
  132. void* callbackInfo;
  133. bool hasCallbackInfo;
  134. FlightSimFloat *next;
  135. static FlightSimFloat *first;
  136. static FlightSimFloat *last;
  137. friend class FlightSimClass;
  138. };
  139. class FlightSimElapsedFrames
  140. {
  141. private:
  142. unsigned long count;
  143. public:
  144. FlightSimElapsedFrames(void) { count = FlightSimClass::getFrameCount(); }
  145. FlightSimElapsedFrames(unsigned long val) { count = FlightSimClass::getFrameCount() - val; }
  146. FlightSimElapsedFrames(const FlightSimElapsedFrames &orig) { count = orig.count; }
  147. operator unsigned long () const { return FlightSimClass::getFrameCount() - count; }
  148. FlightSimElapsedFrames & operator = (const FlightSimElapsedFrames &rhs) { count = rhs.count; return *this; }
  149. FlightSimElapsedFrames & operator = (unsigned long val) { count = FlightSimClass::getFrameCount() - val; return *this; }
  150. FlightSimElapsedFrames & operator -= (unsigned long val) { count += val; return *this; }
  151. FlightSimElapsedFrames & operator += (unsigned long val) { count -= val; return *this; }
  152. FlightSimElapsedFrames operator - (int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  153. FlightSimElapsedFrames operator - (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  154. FlightSimElapsedFrames operator - (long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  155. FlightSimElapsedFrames operator - (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  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. };
  161. extern FlightSimClass FlightSim;
  162. #if 0
  163. class usb_rawhid_class
  164. {
  165. public:
  166. int available(void);
  167. int recv(void *buffer, uint16_t timeout);
  168. int send(const void *buffer, uint16_t timeout);
  169. };
  170. extern usb_rawhid_class RawHID;
  171. #endif
  172. class usb_serial_class : public Stream
  173. {
  174. public:
  175. // standard Arduino functions
  176. void begin(long);
  177. void end();
  178. virtual int available();
  179. virtual int read();
  180. virtual int peek();
  181. virtual void flush();
  182. #if ARDUINO >= 100
  183. virtual size_t write(uint8_t);
  184. #else
  185. virtual void write(uint8_t);
  186. #endif
  187. using Print::write;
  188. operator bool();
  189. // Teensy extensions
  190. void send_now(void);
  191. uint32_t baud(void);
  192. uint8_t stopbits(void);
  193. uint8_t paritytype(void);
  194. uint8_t numbits(void);
  195. uint8_t dtr(void);
  196. uint8_t rts(void);
  197. private:
  198. uint8_t readnext(void);
  199. };
  200. extern usb_serial_class Serial;
  201. #endif