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.

USBHIDBootKbd.ino 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <hidboot.h>
  2. #include <usbhub.h>
  3. // Satisfy the IDE, which needs to see the include statment in the ino too.
  4. #ifdef dobogusinclude
  5. #include <spi4teensy3.h>
  6. #include <SPI.h>
  7. #endif
  8. class KbdRptParser : public KeyboardReportParser
  9. {
  10. void PrintKey(uint8_t mod, uint8_t key);
  11. protected:
  12. void OnControlKeysChanged(uint8_t before, uint8_t after);
  13. void OnKeyDown (uint8_t mod, uint8_t key);
  14. void OnKeyUp (uint8_t mod, uint8_t key);
  15. void OnKeyPressed(uint8_t key);
  16. };
  17. void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
  18. {
  19. MODIFIERKEYS mod;
  20. *((uint8_t*)&mod) = m;
  21. Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
  22. Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
  23. Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
  24. Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
  25. Serial.print(" >");
  26. PrintHex<uint8_t>(key, 0x80);
  27. Serial.print("< ");
  28. Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
  29. Serial.print((mod.bmRightShift == 1) ? "S" : " ");
  30. Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
  31. Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
  32. };
  33. void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
  34. {
  35. Serial.print("DN ");
  36. PrintKey(mod, key);
  37. uint8_t c = OemToAscii(mod, key);
  38. if (c)
  39. OnKeyPressed(c);
  40. }
  41. void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
  42. MODIFIERKEYS beforeMod;
  43. *((uint8_t*)&beforeMod) = before;
  44. MODIFIERKEYS afterMod;
  45. *((uint8_t*)&afterMod) = after;
  46. if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
  47. Serial.println("LeftCtrl changed");
  48. }
  49. if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
  50. Serial.println("LeftShift changed");
  51. }
  52. if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
  53. Serial.println("LeftAlt changed");
  54. }
  55. if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
  56. Serial.println("LeftGUI changed");
  57. }
  58. if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
  59. Serial.println("RightCtrl changed");
  60. }
  61. if (beforeMod.bmRightShift != afterMod.bmRightShift) {
  62. Serial.println("RightShift changed");
  63. }
  64. if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
  65. Serial.println("RightAlt changed");
  66. }
  67. if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
  68. Serial.println("RightGUI changed");
  69. }
  70. }
  71. void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
  72. {
  73. Serial.print("UP ");
  74. PrintKey(mod, key);
  75. }
  76. void KbdRptParser::OnKeyPressed(uint8_t key)
  77. {
  78. Serial.print("ASCII: ");
  79. Serial.println((char)key);
  80. };
  81. USB Usb;
  82. //USBHub Hub(&Usb);
  83. HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
  84. uint32_t next_time;
  85. KbdRptParser Prs;
  86. void setup()
  87. {
  88. Serial.begin( 115200 );
  89. #if !defined(__MIPSEL__)
  90. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  91. #endif
  92. Serial.println("Start");
  93. if (Usb.Init() == -1)
  94. Serial.println("OSC did not start.");
  95. delay( 200 );
  96. next_time = millis() + 5000;
  97. HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs);
  98. }
  99. void loop()
  100. {
  101. Usb.Task();
  102. }