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
7.0KB

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