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.

usb_api.h 6.9KB

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