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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

USBAPI.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #ifndef __USBAPI__
  2. #define __USBAPI__
  3. #if defined(USBCON)
  4. //================================================================================
  5. //================================================================================
  6. // USB
  7. class USBDevice_
  8. {
  9. public:
  10. USBDevice_();
  11. bool configured();
  12. void attach();
  13. void detach(); // Serial port goes down too...
  14. void poll();
  15. };
  16. extern USBDevice_ USBDevice;
  17. //================================================================================
  18. //================================================================================
  19. // Serial over CDC (Serial1 is the physical port)
  20. class Serial_ : public Stream
  21. {
  22. private:
  23. ring_buffer *_cdc_rx_buffer;
  24. public:
  25. void begin(uint16_t baud_count);
  26. void end(void);
  27. virtual int available(void);
  28. virtual void accept(void);
  29. virtual int peek(void);
  30. virtual int read(void);
  31. virtual void flush(void);
  32. virtual size_t write(uint8_t);
  33. operator bool();
  34. };
  35. extern Serial_ Serial;
  36. //================================================================================
  37. //================================================================================
  38. // Mouse
  39. #define MOUSE_LEFT 1
  40. #define MOUSE_RIGHT 2
  41. #define MOUSE_MIDDLE 4
  42. #define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
  43. class Mouse_
  44. {
  45. private:
  46. uint8_t _buttons;
  47. void buttons(uint8_t b);
  48. public:
  49. Mouse_(void);
  50. void begin(void);
  51. void end(void);
  52. void click(uint8_t b = MOUSE_LEFT);
  53. void move(signed char x, signed char y, signed char wheel = 0);
  54. void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
  55. void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
  56. bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
  57. };
  58. extern Mouse_ Mouse;
  59. //================================================================================
  60. //================================================================================
  61. // Keyboard
  62. #define KEY_LEFT_CTRL 0x80
  63. #define KEY_LEFT_SHIFT 0x81
  64. #define KEY_LEFT_ALT 0x82
  65. #define KEY_LEFT_GUI 0x83
  66. #define KEY_RIGHT_CTRL 0x84
  67. #define KEY_RIGHT_SHIFT 0x85
  68. #define KEY_RIGHT_ALT 0x86
  69. #define KEY_RIGHT_GUI 0x87
  70. #define KEY_UP_ARROW 0xDA
  71. #define KEY_DOWN_ARROW 0xD9
  72. #define KEY_LEFT_ARROW 0xD8
  73. #define KEY_RIGHT_ARROW 0xD7
  74. #define KEY_BACKSPACE 0xB2
  75. #define KEY_TAB 0xB3
  76. #define KEY_RETURN 0xB0
  77. #define KEY_ESC 0xB1
  78. #define KEY_INSERT 0xD1
  79. #define KEY_DELETE 0xD4
  80. #define KEY_PAGE_UP 0xD3
  81. #define KEY_PAGE_DOWN 0xD6
  82. #define KEY_HOME 0xD2
  83. #define KEY_END 0xD5
  84. #define KEY_CAPS_LOCK 0xC1
  85. #define KEY_F1 0xC2
  86. #define KEY_F2 0xC3
  87. #define KEY_F3 0xC4
  88. #define KEY_F4 0xC5
  89. #define KEY_F5 0xC6
  90. #define KEY_F6 0xC7
  91. #define KEY_F7 0xC8
  92. #define KEY_F8 0xC9
  93. #define KEY_F9 0xCA
  94. #define KEY_F10 0xCB
  95. #define KEY_F11 0xCC
  96. #define KEY_F12 0xCD
  97. // Low level key report: up to 6 keys and shift, ctrl etc at once
  98. typedef struct
  99. {
  100. uint8_t modifiers;
  101. uint8_t reserved;
  102. uint8_t keys[6];
  103. } KeyReport;
  104. class Keyboard_ : public Print
  105. {
  106. private:
  107. KeyReport _keyReport;
  108. void sendReport(KeyReport* keys);
  109. public:
  110. Keyboard_(void);
  111. void begin(void);
  112. void end(void);
  113. virtual size_t write(uint8_t k);
  114. virtual size_t press(uint8_t k);
  115. virtual size_t release(uint8_t k);
  116. virtual void releaseAll(void);
  117. };
  118. extern Keyboard_ Keyboard;
  119. //================================================================================
  120. //================================================================================
  121. // Low level API
  122. typedef struct
  123. {
  124. uint8_t bmRequestType;
  125. uint8_t bRequest;
  126. uint8_t wValueL;
  127. uint8_t wValueH;
  128. uint16_t wIndex;
  129. uint16_t wLength;
  130. } Setup;
  131. //================================================================================
  132. //================================================================================
  133. // HID 'Driver'
  134. int HID_GetInterface(uint8_t* interfaceNum);
  135. int HID_GetDescriptor(int i);
  136. bool HID_Setup(Setup& setup);
  137. void HID_SendReport(uint8_t id, const void* data, int len);
  138. //================================================================================
  139. //================================================================================
  140. // MSC 'Driver'
  141. int MSC_GetInterface(uint8_t* interfaceNum);
  142. int MSC_GetDescriptor(int i);
  143. bool MSC_Setup(Setup& setup);
  144. bool MSC_Data(uint8_t rx,uint8_t tx);
  145. //================================================================================
  146. //================================================================================
  147. // CSC 'Driver'
  148. int CDC_GetInterface(uint8_t* interfaceNum);
  149. int CDC_GetDescriptor(int i);
  150. bool CDC_Setup(Setup& setup);
  151. //================================================================================
  152. //================================================================================
  153. #define TRANSFER_PGM 0x80
  154. #define TRANSFER_RELEASE 0x40
  155. #define TRANSFER_ZERO 0x20
  156. int USB_SendControl(uint8_t flags, const void* d, int len);
  157. int USB_RecvControl(void* d, int len);
  158. uint8_t USB_Available(uint8_t ep);
  159. int USB_Send(uint8_t ep, const void* data, int len); // blocking
  160. int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
  161. int USB_Recv(uint8_t ep); // non-blocking
  162. void USB_Flush(uint8_t ep);
  163. #endif
  164. #endif /* if defined(USBCON) */