UsbKeyboard.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 7 // 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. const PROGMEM 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. typedef struct
  47. {
  48. uint8_t modifiers;
  49. uint8_t keys[6];
  50. } KeyReport;
  51. /* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
  52. * 10 Keyboard/Keypad Page for more codes.
  53. */
  54. #define MOD_CONTROL_LEFT (1<<0)
  55. #define MOD_SHIFT_LEFT (1<<1)
  56. #define MOD_ALT_LEFT (1<<2)
  57. #define MOD_GUI_LEFT (1<<3)
  58. #define MOD_CONTROL_RIGHT (1<<4)
  59. #define MOD_SHIFT_RIGHT (1<<5)
  60. #define MOD_ALT_RIGHT (1<<6)
  61. #define MOD_GUI_RIGHT (1<<7)
  62. #define KEY_A 4
  63. #define KEY_B 5
  64. #define KEY_C 6
  65. #define KEY_D 7
  66. #define KEY_E 8
  67. #define KEY_F 9
  68. #define KEY_G 10
  69. #define KEY_H 11
  70. #define KEY_I 12
  71. #define KEY_J 13
  72. #define KEY_K 14
  73. #define KEY_L 15
  74. #define KEY_M 16
  75. #define KEY_N 17
  76. #define KEY_O 18
  77. #define KEY_P 19
  78. #define KEY_Q 20
  79. #define KEY_R 21
  80. #define KEY_S 22
  81. #define KEY_T 23
  82. #define KEY_U 24
  83. #define KEY_V 25
  84. #define KEY_W 26
  85. #define KEY_X 27
  86. #define KEY_Y 28
  87. #define KEY_Z 29
  88. #define KEY_1 30
  89. #define KEY_2 31
  90. #define KEY_3 32
  91. #define KEY_4 33
  92. #define KEY_5 34
  93. #define KEY_6 35
  94. #define KEY_7 36
  95. #define KEY_8 37
  96. #define KEY_9 38
  97. #define KEY_0 39
  98. #define KEY_ENTER 40
  99. #define KEY_SPACE 44
  100. #define KEY_F1 58
  101. #define KEY_F2 59
  102. #define KEY_F3 60
  103. #define KEY_F4 61
  104. #define KEY_F5 62
  105. #define KEY_F6 63
  106. #define KEY_F7 64
  107. #define KEY_F8 65
  108. #define KEY_F9 66
  109. #define KEY_F10 67
  110. #define KEY_F11 68
  111. #define KEY_F12 69
  112. #define KEY_ARROW_LEFT 0x50
  113. class UsbKeyboardDevice {
  114. public:
  115. UsbKeyboardDevice () {
  116. PORTD = 0; // TODO: Only for USB pins?
  117. DDRD |= ~USBMASK;
  118. cli();
  119. usbDeviceDisconnect();
  120. _delay_ms(250);
  121. usbDeviceConnect();
  122. usbInit();
  123. sei();
  124. // TODO: Remove the next two lines once we fix
  125. // missing first keystroke bug properly.
  126. memset(reportBuffer, 0, sizeof(reportBuffer));
  127. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  128. }
  129. void update() {
  130. usbPoll();
  131. }
  132. void sendKeyStroke(byte keyStroke) {
  133. sendKeyStroke(keyStroke, 0);
  134. }
  135. void sendKeyStroke(byte keyStroke, byte modifiers) {
  136. while (!usbInterruptIsReady()) {
  137. // Note: We wait until we can send keystroke
  138. // so we know the previous keystroke was
  139. // sent.
  140. }
  141. memset(reportBuffer, 0, sizeof(reportBuffer));
  142. reportBuffer[0] = modifiers;
  143. reportBuffer[1] = keyStroke;
  144. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  145. while (!usbInterruptIsReady()) {
  146. // Note: We wait until we can send keystroke
  147. // so we know the previous keystroke was
  148. // sent.
  149. }
  150. // This stops endlessly repeating keystrokes:
  151. memset(reportBuffer, 0, sizeof(reportBuffer));
  152. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  153. }
  154. size_t press(uint8_t k)
  155. {
  156. uint8_t i, j;
  157. // Add k to the key report only if it's not already present
  158. // and if there is an empty slot.
  159. for(j = 0; j < sizeof(_keyReport.keys); j++) {
  160. if(_keyReport.keys[j] == k) {
  161. break;
  162. }
  163. }
  164. if (j == sizeof(_keyReport.keys)) {
  165. for (i = 0; i < sizeof(_keyReport.keys); i++) {
  166. if (_keyReport.keys[i] == 0x00) {
  167. _keyReport.keys[i] = k;
  168. break;
  169. }
  170. }
  171. if (i == sizeof(_keyReport.keys)) {
  172. return 0;
  173. }
  174. }
  175. usbSetInterrupt((uint8_t*)&_keyReport, sizeof(_keyReport));
  176. return 1;
  177. }
  178. size_t release(uint8_t k)
  179. {
  180. uint8_t i;
  181. // Test the key report to see if k is present. Clear it if it exists.
  182. // Check all positions in case the key is present more than once (which it shouldn't be)
  183. for (i = 0; i < sizeof(_keyReport.keys); i++) {
  184. if (0 != k && _keyReport.keys[i] == k) {
  185. _keyReport.keys[i] = 0x00;
  186. }
  187. }
  188. usbSetInterrupt((uint8_t*)&_keyReport, sizeof(_keyReport));
  189. return 1;
  190. }
  191. KeyReport _keyReport;
  192. //private: TODO: Make friend?
  193. uchar reportBuffer[4]; // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]
  194. };
  195. UsbKeyboardDevice UsbKeyboard = UsbKeyboardDevice();
  196. #ifdef __cplusplus
  197. extern "C"{
  198. #endif
  199. // USB_PUBLIC uchar usbFunctionSetup
  200. uchar usbFunctionSetup(uchar data[8])
  201. {
  202. usbRequest_t *rq = (usbRequest_t *)((void *)data);
  203. usbMsgPtr = UsbKeyboard.reportBuffer; //
  204. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){
  205. /* class request type */
  206. if(rq->bRequest == USBRQ_HID_GET_REPORT){
  207. /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  208. /* we only have one report type, so don't look at wValue */
  209. // TODO: Ensure it's okay not to return anything here?
  210. return 0;
  211. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  212. // usbMsgPtr = &idleRate;
  213. // return 1;
  214. return 0;
  215. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  216. idleRate = rq->wValue.bytes[1];
  217. }
  218. }else{
  219. /* no vendor specific requests implemented */
  220. }
  221. return 0;
  222. }
  223. #ifdef __cplusplus
  224. } // extern "C"
  225. #endif
  226. #endif // __UsbKeyboard_h__