Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

204 líneas
6.9KB

  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, uint8_t n2);
  30. friend class FlightSimCommand;
  31. friend class FlightSimInteger;
  32. friend class FlightSimFloat;
  33. };
  34. class FlightSimCommand
  35. {
  36. public:
  37. FlightSimCommand();
  38. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  39. FlightSimCommand & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  40. void begin(void) { sendcmd(4); }
  41. void end(void) { sendcmd(5); }
  42. FlightSimCommand & operator = (int n) { sendcmd((n) ? 4 : 5); return *this; }
  43. void once(void) { sendcmd(6); }
  44. void identify(void);
  45. private:
  46. unsigned int id;
  47. const _XpRefStr_ *name;
  48. void sendcmd(uint8_t n);
  49. FlightSimCommand *next;
  50. static FlightSimCommand *first;
  51. static FlightSimCommand *last;
  52. friend class FlightSimClass;
  53. };
  54. class FlightSimInteger
  55. {
  56. public:
  57. FlightSimInteger();
  58. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  59. FlightSimInteger & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  60. void write(long val);
  61. FlightSimInteger & operator = (char n) { write((long)n); return *this; }
  62. FlightSimInteger & operator = (int n) { write((long)n); return *this; }
  63. FlightSimInteger & operator = (long n) { write(n); return *this; }
  64. FlightSimInteger & operator = (unsigned char n) { write((long)n); return *this; }
  65. FlightSimInteger & operator = (unsigned int n) { write((long)n); return *this; }
  66. FlightSimInteger & operator = (unsigned long n) { write((long)n); return *this; }
  67. FlightSimInteger & operator = (float n) { write((long)n); return *this; }
  68. FlightSimInteger & operator = (double n) { write((long)n); return *this; }
  69. long read(void) const { return value; }
  70. operator long () const { return value; }
  71. void identify(void);
  72. void update(long val);
  73. static FlightSimInteger * find(unsigned int n);
  74. void onChange(void (*fptr)(long)) { change_callback = fptr; }
  75. // TODO: math operators.... + - * / % ++ --
  76. private:
  77. unsigned int id;
  78. const _XpRefStr_ *name;
  79. long value;
  80. void (*change_callback)(long);
  81. FlightSimInteger *next;
  82. static FlightSimInteger *first;
  83. static FlightSimInteger *last;
  84. friend class FlightSimClass;
  85. };
  86. class FlightSimFloat
  87. {
  88. public:
  89. FlightSimFloat();
  90. void assign(const _XpRefStr_ *s) { name = s; if (FlightSimClass::enabled) identify(); }
  91. FlightSimFloat & operator = (const _XpRefStr_ *s) { assign(s); return *this; }
  92. void write(float val);
  93. FlightSimFloat & operator = (char n) { write((float)n); return *this; }
  94. FlightSimFloat & operator = (int n) { write((float)n); return *this; }
  95. FlightSimFloat & operator = (long n) { write((float)n); return *this; }
  96. FlightSimFloat & operator = (unsigned char n) { write((float)n); return *this; }
  97. FlightSimFloat & operator = (unsigned int n) { write((float)n); return *this; }
  98. FlightSimFloat & operator = (unsigned long n) { write((float)n); return *this; }
  99. FlightSimFloat & operator = (float n) { write(n); return *this; }
  100. FlightSimFloat & operator = (double n) { write((float)n); return *this; }
  101. float read(void) const { return value; }
  102. operator float () const { return value; }
  103. void identify(void);
  104. void update(float val);
  105. static FlightSimFloat * find(unsigned int n);
  106. void onChange(void (*fptr)(float)) { change_callback = fptr; }
  107. // TODO: math operators.... + - * / % ++ --
  108. private:
  109. unsigned int id;
  110. const _XpRefStr_ *name;
  111. float value;
  112. void (*change_callback)(float);
  113. FlightSimFloat *next;
  114. static FlightSimFloat *first;
  115. static FlightSimFloat *last;
  116. friend class FlightSimClass;
  117. };
  118. class FlightSimElapsedFrames
  119. {
  120. private:
  121. unsigned long count;
  122. public:
  123. FlightSimElapsedFrames(void) { count = FlightSimClass::getFrameCount(); }
  124. FlightSimElapsedFrames(unsigned long val) { count = FlightSimClass::getFrameCount() - val; }
  125. FlightSimElapsedFrames(const FlightSimElapsedFrames &orig) { count = orig.count; }
  126. operator unsigned long () const { return FlightSimClass::getFrameCount() - count; }
  127. FlightSimElapsedFrames & operator = (const FlightSimElapsedFrames &rhs) { count = rhs.count; return *this; }
  128. FlightSimElapsedFrames & operator = (unsigned long val) { count = FlightSimClass::getFrameCount() - val; return *this; }
  129. FlightSimElapsedFrames & operator -= (unsigned long val) { count += val; return *this; }
  130. FlightSimElapsedFrames & operator += (unsigned long val) { count -= val; return *this; }
  131. FlightSimElapsedFrames operator - (int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  132. FlightSimElapsedFrames operator - (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  133. FlightSimElapsedFrames operator - (long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  134. FlightSimElapsedFrames operator - (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count += val; return r; }
  135. FlightSimElapsedFrames operator + (int val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  136. FlightSimElapsedFrames operator + (unsigned int val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  137. FlightSimElapsedFrames operator + (long val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  138. FlightSimElapsedFrames operator + (unsigned long val) const { FlightSimElapsedFrames r(*this); r.count -= val; return r; }
  139. };
  140. extern FlightSimClass FlightSim;
  141. #if 0
  142. class usb_rawhid_class
  143. {
  144. public:
  145. int available(void);
  146. int recv(void *buffer, uint16_t timeout);
  147. int send(const void *buffer, uint16_t timeout);
  148. };
  149. extern usb_rawhid_class RawHID;
  150. #endif
  151. class usb_serial_class : public Stream
  152. {
  153. public:
  154. // standard Arduino functions
  155. void begin(long);
  156. void end();
  157. virtual int available();
  158. virtual int read();
  159. virtual int peek();
  160. virtual void flush();
  161. #if ARDUINO >= 100
  162. virtual size_t write(uint8_t);
  163. #else
  164. virtual void write(uint8_t);
  165. #endif
  166. using Print::write;
  167. operator bool();
  168. // Teensy extensions
  169. void send_now(void);
  170. uint32_t baud(void);
  171. uint8_t stopbits(void);
  172. uint8_t paritytype(void);
  173. uint8_t numbits(void);
  174. uint8_t dtr(void);
  175. uint8_t rts(void);
  176. private:
  177. uint8_t readnext(void);
  178. };
  179. extern usb_serial_class Serial;
  180. #endif