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.

211 lines
5.6KB

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