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.hxx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //
  2. // Created by robin on 1/8/16.
  3. //
  4. UsbKeyboardDevice UsbKeyboard = UsbKeyboardDevice("us");
  5. /* We use a simplifed keyboard report descriptor which does not support the
  6. * boot protocol. We don't allow setting status LEDs and but we do allow
  7. * simultaneous key presses.
  8. * The report descriptor has been created with usb.org's "HID Descriptor Tool"
  9. * which can be downloaded from http://www.usb.org/developers/hidpage/.
  10. * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
  11. * for the second INPUT item.
  12. */
  13. PROGMEM const char usbHidReportDescriptor[35] = { /* USB report descriptor */
  14. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  15. 0x09, 0x06, // USAGE (Keyboard)
  16. 0xa1, 0x01, // COLLECTION (Application)
  17. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  18. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  19. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  20. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  21. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  22. 0x75, 0x01, // REPORT_SIZE (1)
  23. 0x95, 0x08, // REPORT_COUNT (8)
  24. 0x81, 0x02, // INPUT (Data,Var,Abs)
  25. 0x95, BUFFER_SIZE-1, // REPORT_COUNT (simultaneous keystrokes)
  26. 0x75, 0x08, // REPORT_SIZE (8)
  27. 0x25, 0x65, // LOGICAL_MAXIMUM (101)
  28. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  29. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  30. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  31. 0xc0 // END_COLLECTION
  32. };
  33. UsbKeyboardDevice::UsbKeyboardDevice(const char* layout)
  34. : _layout(0)
  35. {
  36. PORTD = 0; // TODO: Only for USB pins?
  37. DDRD |= ~USBMASK;
  38. cli();
  39. usbDeviceDisconnect();
  40. usbDeviceConnect();
  41. usbInit();
  42. sei();
  43. // TODO: Remove the next two lines once we fix
  44. // missing first keystroke bug properly.
  45. memset(reportBuffer, 0, sizeof(reportBuffer));
  46. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  47. setLayout("us");
  48. }
  49. bool UsbKeyboardDevice::isUsbReady()
  50. {
  51. UsbKeyboard.update();
  52. return usbInterruptIsReady();
  53. }
  54. void UsbKeyboardDevice::setLayout(const char* layout)
  55. {
  56. delete _layout;
  57. _layout = new char[strlen(layout)];
  58. strcpy(_layout, layout);
  59. }
  60. int UsbKeyboardDevice::charModAltGr(char c, int* modifier)
  61. {
  62. *modifier = MOD_ALT_RIGHT;
  63. return c;
  64. }
  65. int UsbKeyboardDevice::charModShift(char c, int* modifier)
  66. {
  67. *modifier = MOD_SHIFT_LEFT;
  68. return c;
  69. }
  70. #ifdef ARD_USBKBD_AZERTY
  71. int UsbKeyboardDevice::charToKeyAzerty(int c, int* modifier)
  72. {
  73. *modifier = 0;
  74. if (c == '0')
  75. return charModShift(KEY_0, modifier);
  76. if (c >= '1' && c <= '9')
  77. return charModShift(KEY_1 + c - '1', modifier);
  78. if (c == '\n')
  79. return KEY_ENTER;
  80. if (c == ' ')
  81. return KEY_SPACE;
  82. if (c >= 'a' && c <= 'z')
  83. {
  84. if (c == 'a')
  85. c = 'q';
  86. else if (c == 'm')
  87. return KEY_SEMICOLON;
  88. else if (c == 'q')
  89. c = 'a';
  90. else if (c == 'w')
  91. c = 'z';
  92. else if (c == 'z')
  93. c = 'w';
  94. return KEY_A + c - 'a';
  95. }
  96. if (c >= 'A' && c <= 'Z')
  97. {
  98. if (c == 'A')
  99. c = 'Q';
  100. else if (c == 'M')
  101. return charModShift(KEY_SEMICOLON, modifier);
  102. else if (c == 'Q')
  103. c = 'A';
  104. else if (c == 'W')
  105. c = 'Z';
  106. else if (c == 'Z')
  107. c = 'W';
  108. return charModShift(KEY_A + c - 'A', modifier);
  109. }
  110. if (c == '~')
  111. return charModAltGr(KEY_2, modifier);
  112. if (c == '#')
  113. return charModAltGr(KEY_3, modifier);
  114. if (c == '{')
  115. return charModAltGr(KEY_4, modifier);
  116. if (c == '[')
  117. return charModAltGr(KEY_5, modifier);
  118. if (c == '|')
  119. return charModAltGr(KEY_6, modifier);
  120. if (c == '`')
  121. return charModAltGr(KEY_7, modifier);
  122. if (c == '\\')
  123. return charModAltGr(KEY_8, modifier);
  124. if (c == '^')
  125. return charModAltGr(KEY_9, modifier);
  126. if (c == '@')
  127. return charModAltGr(KEY_0, modifier);
  128. if (c == ']')
  129. return charModAltGr(KEY_HYPHEN, modifier);
  130. if (c == '}')
  131. return charModAltGr(KEY_EQUAL, modifier);
  132. if (c == -75)//µ
  133. return charModShift(KEY_BSLASH, modifier);
  134. if (c == '*')
  135. return KEY_BSLASH;
  136. if (c == '&')
  137. return KEY_1;
  138. if (c == -23)//é
  139. return KEY_2;
  140. if (c == '\"')
  141. return KEY_3;
  142. if (c == '\'')
  143. return KEY_4;
  144. if (c == '(')
  145. return KEY_5;
  146. if (c == '-')
  147. return KEY_6;
  148. if (c == -24)//è
  149. return KEY_7;
  150. if (c == '_')
  151. return KEY_8;
  152. if (c == -25)//ç
  153. return KEY_9;
  154. if (c == -32)//à
  155. return KEY_0;
  156. if (c == ')')
  157. return KEY_HYPHEN;
  158. if (c == '=')
  159. return KEY_EQUAL;
  160. if (c == '+')
  161. return charModShift(KEY_EQUAL, modifier);
  162. if (c == ',')
  163. return KEY_M;
  164. if (c == ';')
  165. return KEY_COMMA;
  166. if (c == ':')
  167. return KEY_DOT;
  168. if (c == '!')
  169. return KEY_SLASH;
  170. if (c == -7)//ù
  171. return KEY_QUOTE;
  172. if (c == '<')
  173. return KEYP_NUS_BSLASH;
  174. if (c == '?')
  175. return charModShift(KEY_M, modifier);
  176. if (c == '.')
  177. return charModShift(KEY_COMMA, modifier);
  178. if (c == '/')
  179. return charModShift(KEY_DOT, modifier);
  180. if (c == -89)//§
  181. return charModShift(KEY_SLASH, modifier);
  182. if (c == '%')
  183. return charModShift(KEY_QUOTE, modifier);
  184. if (c == '>')
  185. return charModShift(KEYP_NUS_BSLASH, modifier);
  186. return 0;
  187. }
  188. #endif
  189. #ifdef ARD_USBKBD_QWERTY
  190. int UsbKeyboardDevice::charToKeyQwerty(int c, int* modifier)
  191. {
  192. *modifier = 0;
  193. if (c == '0')
  194. return KEY_0;
  195. if (c >= '1' && c <= '9')
  196. return KEY_1 + c - '1';
  197. if (c >= 'a' && c <= 'z')
  198. return KEY_A + c - 'a';
  199. if (c >= 'A' && c <= 'Z')
  200. return charModShift(KEY_A + c - 'A', modifier);
  201. if (c == '\n')
  202. return KEY_ENTER;
  203. if (c == ' ')
  204. return KEY_SPACE;
  205. if (c == '.')
  206. return KEY_DOT;
  207. if (c == ',')
  208. return KEY_COMMA;
  209. if (c == '/')
  210. return KEY_SLASH;
  211. if (c == ';')
  212. return KEY_SEMICOLON;
  213. if (c == '`')
  214. return KEY_TILDE;
  215. if (c == '-')
  216. return KEY_HYPHEN;
  217. if (c == '=')
  218. return KEY_EQUAL;
  219. if (c == '\'')
  220. return KEY_QUOTE;
  221. if (c == '~')
  222. return charModShift(KEY_TILDE, modifier);
  223. if (c == '<')
  224. return charModShift(KEY_COMMA, modifier);
  225. if (c == '>')
  226. return charModShift(KEY_DOT, modifier);
  227. if (c == '?')
  228. return charModShift(KEY_SLASH, modifier);
  229. if (c == ':')
  230. return charModShift(KEY_SEMICOLON, modifier);
  231. if (c == '!')
  232. return charModShift(KEY_1, modifier);
  233. if (c == '@')
  234. return charModShift(KEY_2, modifier);
  235. if (c == '#')
  236. return charModShift(KEY_3, modifier);
  237. if (c == '$')
  238. return charModShift(KEY_4, modifier);
  239. if (c == '%')
  240. return charModShift(KEY_5, modifier);
  241. if (c == '^')
  242. return charModShift(KEY_6, modifier);
  243. if (c == '&')
  244. return charModShift(KEY_7, modifier);
  245. if (c == '*')
  246. return charModShift(KEY_8, modifier);
  247. if (c == '(')
  248. return charModShift(KEY_9, modifier);
  249. if (c == ')')
  250. return charModShift(KEY_0, modifier);
  251. if (c == '_')
  252. return charModShift(KEY_HYPHEN, modifier);
  253. if (c == '+')
  254. return charModShift(KEY_EQUAL, modifier);
  255. if (c == '"')
  256. return charModShift(KEY_QUOTE, modifier);
  257. if (c == '{')
  258. return charModShift(KEY_LBRACKET, modifier);
  259. if (c == '}')
  260. return charModShift(KEY_RBRACKET, modifier);
  261. if (c == '|')
  262. return charModShift(KEY_BSLASH, modifier);
  263. if (c == '[')
  264. return KEY_LBRACKET;
  265. if (c == ']')
  266. return KEY_RBRACKET;
  267. if (c == '\\')
  268. return KEY_BSLASH;
  269. return 0;
  270. }
  271. #endif
  272. void UsbKeyboardDevice::sendKeyStrokes(const char* str)
  273. {
  274. int (*charToKey)(int,int*) = 0;
  275. #ifdef ARD_USBKBD_AZERTY
  276. if (!strcmp(_layout, "fr"))
  277. {
  278. charToKey = charToKeyAzerty;
  279. }
  280. #endif
  281. #ifdef ARD_USBKBD_QWERTY
  282. if (!strcmp(_layout, "us"))
  283. {
  284. charToKey = charToKeyQwerty;
  285. }
  286. #endif
  287. if (!charToKey)
  288. {
  289. return;
  290. }
  291. while (*str)
  292. {
  293. int modifier = 0;
  294. int k = charToKey(*str, &modifier);
  295. /*if (!k)
  296. {
  297. k = KEY_SPACE;
  298. char buf[12];
  299. itoa((unsigned)*str, buf, 10);
  300. sendKeyStrokes(buf);
  301. }*/
  302. if (k)
  303. {
  304. UsbKeyboard.sendKeyStroke(k, modifier);
  305. }
  306. ++str;
  307. }
  308. }
  309. void UsbKeyboardDevice::sendKeyStroke(byte keyStroke, byte modifiers)
  310. {
  311. while (!usbInterruptIsReady()) {
  312. // Note: We wait until we can send keystroke
  313. // so we know the previous keystroke was
  314. // sent.
  315. }
  316. memset(reportBuffer, 0, sizeof(reportBuffer));
  317. reportBuffer[0] = modifiers;
  318. reportBuffer[1] = keyStroke;
  319. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  320. while (!usbInterruptIsReady()) {
  321. // Note: We wait until we can send keystroke
  322. // so we know the previous keystroke was
  323. // sent.
  324. }
  325. // This stops endlessly repeating keystrokes:
  326. memset(reportBuffer, 0, sizeof(reportBuffer));
  327. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  328. }
  329. uchar usbFunctionSetup(uchar data[8])
  330. {
  331. usbRequest_t *rq = (usbRequest_t *)((void *)data);
  332. usbMsgPtr = UsbKeyboard.reportBuffer; //
  333. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){
  334. /* class request type */
  335. if(rq->bRequest == USBRQ_HID_GET_REPORT){
  336. /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  337. /* we only have one report type, so don't look at wValue */
  338. // TODO: Ensure it's okay not to return anything here?
  339. return 0;
  340. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  341. // usbMsgPtr = &idleRate;
  342. // return 1;
  343. return 0;
  344. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  345. idleRate = rq->wValue.bytes[1];
  346. }
  347. }else{
  348. /* no vendor specific requests implemented */
  349. }
  350. return 0;
  351. }