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.

UsbKeyboard.h 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Based on Obdev's AVRUSB code and under the same license.
  3. *
  4. * TODO: Make a proper file header. :-)
  5. */
  6. #ifndef __UsbKeyboard_h__
  7. #define __UsbKeyboard_h__
  8. #include <avr/pgmspace.h>
  9. #include <avr/interrupt.h>
  10. #include <string.h>
  11. #include "usbdrv.h"
  12. // TODO: Work around Arduino 12 issues better.
  13. //#include <WConstants.h>
  14. //#undef int()
  15. typedef uint8_t byte;
  16. #define BUFFER_SIZE 4 // Minimum of 2: 1 for modifiers + 1 for keystroke
  17. static uchar idleRate; // in 4 ms units
  18. /* We use a simplifed keyboard report descriptor which does not support the
  19. * boot protocol. We don't allow setting status LEDs and but we do allow
  20. * simultaneous key presses.
  21. * The report descriptor has been created with usb.org's "HID Descriptor Tool"
  22. * which can be downloaded from http://www.usb.org/developers/hidpage/.
  23. * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
  24. * for the second INPUT item.
  25. */
  26. PROGMEM const char usbHidReportDescriptor[35] = { /* USB report descriptor */
  27. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  28. 0x09, 0x06, // USAGE (Keyboard)
  29. 0xa1, 0x01, // COLLECTION (Application)
  30. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  31. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  32. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  33. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  34. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  35. 0x75, 0x01, // REPORT_SIZE (1)
  36. 0x95, 0x08, // REPORT_COUNT (8)
  37. 0x81, 0x02, // INPUT (Data,Var,Abs)
  38. 0x95, BUFFER_SIZE-1, // REPORT_COUNT (simultaneous keystrokes)
  39. 0x75, 0x08, // REPORT_SIZE (8)
  40. 0x25, 0x65, // LOGICAL_MAXIMUM (101)
  41. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  42. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  43. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  44. 0xc0 // END_COLLECTION
  45. };
  46. /* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
  47. * 10 Keyboard/Keypad Page for more codes.
  48. */
  49. #define MOD_CONTROL_LEFT (1<<0)
  50. #define MOD_SHIFT_LEFT (1<<1)
  51. #define MOD_ALT_LEFT (1<<2)
  52. #define MOD_GUI_LEFT (1<<3)
  53. #define MOD_CONTROL_RIGHT (1<<4)
  54. #define MOD_SHIFT_RIGHT (1<<5)
  55. #define MOD_ALT_RIGHT (1<<6)
  56. #define MOD_GUI_RIGHT (1<<7)
  57. #define KEY_A 4
  58. #define KEY_B 5
  59. #define KEY_C 6
  60. #define KEY_D 7
  61. #define KEY_E 8
  62. #define KEY_F 9
  63. #define KEY_G 10
  64. #define KEY_H 11
  65. #define KEY_I 12
  66. #define KEY_J 13
  67. #define KEY_K 14
  68. #define KEY_L 15
  69. #define KEY_M 16
  70. #define KEY_N 17
  71. #define KEY_O 18
  72. #define KEY_P 19
  73. #define KEY_Q 20
  74. #define KEY_R 21
  75. #define KEY_S 22
  76. #define KEY_T 23
  77. #define KEY_U 24
  78. #define KEY_V 25
  79. #define KEY_W 26
  80. #define KEY_X 27
  81. #define KEY_Y 28
  82. #define KEY_Z 29
  83. #define KEY_1 30
  84. #define KEY_2 31
  85. #define KEY_3 32
  86. #define KEY_4 33
  87. #define KEY_5 34
  88. #define KEY_6 35
  89. #define KEY_7 36
  90. #define KEY_8 37
  91. #define KEY_9 38
  92. #define KEY_0 39
  93. #define KEY_ENTER 40
  94. #define KEY_ESCAPE 41
  95. #define KEY_BSPACE 42
  96. #define KEY_TAB 43
  97. #define KEY_SPACE 44
  98. #define KEY_HYPHEN 45
  99. #define KEY_EQUAL 46
  100. #define KEY_LBRACKET 47
  101. #define KEY_RBRACKET 48
  102. #define KEY_BSLASH 49
  103. #define KEY_HASHTAG 50
  104. #define KEY_SEMICOLON 51
  105. #define KEY_QUOTE 52
  106. #define KEY_TILDE 53
  107. #define KEY_COMMA 54
  108. #define KEY_DOT 55
  109. #define KEY_SLASH 56
  110. #define KEY_CAPSLOCK 57
  111. #define KEY_F1 58
  112. #define KEY_F2 59
  113. #define KEY_F3 60
  114. #define KEY_F4 61
  115. #define KEY_F5 62
  116. #define KEY_F6 63
  117. #define KEY_F7 64
  118. #define KEY_F8 65
  119. #define KEY_F9 66
  120. #define KEY_F10 67
  121. #define KEY_F11 68
  122. #define KEY_F12 69
  123. #define KEY_PRINTSCREEN 70
  124. #define KEY_SCROLLLOCK 71
  125. #define KEY_PAUSE 72
  126. #define KEY_INSERT 73
  127. #define KEY_HOME 74
  128. #define KEY_PAGEUP 75
  129. #define KEY_DELETE 76
  130. #define KEY_END 77
  131. #define KEY_PAGEDOWN 78
  132. #define KEY_ARROW_RIGHT 79
  133. #define KEY_ARROW_LEFT 80
  134. #define KEY_ARROW_DOWN 81
  135. #define KEY_ARROW_UP 82
  136. #define KEY_NUMLOCK 83
  137. #define KEYP_SLASH 84
  138. #define KEYP_STAR 85
  139. #define KEYP_MINUS 86
  140. #define KEYP_PLUS 87
  141. #define KEYP_ENTER 88
  142. #define KEYP_NUS_BSLASH 100
  143. class UsbKeyboardDevice {
  144. public:
  145. UsbKeyboardDevice () {
  146. PORTD = 0; // TODO: Only for USB pins?
  147. DDRD |= ~USBMASK;
  148. cli();
  149. usbDeviceDisconnect();
  150. usbDeviceConnect();
  151. usbInit();
  152. sei();
  153. // TODO: Remove the next two lines once we fix
  154. // missing first keystroke bug properly.
  155. memset(reportBuffer, 0, sizeof(reportBuffer));
  156. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  157. }
  158. void update() {
  159. usbPoll();
  160. }
  161. void sendKeyStroke(byte keyStroke) {
  162. sendKeyStroke(keyStroke, 0);
  163. }
  164. void sendKeyStroke(byte keyStroke, byte modifiers) {
  165. while (!usbInterruptIsReady()) {
  166. // Note: We wait until we can send keystroke
  167. // so we know the previous keystroke was
  168. // sent.
  169. }
  170. memset(reportBuffer, 0, sizeof(reportBuffer));
  171. reportBuffer[0] = modifiers;
  172. reportBuffer[1] = keyStroke;
  173. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  174. while (!usbInterruptIsReady()) {
  175. // Note: We wait until we can send keystroke
  176. // so we know the previous keystroke was
  177. // sent.
  178. }
  179. // This stops endlessly repeating keystrokes:
  180. memset(reportBuffer, 0, sizeof(reportBuffer));
  181. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  182. }
  183. //private: TODO: Make friend?
  184. uchar reportBuffer[4]; // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]
  185. };
  186. UsbKeyboardDevice UsbKeyboard = UsbKeyboardDevice();
  187. #ifdef __cplusplus
  188. extern "C"{
  189. #endif
  190. // USB_PUBLIC uchar usbFunctionSetup
  191. uchar usbFunctionSetup(uchar data[8])
  192. {
  193. usbRequest_t *rq = (usbRequest_t *)((void *)data);
  194. usbMsgPtr = UsbKeyboard.reportBuffer; //
  195. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){
  196. /* class request type */
  197. if(rq->bRequest == USBRQ_HID_GET_REPORT){
  198. /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  199. /* we only have one report type, so don't look at wValue */
  200. // TODO: Ensure it's okay not to return anything here?
  201. return 0;
  202. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  203. // usbMsgPtr = &idleRate;
  204. // return 1;
  205. return 0;
  206. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  207. idleRate = rq->wValue.bytes[1];
  208. }
  209. }else{
  210. /* no vendor specific requests implemented */
  211. }
  212. return 0;
  213. }
  214. #ifdef __cplusplus
  215. } // extern "C"
  216. #endif
  217. #endif // __UsbKeyboard_h__