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.

USBHIDBootKbdAndMouse.ino 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <hidboot.h>
  2. #include <usbhub.h>
  3. // Satisfy IDE, which only needs to see the include statment in the ino.
  4. #ifdef dobogusinclude
  5. #include <spi4teensy3.h>
  6. #include <SPI.h>
  7. #endif
  8. class MouseRptParser : public MouseReportParser
  9. {
  10. protected:
  11. void OnMouseMove(MOUSEINFO *mi);
  12. void OnLeftButtonUp(MOUSEINFO *mi);
  13. void OnLeftButtonDown(MOUSEINFO *mi);
  14. void OnRightButtonUp(MOUSEINFO *mi);
  15. void OnRightButtonDown(MOUSEINFO *mi);
  16. void OnMiddleButtonUp(MOUSEINFO *mi);
  17. void OnMiddleButtonDown(MOUSEINFO *mi);
  18. };
  19. void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
  20. {
  21. Serial.print("dx=");
  22. Serial.print(mi->dX, DEC);
  23. Serial.print(" dy=");
  24. Serial.println(mi->dY, DEC);
  25. };
  26. void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
  27. {
  28. Serial.println("L Butt Up");
  29. };
  30. void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
  31. {
  32. Serial.println("L Butt Dn");
  33. };
  34. void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
  35. {
  36. Serial.println("R Butt Up");
  37. };
  38. void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
  39. {
  40. Serial.println("R Butt Dn");
  41. };
  42. void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)
  43. {
  44. Serial.println("M Butt Up");
  45. };
  46. void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
  47. {
  48. Serial.println("M Butt Dn");
  49. };
  50. class KbdRptParser : public KeyboardReportParser
  51. {
  52. void PrintKey(uint8_t mod, uint8_t key);
  53. protected:
  54. void OnControlKeysChanged(uint8_t before, uint8_t after);
  55. void OnKeyDown (uint8_t mod, uint8_t key);
  56. void OnKeyUp (uint8_t mod, uint8_t key);
  57. void OnKeyPressed(uint8_t key);
  58. };
  59. void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
  60. {
  61. MODIFIERKEYS mod;
  62. *((uint8_t*)&mod) = m;
  63. Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
  64. Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
  65. Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
  66. Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
  67. Serial.print(" >");
  68. PrintHex<uint8_t>(key, 0x80);
  69. Serial.print("< ");
  70. Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
  71. Serial.print((mod.bmRightShift == 1) ? "S" : " ");
  72. Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
  73. Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
  74. };
  75. void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
  76. {
  77. Serial.print("DN ");
  78. PrintKey(mod, key);
  79. uint8_t c = OemToAscii(mod, key);
  80. if (c)
  81. OnKeyPressed(c);
  82. }
  83. void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
  84. MODIFIERKEYS beforeMod;
  85. *((uint8_t*)&beforeMod) = before;
  86. MODIFIERKEYS afterMod;
  87. *((uint8_t*)&afterMod) = after;
  88. if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
  89. Serial.println("LeftCtrl changed");
  90. }
  91. if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
  92. Serial.println("LeftShift changed");
  93. }
  94. if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
  95. Serial.println("LeftAlt changed");
  96. }
  97. if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
  98. Serial.println("LeftGUI changed");
  99. }
  100. if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
  101. Serial.println("RightCtrl changed");
  102. }
  103. if (beforeMod.bmRightShift != afterMod.bmRightShift) {
  104. Serial.println("RightShift changed");
  105. }
  106. if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
  107. Serial.println("RightAlt changed");
  108. }
  109. if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
  110. Serial.println("RightGUI changed");
  111. }
  112. }
  113. void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
  114. {
  115. Serial.print("UP ");
  116. PrintKey(mod, key);
  117. }
  118. void KbdRptParser::OnKeyPressed(uint8_t key)
  119. {
  120. Serial.print("ASCII: ");
  121. Serial.println((char)key);
  122. };
  123. USB Usb;
  124. USBHub Hub(&Usb);
  125. HIDBoot < HID_PROTOCOL_KEYBOARD | HID_PROTOCOL_MOUSE > HidComposite(&Usb);
  126. HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
  127. HIDBoot<HID_PROTOCOL_MOUSE> HidMouse(&Usb);
  128. //uint32_t next_time;
  129. KbdRptParser KbdPrs;
  130. MouseRptParser MousePrs;
  131. void setup()
  132. {
  133. Serial.begin( 115200 );
  134. #if !defined(__MIPSEL__)
  135. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  136. #endif
  137. Serial.println("Start");
  138. if (Usb.Init() == -1)
  139. Serial.println("OSC did not start.");
  140. delay( 200 );
  141. //next_time = millis() + 5000;
  142. HidComposite.SetReportParser(0, (HIDReportParser*)&KbdPrs);
  143. HidComposite.SetReportParser(1, (HIDReportParser*)&MousePrs);
  144. HidKeyboard.SetReportParser(0, (HIDReportParser*)&KbdPrs);
  145. HidMouse.SetReportParser(0, (HIDReportParser*)&MousePrs);
  146. }
  147. void loop()
  148. {
  149. Usb.Task();
  150. }