PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

214 行
6.0KB

  1. #ifndef USBserial_h_
  2. #define USBserial_h_
  3. #include <inttypes.h>
  4. #include "keylayouts.h"
  5. #include "Print.h"
  6. #include "Stream.h"
  7. class usb_keyboard_class : public Print
  8. {
  9. public:
  10. void begin(void) { }
  11. void end(void) { }
  12. virtual size_t write(uint8_t);
  13. using Print::write;
  14. void write_unicode(uint16_t unicode) { write_keycode(unicode_to_keycode(unicode)); }
  15. void set_modifier(uint16_t);
  16. void set_key1(uint8_t);
  17. void set_key2(uint8_t);
  18. void set_key3(uint8_t);
  19. void set_key4(uint8_t);
  20. void set_key5(uint8_t);
  21. void set_key6(uint8_t);
  22. void set_media(uint16_t c) {
  23. if (c == 0) {
  24. keymedia_release_all();
  25. } else if (c >= 0xE400 && c <= 0xE7FF) {
  26. press(c);
  27. }
  28. }
  29. void send_now(void);
  30. void press(uint16_t n);
  31. void release(uint16_t n);
  32. void releaseAll(void);
  33. private:
  34. KEYCODE_TYPE unicode_to_keycode(uint16_t unicode);
  35. KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode);
  36. uint8_t keycode_to_modifier(KEYCODE_TYPE keycode);
  37. uint8_t keycode_to_key(KEYCODE_TYPE keycode);
  38. void presskey(uint8_t key, uint8_t modifier);
  39. void releasekey(uint8_t key, uint8_t modifier);
  40. void write_keycode(KEYCODE_TYPE key);
  41. void write_key(KEYCODE_TYPE code);
  42. void press_consumer_key(uint16_t key);
  43. void release_consumer_key(uint16_t key);
  44. void press_system_key(uint8_t key);
  45. void release_system_key(uint8_t key);
  46. void keymedia_release_all(void);
  47. void keymedia_send(void);
  48. uint8_t utf8_state;
  49. uint16_t unicode_wchar;
  50. };
  51. extern usb_keyboard_class Keyboard;
  52. #define MOUSE_LEFT 1
  53. #define MOUSE_MIDDLE 4
  54. #define MOUSE_RIGHT 2
  55. #define MOUSE_BACK 8
  56. #define MOUSE_FORWARD 16
  57. #define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE | MOUSE_BACK | MOUSE_FORWARD)
  58. class usb_mouse_class
  59. {
  60. public:
  61. void begin(void) { }
  62. void end(void) { }
  63. void move(int8_t x, int8_t y, int8_t wheel=0, int8_t horiz=0);
  64. void click(uint8_t b = MOUSE_LEFT);
  65. void scroll(int8_t wheel, int8_t horiz=0);
  66. void set_buttons(uint8_t left, uint8_t middle=0, uint8_t right=0, uint8_t back=0, uint8_t forward=0);
  67. void press(uint8_t b = MOUSE_LEFT);
  68. void release(uint8_t b = MOUSE_LEFT);
  69. bool isPressed(uint8_t b = MOUSE_ALL);
  70. };
  71. extern usb_mouse_class Mouse;
  72. extern uint8_t joystick_report_data[12];
  73. class usb_joystick_class
  74. {
  75. public:
  76. usb_joystick_class() { manual_mode = 0; }
  77. inline void button(uint8_t button, bool val) {
  78. button--;
  79. uint8_t mask = (1 << (button & 7));
  80. if (val) {
  81. if (button < 8) joystick_report_data[0] |= mask;
  82. else if (button < 16) joystick_report_data[1] |= mask;
  83. else if (button < 24) joystick_report_data[2] |= mask;
  84. else if (button < 32) joystick_report_data[3] |= mask;
  85. } else {
  86. mask = ~mask;
  87. if (button < 8) joystick_report_data[0] &= mask;
  88. else if (button < 16) joystick_report_data[1] &= mask;
  89. else if (button < 24) joystick_report_data[2] &= mask;
  90. else if (button < 32) joystick_report_data[3] &= mask;
  91. }
  92. if (!manual_mode) send_now();
  93. }
  94. inline void X(uint16_t val) {
  95. if (val > 1023) val = 1023;
  96. joystick_report_data[4] = (joystick_report_data[4] & 0x0F) | (val << 4);
  97. joystick_report_data[5] = (joystick_report_data[5] & 0xC0) | (val >> 4);
  98. if (!manual_mode) send_now();
  99. }
  100. inline void Y(uint16_t val) {
  101. if (val > 1023) val = 1023;
  102. joystick_report_data[5] = (joystick_report_data[5] & 0x3F) | (val << 6);
  103. joystick_report_data[6] = (val >> 2);
  104. if (!manual_mode) send_now();
  105. }
  106. inline void position(uint16_t x, uint16_t y) {
  107. if (x > 1023) x = 1023;
  108. if (y > 1023) y = 1023;
  109. joystick_report_data[4] = (joystick_report_data[4] & 0x0F) | (x << 4);
  110. joystick_report_data[5] = (x >> 4) | (y << 6);
  111. joystick_report_data[6] = (y >> 2);
  112. if (!manual_mode) send_now();
  113. }
  114. inline void Z(uint16_t val) {
  115. if (val > 1023) val = 1023;
  116. joystick_report_data[7] = val;
  117. joystick_report_data[8] = (joystick_report_data[8] & 0xFC) | (val >> 8);
  118. if (!manual_mode) send_now();
  119. }
  120. inline void Zrotate(uint16_t val) {
  121. if (val > 1023) val = 1023;
  122. joystick_report_data[8] = (joystick_report_data[8] & 0x03) | (val << 2);
  123. joystick_report_data[9] = (joystick_report_data[9] & 0xF0) | (val >> 6);
  124. if (!manual_mode) send_now();
  125. }
  126. inline void sliderLeft(uint16_t val) {
  127. if (val > 1023) val = 1023;
  128. joystick_report_data[9] = (joystick_report_data[9] & 0x0F) | (val << 4);
  129. joystick_report_data[10] = (joystick_report_data[10] & 0xC0) | (val >> 4);
  130. if (!manual_mode) send_now();
  131. }
  132. inline void sliderRight(uint16_t val) {
  133. if (val > 1023) val = 1023;
  134. joystick_report_data[10] = (joystick_report_data[10] & 0x3F) | (val << 6);
  135. joystick_report_data[11] = (val >> 2);
  136. if (!manual_mode) send_now();
  137. }
  138. inline void slider(uint16_t val) {
  139. if (val > 1023) val = 1023;
  140. joystick_report_data[9] = (joystick_report_data[9] & 0x0F) | (val << 4);
  141. joystick_report_data[10] = (val >> 4) | (val << 6);
  142. joystick_report_data[11] = (val >> 2);
  143. if (!manual_mode) send_now();
  144. }
  145. inline void hat(int16_t dir) {
  146. uint8_t val;
  147. if (dir < 0) val = 15;
  148. else if (dir < 23) val = 0;
  149. else if (dir < 68) val = 1;
  150. else if (dir < 113) val = 2;
  151. else if (dir < 158) val = 3;
  152. else if (dir < 203) val = 4;
  153. else if (dir < 245) val = 5;
  154. else if (dir < 293) val = 6;
  155. else if (dir < 338) val = 7;
  156. joystick_report_data[4] = (joystick_report_data[4] & 0xF0) | val;
  157. if (!manual_mode) send_now();
  158. }
  159. inline void useManualSend(bool mode) {
  160. manual_mode = mode;
  161. }
  162. void send_now(void);
  163. private:
  164. //static uint8_t manual_mode;
  165. uint8_t manual_mode;
  166. };
  167. extern usb_joystick_class Joystick;
  168. class usb_serial_class : public Stream
  169. {
  170. public:
  171. // standard Arduino functions
  172. void begin(long);
  173. void end();
  174. virtual int available();
  175. virtual int read();
  176. virtual int peek();
  177. virtual void flush();
  178. virtual size_t write(uint8_t);
  179. using Print::write;
  180. operator bool();
  181. // Teensy extensions
  182. void send_now(void);
  183. uint32_t baud(void);
  184. uint8_t stopbits(void);
  185. uint8_t paritytype(void);
  186. uint8_t numbits(void);
  187. uint8_t dtr(void);
  188. uint8_t rts(void);
  189. private:
  190. uint8_t readnext(void);
  191. };
  192. extern usb_serial_class Serial;
  193. #endif