Teensy 4.1 core updated for C++20
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.

212 lines
5.8KB

  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(uint8_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_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
  56. class usb_mouse_class
  57. {
  58. public:
  59. void begin(void) { }
  60. void end(void) { }
  61. void move(int8_t x, int8_t y, int8_t wheel=0);
  62. void click(uint8_t b = MOUSE_LEFT);
  63. void scroll(int8_t wheel);
  64. void set_buttons(uint8_t left, uint8_t middle=0, uint8_t right=0);
  65. void press(uint8_t b = MOUSE_LEFT);
  66. void release(uint8_t b = MOUSE_LEFT);
  67. bool isPressed(uint8_t b = MOUSE_ALL);
  68. };
  69. extern usb_mouse_class Mouse;
  70. extern uint8_t joystick_report_data[12];
  71. class usb_joystick_class
  72. {
  73. public:
  74. usb_joystick_class() { manual_mode = 0; }
  75. inline void button(uint8_t button, bool val) {
  76. button--;
  77. uint8_t mask = (1 << (button & 7));
  78. if (val) {
  79. if (button < 8) joystick_report_data[0] |= mask;
  80. else if (button < 16) joystick_report_data[1] |= mask;
  81. else if (button < 24) joystick_report_data[2] |= mask;
  82. else if (button < 32) joystick_report_data[3] |= mask;
  83. } else {
  84. mask = ~mask;
  85. if (button < 8) joystick_report_data[0] &= mask;
  86. else if (button < 16) joystick_report_data[1] &= mask;
  87. else if (button < 24) joystick_report_data[2] &= mask;
  88. else if (button < 32) joystick_report_data[3] &= mask;
  89. }
  90. if (!manual_mode) send_now();
  91. }
  92. inline void X(uint16_t val) {
  93. if (val > 1023) val = 1023;
  94. joystick_report_data[4] = (joystick_report_data[4] & 0x0F) | (val << 4);
  95. joystick_report_data[5] = (joystick_report_data[5] & 0xC0) | (val >> 4);
  96. if (!manual_mode) send_now();
  97. }
  98. inline void Y(uint16_t val) {
  99. if (val > 1023) val = 1023;
  100. joystick_report_data[5] = (joystick_report_data[5] & 0x3F) | (val << 6);
  101. joystick_report_data[6] = (val >> 2);
  102. if (!manual_mode) send_now();
  103. }
  104. inline void position(uint16_t x, uint16_t y) {
  105. if (x > 1023) x = 1023;
  106. if (y > 1023) y = 1023;
  107. joystick_report_data[4] = (joystick_report_data[4] & 0x0F) | (x << 4);
  108. joystick_report_data[5] = (x >> 4) | (y << 6);
  109. joystick_report_data[6] = (y >> 2);
  110. if (!manual_mode) send_now();
  111. }
  112. inline void Z(uint16_t val) {
  113. if (val > 1023) val = 1023;
  114. joystick_report_data[7] = val;
  115. joystick_report_data[8] = (joystick_report_data[8] & 0xFC) | (val >> 8);
  116. if (!manual_mode) send_now();
  117. }
  118. inline void Zrotate(uint16_t val) {
  119. if (val > 1023) val = 1023;
  120. joystick_report_data[8] = (joystick_report_data[8] & 0x03) | (val << 2);
  121. joystick_report_data[9] = (joystick_report_data[9] & 0xF0) | (val >> 6);
  122. if (!manual_mode) send_now();
  123. }
  124. inline void sliderLeft(uint16_t val) {
  125. if (val > 1023) val = 1023;
  126. joystick_report_data[9] = (joystick_report_data[9] & 0x0F) | (val << 4);
  127. joystick_report_data[10] = (joystick_report_data[10] & 0xC0) | (val >> 4);
  128. if (!manual_mode) send_now();
  129. }
  130. inline void sliderRight(uint16_t val) {
  131. if (val > 1023) val = 1023;
  132. joystick_report_data[10] = (joystick_report_data[10] & 0x3F) | (val << 6);
  133. joystick_report_data[11] = (val >> 2);
  134. if (!manual_mode) send_now();
  135. }
  136. inline void slider(uint16_t val) {
  137. if (val > 1023) val = 1023;
  138. joystick_report_data[9] = (joystick_report_data[9] & 0x0F) | (val << 4);
  139. joystick_report_data[10] = (val >> 4) | (val << 6);
  140. joystick_report_data[11] = (val >> 2);
  141. if (!manual_mode) send_now();
  142. }
  143. inline void hat(int16_t dir) {
  144. uint8_t val;
  145. if (dir < 0) val = 15;
  146. else if (dir < 23) val = 0;
  147. else if (dir < 68) val = 1;
  148. else if (dir < 113) val = 2;
  149. else if (dir < 158) val = 3;
  150. else if (dir < 203) val = 4;
  151. else if (dir < 245) val = 5;
  152. else if (dir < 293) val = 6;
  153. else if (dir < 338) val = 7;
  154. joystick_report_data[4] = (joystick_report_data[4] & 0xF0) | val;
  155. if (!manual_mode) send_now();
  156. }
  157. inline void useManualSend(bool mode) {
  158. manual_mode = mode;
  159. }
  160. void send_now(void);
  161. private:
  162. //static uint8_t manual_mode;
  163. uint8_t manual_mode;
  164. };
  165. extern usb_joystick_class Joystick;
  166. class usb_serial_class : public Stream
  167. {
  168. public:
  169. // standard Arduino functions
  170. void begin(long);
  171. void end();
  172. virtual int available();
  173. virtual int read();
  174. virtual int peek();
  175. virtual void flush();
  176. virtual size_t write(uint8_t);
  177. using Print::write;
  178. operator bool();
  179. // Teensy extensions
  180. void send_now(void);
  181. uint32_t baud(void);
  182. uint8_t stopbits(void);
  183. uint8_t paritytype(void);
  184. uint8_t numbits(void);
  185. uint8_t dtr(void);
  186. uint8_t rts(void);
  187. private:
  188. uint8_t readnext(void);
  189. };
  190. extern usb_serial_class Serial;
  191. #endif